Skip to content

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

@jorenham

Description

@jorenham

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).

So at runtime we have

>>> import numpy as np
>>> np.__version__
'2.1.0'
>>> np.int32(8) < np.int32(9)
np.True_
>>> min(np.int32(8), np.int32(9))
np.int32(8)

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions