8000 BUG: typing doesn't handle type aliases via Python types · Issue #21900 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: typing doesn't handle type aliases via Python types #21900

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
bmerry opened this issue Jul 1, 2022 · 5 comments
Closed

BUG: typing doesn't handle type aliases via Python types #21900

bmerry opened this issue Jul 1, 2022 · 5 comments

Comments

@bmerry
Copy link
Contributor
bmerry commented Jul 1, 2022

Describe the issue:

numpy 1.20 encouraged specifying plain bool as a dtype as an equivalent to np.bool_, but these aliases don't behave the same as the explicit numpy versions. mypy infers the dtype as "Any" instead. See the example below, where I expected both lines to output the same type.

Reproduce the code example:

import numpy as np

def what_the() -> None:
    reveal_type(np.arange(10, dtype=bool))
    reveal_type(np.arange(10, dtype=np.bool_))

Error message:

No error, but output from mypy 0.961:

show_type2.py:4: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[Any]]"
show_type2.py:5: note: Revealed type is "numpy.ndarray[Any, numpy.dtype[numpy.bool_]]"


### NumPy/Python version information:

1.23.0 3.10.5 (main, Jun 11 2022, 16:53:24) [GCC 9.4.0]
@bibhabasumohapatra
Copy link
numpy.__version__ =  '1.23.0'
python version = Python 3.9.12

image

I tried to do this but got the expected result

but reproducing the results: I could not do that . . .

image

But I tried : np.arange(10, dtype=int) and np.arange(10, dtype=float) which mapped to np.int and np.float64 fine.

@bmerry
Copy link
Contributor Author
bmerry commented Jul 2, 2022

@bibhabasumohapatra you're looking at the runtime type. This bug is about static typing.

@bibhabasumohapatra
Copy link
bibhabasumohapatra commented Jul 2, 2022

@bibhabasumohapatra you're looking at the runtime type. This bug is about static typing.

Yes, looks like mypy issue. Sorry for misunderstanding. Should check the source more.

@bmerry
Copy link
Contributor Author
bmerry commented Jul 3, 2022

In #21898, @BvB93 suggested that this might duplicate a known issue in #19252.

@BvB93
Copy link
Member
BvB93 commented Jul 6, 2022

As was noted by @bmerry, this is a duplicate of #19252 and I’d suggest keep this discussion contained to that issue.

But to summarise: it’s non-trivial to map builtins types to their np.generic counterpart with the currently available typing tools, hence why the former will more often than not resolve to a Any-based data type.

@BvB93 BvB93 closed this as completed Jul 6, 2022
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

3 participants
0