Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TYP: Error when calling min() or max() on NumPy scalars #27251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DiegoBaldassarMilleuno opened this issue Aug 20, 2024 · 6 comments
Closed

TYP: Error when calling min() or max() on NumPy scalars #27251

DiegoBaldassarMilleuno opened this issue Aug 20, 2024 · 6 comments
Assignees

Comments

@DiegoBaldassarMilleuno
Copy link
Contributor

Describe the issue:

Since NumPy version 2.1.0 both mypy and pyright complain when calling any of the min() or max() builtins on numpy scalars.
The type doesn't matter (int32, int64, float32, float64, ...)
The error does not occur in version 2.0.1.

Reproduce the code example:

import numpy

v1 = numpy.int32(9)
v2 = numpy.int32(10)

print(min(v1, v2))

Error message:

MYPY:

delme2.py:7: error: No overload variant of "min" matches argument types "signedinteger[_32Bit]", "signedinteger[_32Bit]"  [call-overload]
delme2.py:7: note: Possible overload variants:
delme2.py:7: note:     def [SupportsRichComparisonT] min(SupportsRichComparisonT, SupportsRichComparisonT, /, *_args: SupportsRichComparisonT, key: None = ...) -> SupportsRichComparisonT
delme2.py:7: note:     def [_T] min(_T, _T, /, *_args: _T, key: Callable[[_T], SupportsDunderLT[Any] | SupportsDunderGT[Any]]) -> _T
delme2.py:7: note:     def [SupportsRichComparisonT] min(Iterable[SupportsRichComparisonT], /, *, key: None = ...) -> SupportsRichComparisonT
delme2.py:7: note:     def [_T] min(Iterable[_T], /, *, key: Callable[[_T], SupportsDunderLT[Any] | SupportsDunderGT[Any]]) -> _T
delme2.py:7: note:     def [SupportsRichComparisonT, _T] min(Iterable[SupportsRichComparisonT], /, *, key: None = ..., default: _T) -> SupportsRichComparisonT | _T
delme2.py:7: note:     def [_T1, _T2] min(Iterable[_T1], /, *, key: Callable[[_T1], SupportsDunderLT[Any] | SupportsDunderGT[Any]], default: _T2) -> _T1 | _T2
Found 1 error in 1 file (checked 1 source file)

PYRIGHT:

delme2.py
  delme2.py:7:5 - 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)
  delme2.py:7:9 - 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)
2 errors, 0 warnings, 0 informations

Python and NumPy Versions:

2.1.0
3.12.5 (main, Aug 6 2024, 19:08:49) [Clang 15.0.0 (clang-1500.3.9.4)]

Type-checker version and settings:

mypy version: 1.11.1 (compiled: yes)
pyright version: 1.1.376
Both run with no extra flags.

Additional typing packages.

No response

@jorenham
Copy link
Member

My guess is that this has to do something with #26942

@jorenham jorenham self-assigned this Aug 20, 2024
@jorenham
Copy link
Member

jorenham commented Aug 20, 2024

This has to do with the fact that typeshed requires __lt__ to always return a builtins.bool type in builtins.min, whereas numpy.int32.__lt__ returns a numpy.bool type:
https://github.com/python/typeshed/blob/c5ba20f3156e667b63af47a013139c26ad5ba3d4/stdlib/builtins.pyi#L1520-L1532

TLDR; I believe that this is actually a typeshed issue

@jorenham
Copy link
Member

But I'll try to see whether I can find a workaround

@jorenham
Copy link
Member

But as a workaround for your example @DiegoBaldassarMilleuno, you could replace the min with numpy.minimum.

@jorenham
Copy link
Member

There's a PR with a fix for this now: python/typeshed#12573

So is it OK with you if we close this issue, @DiegoBaldassarMilleuno?

@jorenham jorenham added the 57 - Close? Issues which may be closable unless discussion continued label Aug 21, 2024
@DiegoBaldassarMilleuno
Copy link
Contributor Author

@jorenham Great, thank you! Yes, I'll close this.

@jorenham jorenham removed the 57 - Close? Issues which may be closable unless discussion continued label Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants