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

NameError: name '_DType_co' is not defined #5631

Closed
monocongo opened this issue Jul 23, 2021 · 7 comments
Closed

NameError: name '_DType_co' is not defined #5631

monocongo opened this issue Jul 23, 2021 · 7 comments

Comments

@monocongo
Copy link

What happened: I installed a package that has xarray as a dependency. I then ran the package's console script, which resulted in the NameError shown below.

What you expected to happen: Successful import of the xarray package.

Minimal Complete Verifiable Example:

$ conda create -n tstenv python=3.7
$ conda activate tstenv
(tstenv) $ pip install climate-indices
(tstenv) $ python
>>> import xarray
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/james/miniconda3/envs/tstenv/lib/python3.7/site-packages/xarray/__init__.py", line 3, in <module>
    from . import testing, tutorial, ufuncs
  File "/home/james/miniconda3/envs/tstenv/lib/python3.7/site-packages/xarray/testing.py", line 8, in <module>
    from xarray.core import duck_array_ops, formatting, utils
  File "/home/james/miniconda3/envs/tstenv/lib/python3.7/site-packages/xarray/core/duck_array_ops.py", line 16, in <module>
    from . import dask_array_compat, dask_array_ops, dtypes, npcompat, nputils
  File "/home/james/miniconda3/envs/tstenv/lib/python3.7/site-packages/xarray/core/npcompat.py", line 81, in <module>
    from numpy.typing import ArrayLike, DTypeLike
  File "/home/james/miniconda3/envs/tstenv/lib/python3.7/site-packages/numpy/typing/__init__.py", line 316, in <module>
    from ._dtype_like import (
  File "/home/james/miniconda3/envs/tstenv/lib/python3.7/site-packages/numpy/typing/_dtype_like.py", line 95, in <module>
    class _SupportsDType(Generic[_DType_co]):

Anything else we need to know?:
This does not happen if I just install xarray, so there seems to be a conflict with another dependency package at play here. Are there methods for finding namespace conflicts like this without resorting to a "manual" method of trying all the various combinations? It seems that I've included a version of another package in my requirements that has a namespace conflict with xarray -- how can I work out which this is? Or maybe a simpler solution is to not use specific versions in the requirements.txt of the package and instead let conda work out the correct/latest versions?

Thanks in advance for any ideas on where to look to resolve this issue (and in general for all the work that goes into xarray's care and feeding).

Environment:
Linux (Ubuntu 20.04)
Anaconda, Python 3.7

$ conda list xarray
# packages in environment at /home/james/miniconda3/envs/tstenv:
#
# Name                    Version                   Build  Channel
xarray                    0.18.2                   pypi_0    pypi
@max-sixty
Copy link
Collaborator

@monocongo does it raise an error if you try importing numpy without xarray installed?

@menzel-gfdl
Copy link

menzel-gfdl commented Jul 23, 2021

I get the same error, but can import numpy with/without xarray installed. I'm running python 3.7.7 on RHEL 7.9 (Maipo):

$ pip install --upgrade pip
$ pip install xarray
$ python3
>>> import numpy
>>> import xarray
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "env/lib/python3.7/site-packages/xarray/__init__.py", line 3, in <module>
    from . import testing, tutorial, ufuncs
  File "env/lib/python3.7/site-packages/xarray/testing.py", line 8, in <module>
    from xarray.core import duck_array_ops, formatting, utils
  File "env/lib/python3.7/site-packages/xarray/core/duck_array_ops.py", line 16, in <module>
    from . import dask_array_compat, dask_array_ops, dtypes, npcompat, nputils
  File "env/lib/python3.7/site-packages/xarray/core/npcompat.py", line 81, in <module>
    from numpy.typing import ArrayLike, DTypeLike
  File "env/lib/python3.7/site-packages/numpy/typing/__init__.py", line 316, in <module>
    from ._dtype_like import (
  File "env/lib/python3.7/site-packages/numpy/typing/_dtype_like.py", line 95, in <module>
    class _SupportsDType(Generic[_DType_co]):
NameError: name '_DType_co' is not defined

@shoyer
Copy link
Member

shoyer commented Jul 23, 2021

What happens if you just write import numpy.typing ?

@menzel-gfdl
Copy link

menzel-gfdl commented Jul 23, 2021

I get the same error, as expected:

>>> import numpy.typing
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "env/lib/python3.7/site-packages/numpy/typing/__init__.py", line 316, in <module>
    from ._dtype_like import (
  File "env/lib/python3.7/site-packages/numpy/typing/_dtype_like.py", line 95, in <module>
    class _SupportsDType(Generic[_DType_co]):
NameError: name '_DType_co' is not defined
>>> numpy.version.version
'1.21.1'

numpy is aware of this issue. It looks like it is only an issue for python 3.7.

@shoyer
Copy link
Member

shoyer commented Jul 23, 2021

OK, closing this over here since it seems to be a bug with numpy rather than xarray

@shoyer shoyer closed this as completed Jul 23, 2021
@rhkleijn
Copy link
Contributor

rhkleijn commented Jul 23, 2021

From numpy/numpy#19521: installing typing-extensions may help when running on python 3.7.

@monocongo
Copy link
Author

Thanks to all for your help. Installing typing-extensions did solve the problem, thanks for the heads up @rhkleijn

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

5 participants