Skip to content

Conversation

randolf-scholz
Copy link
Contributor

Fixes #12562.

Essentially a reopen of #12939 with additional tests. This PR was closed due to a misunderstanding.

Note that an earlier comment by JelleZijlstra suggested that SupportsBool is equivalent to object in practice. This does not seem to quite be the case: Code sample in pyright playground

from typing import Protocol, runtime_checkable

@runtime_checkable
class SupportsBool(Protocol):
    def __bool__(self) -> bool: ...

x: SupportsBool = True
y: SupportsBool = object()  # mypy: ❌ pyright:  ❌

assert isinstance(x, SupportsBool)  # ✅
assert not isinstance(y, SupportsBool)  # ✅

Indeed, despite the documentation mentioning object.__bool__, this method is not defined on object. Rather, the truthiness of an object is defined roughly like:

if hasattr(obj, "__bool__"):
    return obj.__bool__()
if hasattr(obj, "__len__"):
   return bool(len(obj))
return True

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

colour (https://github.com/colour-science/colour)
- colour/plotting/colorimetry.py:565: error: Value of type variable "SupportsRichComparisonT" of "min" cannot be "floating[_16Bit] | floating[_32Bit] | float64"  [type-var]
- colour/plotting/colorimetry.py:566: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "floating[_16Bit] | floating[_32Bit] | float64"  [type-var]

jax (https://github.com/google/jax)
+ jax/_src/pallas/mosaic/pipeline.py:1779: error: Unused "type: ignore" comment  [unused-ignore]

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/types/relations.py:2986: error: Value of type variable "SupportsRichComparisonT" of "sorted" cannot be "NumericValue | float"  [type-var]

arviz (https://github.com/arviz-devs/arviz)
- arviz/stats/ecdf_utils.py:72: error: Value of type variable "SupportsRichComparisonT" of "min" cannot be "Any | float64"  [type-var]

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@srittau srittau merged commit bd17a57 into python:main Jul 16, 2025
64 checks passed
@cdce8p cdce8p mentioned this pull request Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive with builtins.min(T, T) if T.__lt__ doesn't return a builtins.bool

3 participants