Skip to content
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

dtype is not defined when using ArrayLike with get_type_hints #19327

Closed
thewtex opened this issue Jun 24, 2021 · 2 comments · Fixed by #19328
Closed

dtype is not defined when using ArrayLike with get_type_hints #19327

thewtex opened this issue Jun 24, 2021 · 2 comments · Fixed by #19328

Comments

@thewtex
Copy link
Contributor

thewtex commented Jun 24, 2021

dtype is not defined when using numpy.typing.ArrayLike with typing.get_type_hints and numpy==1.21.0.

Reproducing code example:

from typing import get_type_hints

import numpy as np

from numpy.typing import ArrayLike

def give_me_an_array(array: ArrayLike):
    pass

type_hints = get_type_hints(give_me_an_array)

Error message:

Traceback (most recent call last):
  File "/tmp/test.py", line 10, in <module>
    type_hints = get_type_hints(gimme_an_array)
  File "/home/matt/bin/miniconda/envs/numpy/lib/python3.8/typing.py", line 1264, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "/home/matt/bin/miniconda/envs/numpy/lib/python3.8/typing.py", line 272, in _eval_type
    ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
  File "/home/matt/bin/miniconda/envs/numpy/lib/python3.8/typing.py", line 272, in <genexpr>
    ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
  File "/home/matt/bin/miniconda/envs/numpy/lib/python3.8/typing.py", line 272, in _eval_type
    ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
  File "/home/matt/bin/miniconda/envs/numpy/lib/python3.8/typing.py", line 272, in <genexpr>
    ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
  File "/home/matt/bin/miniconda/envs/numpy/lib/python3.8/typing.py", line 270, in _eval_type
    return t._evaluate(globalns, localns)
  File "/home/matt/bin/miniconda/envs/numpy/lib/python3.8/typing.py", line 518, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
NameError: name 'dtype' is not defined

NumPy/Python version information:

This occurs with numpy 1.21.0. It did not occur with 1.20.3 or previous versions.

@BvB93
Copy link
Member

BvB93 commented Jun 24, 2021

So this is admittedly a rather annoying issue that highlights some serious limitations of typing.get_type_hints.

dtype is currently expressed as a string in the relevant union, which is fine for static type checking, but means
that you'll have to passed the stringified objects via either globals or locals. Except that this simply introduces
a new issue as dtype is not subscriptable during runtime, so you'll get a TypeError and are back to square one.

ArrayLike = Union[
_RecursiveSequence,
_ArrayLike[
"dtype[Any]",
Union[bool, int, float, complex, str, bytes]
],
]

In any case, I've just created #19328 which will resolve the issue once it's backported for 1.21.1.

EDIT: xref python/typing#819

@BvB93 BvB93 added this to the 1.21.1 release milestone Jun 24, 2021
@thewtex
Copy link
Contributor Author

thewtex commented Jun 24, 2021

@BvB93 thank you!! 🙏

thewtex added a commit to InsightSoftwareConsortium/ITK that referenced this issue Aug 11, 2021
This reverts commit b667ce4.

numpy 1.21.1 is available, incorporating the fix:

  numpy/numpy#19327

Closes #2613
thewtex added a commit to thewtex/ITK that referenced this issue Aug 11, 2021
This reverts commit b667ce4.

numpy 1.21.1 is available, incorporating the fix:

  numpy/numpy#19327

Closes InsightSoftwareConsortium#2613
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants