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

Skip to content

SupportsRichComparisonT does not apply to int | numpy.integer #12728

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
inducer opened this issue Oct 2, 2024 · 2 comments
Closed

SupportsRichComparisonT does not apply to int | numpy.integer #12728

inducer opened this issue Oct 2, 2024 · 2 comments

Comments

@inducer
Copy link

inducer commented Oct 2, 2024

import numpy as np
from typing import Union

IntegerT = Union[np.integer, int]

def f(a: IntegerT, b: IntegerT) -> IntegerT:
    return max(a, b)
$ mypy mypybug2.py
mypybug2.py:7: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "integer[Any] | int"  [type-var]
Found 1 error in 1 file (checked 1 source file)

I don't see anything wrong with the expression involving max, so the error is surprising. Even substituting a concrete integer type like np.int64 triggers the same error.

$ mypy -V
mypy 1.11.0 (compiled: yes)
$ python -V
Python 3.12.6
@srittau
Copy link
Collaborator

srittau commented Oct 2, 2024

If I understand the problem correctly, the problem is that np.integer's comparison operators return np.bool, but the typeshed operator expect a "proper" Python bool. np.bool is not derived from bool and therefore incompatible. This is a problem with numpy's type annotations.

A possible fix is for numpy's type stubs to declare that np.bool derives from bool, even if it doesn't at runtime. But that's a decision the numpy project has to make.

@srittau srittau closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2024
@inducer
Copy link
Author

inducer commented Oct 2, 2024

Dupe of numpy/numpy#27251.

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

No branches or pull requests

2 participants