You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
False positive with builtins.min(T, T) if T.__lt__ doesn't return a builtins.bool #12562
Since NumPy 2.1 all subtypes of numpy.number implement __lt__ that returns a numpy.bool (which isn't a builtins.bool), matching the runtime behavior (numpy/numpy#26942).
But as was noticed in numpy/numpy#27251, the consequence of this is that builtins.min with e.g. numpy.int32 input is now rejected (tested with the latest mypy and pyright).
But mypy 1.11.1 rejects min(np.int32(8), np.int32(9)) as:
error: No overload variant of "min" matches argument types "signedinteger[_32Bit]", "signedinteger[_32Bit]" [call-overload]
Similarly, Pyright 1.1.376 reports
error: Argument of type "int32" cannot be assigned to parameter "arg1" of type "SupportsRichComparisonT@min" in function "min"
Type "int32" is incompatible with type "SupportsRichComparison"
Type "int32" is incompatible with type "SupportsRichComparison"
"signedinteger[_32Bit]" is incompatible with protocol "SupportsDunderLT[Any]"
"__lt__" is an incompatible type
No overloaded function matches type "(other: _T_contra@SupportsDunderLT, /) -> bool"
"signedinteger[_32Bit]" is incompatible with protocol "SupportsDunderGT[Any]"
"__gt__" is an incompatible type
No overloaded function matches type "(other: _T_contra@SupportsDunderGT, /) -> bool" (reportArgumentType)
and
error: Argument of type "int32" cannot be assigned to parameter "arg2" of type "SupportsRichComparisonT@min" in function "min"
Type "int32" is incompatible with type "SupportsRichComparison"
Type "int32" is incompatible with type "SupportsRichComparison"
"signedinteger[_32Bit]" is incompatible with protocol "SupportsDunderLT[Any]"
"__lt__" is an incompatible type
No overloaded function matches type "(other: _T_contra@SupportsDunderLT, /) -> bool"
"signedinteger[_32Bit]" is incompatible with protocol "SupportsDunderGT[Any]"
"__gt__" is an incompatible type
No overloaded function matches type "(other: _T_contra@SupportsDunderGT, /) -> bool" (reportArgumentType)
changed the title [-]False negative with `builtins.min(T, T)` if `T.__lt__` doesn't return a `builtins.bool`[/-][+]False positive with `builtins.min(T, T)` if `T.__lt__` doesn't return a `builtins.bool`[/+]on Aug 20, 2024
I'm not sure why __bool__ is not declared on object, but there is probably some reason for it. EDIT: It's because object does not have __bool__ at runtime.
PR #12573 replaces these bool return hints with object. mypy-primer looks good.
However, it makes me wonder whether the bool return hint should be replaced by object in other cases as well, such as SupportsGetItem.__contains__ and similar protocols.
Activity
[-]False negative with `builtins.min(T, T)` if `T.__lt__` doesn't return a `builtins.bool`[/-][+]False positive with `builtins.min(T, T)` if `T.__lt__` doesn't return a `builtins.bool`[/+]srittau commentedon Aug 21, 2024
I think this ultimately boils down to the definition of
SupportsDunderLTetc:typeshed/stdlib/_typeshed/__init__.pyi
Lines 87 to 97 in 86e7416
These expect the dunders to return a real
bool. I'm not sure hownp.boolis implemented, so this may be hard to fix.ngoldbaum commentedon Aug 21, 2024
Unfortunately
boolisn't subclassable, sonp.boolis its own separate thing. It's backed by 0 and 1 8-bit int values.randolf-scholz commentedon Aug 21, 2024
This should be solvable by returning
SupportsBoolwhereJelleZijlstra commentedon Aug 21, 2024
That protocol is equivalent to
objectin practice, so we'd be better off simply usingobjectas the return type in theSupportDunder*protocols.randolf-scholz commentedon Aug 21, 2024
@JelleZijlstra At runtime yes, but currently the type hints for
objectdo not include__bool__. Hence, it makes a difference at type checking time: Code sample in pyright playgroundI'm not sure why
__bool__is not declared onobject, but there is probably some reason for it.EDIT: It's because
objectdoes not have__bool__at runtime.randolf-scholz commentedon Aug 21, 2024
OK
objectseems the right thing as the following works at runtime:randolf-scholz commentedon Aug 21, 2024
There is also a very similar definition in
_operator.pyi, with this chance these maybe can be combinedtypeshed/stdlib/_operator.pyi
Lines 21 to 33 in 8307b3c
randolf-scholz commentedon Aug 21, 2024
PR #12573 replaces these
boolreturn hints withobject. mypy-primer looks good.However, it makes me wonder whether the
boolreturn hint should be replaced byobjectin other cases as well, such asSupportsGetItem.__contains__and similar protocols.Temporarily ignore 2 mypy type-var errors
Temporarily ignore 2 mypy type-var errors
Temporarily ignore 2 mypy type-var errors
srittau commentedon Jul 7, 2025
I don't think we'll get a consensus here. Closing this in favor of the status quo.