Skip to content

BUG: typing overload missing for dtype-creation with metadata #23886

@kmuehlbauer

Description

@kmuehlbauer

Describe the issue:

dt0 = np.dtype(float, metadata={"key": "value"}) as given as example in numpy doc fails for static type checking.

Reproduce the code example:

import numpy as np
dt = np.dtype(float, metadata={"key": "value"})

Error message:

/home/kai/miniconda/envs/pycharm/bin/mypy --install-types --non-interactive --follow-imports=silent /home/kai/python/projects/xarray/xarray/tests/test_coding_strings.py
Found 1 error in 1 file (checked 1 source file)
xarray/tests/test_coding_strings.py:24: error: No overload variant of "dtype" matches argument types "Type[object]", "Dict[str, Type[str]]"  [call-overload]
xarray/tests/test_coding_strings.py:24: note: Possible overload variants:
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Type[_DTypeScalar_co], align: bool = ..., copy: bool = ...) -> dtype[_DTypeScalar_co]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Type[bool], align: bool = ..., copy: bool = ...) -> dtype[bool_]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Type[int], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Optional[Type[float]], align: bool = ..., copy: bool = ...) -> dtype[floating[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Type[complex], align: bool = ..., copy: bool = ...) -> dtype[complexfloating[Any, Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Type[str], align: bool = ..., copy: bool = ...) -> dtype[str_]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Type[bytes], align: bool = ..., copy: bool = ...) -> dtype[bytes_]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_uint8], Literal['uint8', 'u1', '=u1', '<u1', '>u1']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[_8Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_uint16], Literal['uint16', 'u2', '=u2', '<u2', '>u2']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[_16Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_uint32], Literal['uint32', 'u4', '=u4', '<u4', '>u4']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[_32Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_uint64], Literal['uint64', 'u8', '=u8', '<u8', '>u8']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[_64Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_ubyte], Literal['ubyte', 'B', '=B', '<B', '>B']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_ushort], Literal['ushort', 'H', '=H', '<H', '>H']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_uint], Literal['uintc', 'I', '=I', '<I', '>I']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_void_p], Type[c_size_t], Literal['uintp', 'uint0', 'P', '=P', '<P', '>P']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_ulong], Literal['ulong', 'uint', 'L', '=L', '<L', '>L']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_ulonglong], Literal['ulonglong', 'Q', '=Q', '<Q', '>Q']], align: bool = ..., copy: bool = ...) -> dtype[unsignedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_int8], Literal['int8', 'i1', '=i1', '<i1', '>i1']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[_8Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_int16], Literal['int16', 'i2', '=i2', '<i2', '>i2']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[_16Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_int32], Literal['int32', 'i4', '=i4', '<i4', '>i4']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[_32Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_int64], Literal['int64', 'i8', '=i8', '<i8', '>i8']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[_64Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_byte], Literal['byte', 'b', '=b', '<b', '>b']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_short], Literal['short', 'h', '=h', '<h', '>h']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_int], Literal['intc', 'i', '=i', '<i', '>i']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_ssize_t], Literal['intp', 'int0', 'p', '=p', '<p', '>p']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_long], Literal['long', 'int', 'int_', 'l', '=l', '<l', '>l']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_longlong], Literal['longlong', 'q', '=q', '<q', '>q']], align: bool = ..., copy: bool = ...) -> dtype[signedinteger[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['float16', 'f2', '=f2', '<f2', '>f2'], align: bool = ..., copy: bool = ...) -> dtype[floating[_16Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['float32', 'f4', '=f4', '<f4', '>f4'], align: bool = ..., copy: bool = ...) -> dtype[floating[_32Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['float64', 'f8', '=f8', '<f8', '>f8'], align: bool = ..., copy: bool = ...) -> dtype[floating[_64Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['half', 'e', '=e', '<e', '>e'], align: bool = ..., copy: bool = ...) -> dtype[floating[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_float], Literal['single', 'f', '=f', '<f', '>f']], align: bool = ..., copy: bool = ...) -> dtype[floating[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_double], Literal['double', 'float', 'float_', 'd', '=d', '<d', '>d']], align: bool = ..., copy: bool = ...) -> dtype[floating[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_longdouble], Literal['longdouble', 'longfloat', 'g', '=g', '<g', '>g']], align: bool = ..., copy: bool = ...) -> dtype[floating[Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['complex64', 'c8', '=c8', '<c8', '>c8'], align: bool = ..., copy: bool = ...) -> dtype[complexfloating[_32Bit, _32Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['complex128', 'c16', '=c16', '<c16', '>c16'], align: bool = ..., copy: bool = ...) -> dtype[complexfloating[_64Bit, _64Bit]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['csingle', 'singlecomplex', 'F', '=F', '<F', '>F'], align: bool = ..., copy: bool = ...) -> dtype[complexfloating[Any, Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['cdouble', 'complex', 'complex_', 'cfloat', 'D', '=D', '<D', '>D'], align: bool = ..., copy: bool = ...) -> dtype[complexfloating[Any, Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['clongdouble', 'clongfloat', 'longcomplex', 'G', '=G', '<G', '>G'], align: bool = ..., copy: bool = ...) -> dtype[complexfloating[Any, Any]]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_bool], Literal['?', '=?', '<?', '>?', 'bool', 'bool_', 'bool8']], align: bool = ..., copy: bool = ...) -> dtype[bool_]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['timedelta64', '=timedelta64', '<timedelta64', '>timedelta64', 'timedelta64[Y]', '=timedelta64[Y]', '<timedelta64[Y]', '>timedelta64[Y]', 'timedelta64[M]', '=timedelta64[M]', '<timedelta64[M]', '>timedelta64[M]', 'timedelta64[W]', '=timedelta64[W]', '<timedelta64[W]', '>timedelta64[W]', 'timedelta64[D]', '=timedelta64[D]', '<timedelta64[D]', '>timedelta64[D]', 'timedelta64[h]', '=timedelta64[h]', '<timedelta64[h]', '>timedelta64[h]', 'timedelta64[m]', '=timedelta64[m]', '<timedelta64[m]', '>timedelta64[m]', 'timedelta64[s]', '=timedelta64[s]', '<timedelta64[s]', '>timedelta64[s]', 'timedelta64[ms]', '=timedelta64[ms]', '<timedelta64[ms]', '>timedelta64[ms]', 'timedelta64[us]', '=timedelta64[us]', '<timedelta64[us]', '>timedelta64[us]', 'timedelta64[ns]', '=timedelta64[ns]', '<timedelta64[ns]', '>timedelta64[ns]', 'timedelta64[ps]', '=timedelta64[ps]', '<timedelta64[ps]', '>timedelta64[ps]', 'timedelta64[fs]', '=timedelta64[fs]', '<timedelta64[fs]', '>timedelta64[fs]', 'timedelta64[as]', '=timedelta64[as]', '<timedelta64[as]', '>timedelta64[as]', 'm', '=m', '<m', '>m', 'm8', '=m8', '<m8', '>m8', 'm8[Y]', '=m8[Y]', '<m8[Y]', '>m8[Y]', 'm8[M]', '=m8[M]', '<m8[M]', '>m8[M]', 'm8[W]', '=m8[W]', '<m8[W]', '>m8[W]', 'm8[D]', '=m8[D]', '<m8[D]', '>m8[D]', 'm8[h]', '=m8[h]', '<m8[h]', '>m8[h]', 'm8[m]', '=m8[m]', '<m8[m]', '>m8[m]', 'm8[s]', '=m8[s]', '<m8[s]', '>m8[s]', 'm8[ms]', '=m8[ms]', '<m8[ms]', '>m8[ms]', 'm8[us]', '=m8[us]', '<m8[us]', '>m8[us]', 'm8[ns]', '=m8[ns]', '<m8[ns]', '>m8[ns]', 'm8[ps]', '=m8[ps]', '<m8[ps]', '>m8[ps]', 'm8[fs]', '=m8[fs]', '<m8[fs]', '>m8[fs]', 'm8[as]', '=m8[as]', '<m8[as]', '>m8[as]'], align: bool = ..., copy: bool = ...) -> dtype[timedelta64]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['datetime64', '=datetime64', '<datetime64', '>datetime64', 'datetime64[Y]', '=datetime64[Y]', '<datetime64[Y]', '>datetime64[Y]', 'datetime64[M]', '=datetime64[M]', '<datetime64[M]', '>datetime64[M]', 'datetime64[W]', '=datetime64[W]', '<datetime64[W]', '>datetime64[W]', 'datetime64[D]', '=datetime64[D]', '<datetime64[D]', '>datetime64[D]', 'datetime64[h]', '=datetime64[h]', '<datetime64[h]', '>datetime64[h]', 'datetime64[m]', '=datetime64[m]', '<datetime64[m]', '>datetime64[m]', 'datetime64[s]', '=datetime64[s]', '<datetime64[s]', '>datetime64[s]', 'datetime64[ms]', '=datetime64[ms]', '<datetime64[ms]', '>datetime64[ms]', 'datetime64[us]', '=datetime64[us]', '<datetime64[us]', '>datetime64[us]', 'datetime64[ns]', '=datetime64[ns]', '<datetime64[ns]', '>datetime64[ns]', 'datetime64[ps]', '=datetime64[ps]', '<datetime64[ps]', '>datetime64[ps]', 'datetime64[fs]', '=datetime64[fs]', '<datetime64[fs]', '>datetime64[fs]', 'datetime64[as]', '=datetime64[as]', '<datetime64[as]', '>datetime64[as]', 'M', '=M', '<M', '>M', 'M8', '=M8', '<M8', '>M8', 'M8[Y]', '=M8[Y]', '<M8[Y]', '>M8[Y]', 'M8[M]', '=M8[M]', '<M8[M]', '>M8[M]', 'M8[W]', '=M8[W]', '<M8[W]', '>M8[W]', 'M8[D]', '=M8[D]', '<M8[D]', '>M8[D]', 'M8[h]', '=M8[h]', '<M8[h]', '>M8[h]', 'M8[m]', '=M8[m]', '<M8[m]', '>M8[m]', 'M8[s]', '=M8[s]', '<M8[s]', '>M8[s]', 'M8[ms]', '=M8[ms]', '<M8[ms]', '>M8[ms]', 'M8[us]', '=M8[us]', '<M8[us]', '>M8[us]', 'M8[ns]', '=M8[ns]', '<M8[ns]', '>M8[ns]', 'M8[ps]', '=M8[ps]', '<M8[ps]', '>M8[ps]', 'M8[fs]', '=M8[fs]', '<M8[fs]', '>M8[fs]', 'M8[as]', '=M8[as]', '<M8[as]', '>M8[as]'], align: bool = ..., copy: bool = ...) -> dtype[datetime64]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['str', 'str_', 'str0', 'unicode', 'unicode_', 'U', '=U', '<U', '>U'], align: bool = ..., copy: bool = ...) -> dtype[str_]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[c_char], Literal['bytes', 'bytes_', 'bytes0', 'S', '=S', '<S', '>S']], align: bool = ..., copy: bool = ...) -> dtype[bytes_]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Literal['void', 'void0', 'V', '=V', '<V', '>V'], align: bool = ..., copy: bool = ...) -> dtype[void]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Type[py_object[Any]], Literal['object', 'object_', 'O', '=O', '<O', '>O']], align: bool = ..., copy: bool = ...) -> dtype[object_]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: dtype[_DTypeScalar_co], align: bool = ..., copy: bool = ...) -> dtype[_DTypeScalar_co]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: _SupportsDType[dtype[_DTypeScalar_co]], align: bool = ..., copy: bool = ...) -> dtype[_DTypeScalar_co]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: str, align: bool = ..., copy: bool = ...) -> dtype[Any]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, Any]], align: bool = ..., copy: bool = ...) -> dtype[void]
xarray/tests/test_coding_strings.py:24: note:     def [_DTypeScalar_co <: generic] dtype(cls, dtype: Type[object], align: bool = ..., copy: bool = ...) -> dtype[object_]
xarray/tests/test_coding_strings.py:26: note: Revealed type is "Any"

Runtime information:

1.24.2
3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0]

[{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Zen',
'filepath': '/home/kai/miniconda/envs/xarray_311/lib/libopenblasp-r0.3.21.so',
'internal_api': 'openblas',
'num_threads': 16,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.21'}]
None

Context for the issue:

Over at xarray we use `numpy.dtype("O", metadata={"element_type": str}) to add metadata to the object dtype. In the process of code changes typing is introduced to these parts. That way the mypy issue was revealed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions