Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Type error with np.dtype #42

Closed
person142 opened this issue Mar 20, 2020 · 0 comments · Fixed by #43
Closed

Type error with np.dtype #42

person142 opened this issue Mar 20, 2020 · 0 comments · Fixed by #43

Comments

@person142
Copy link
Member

Minimal reproducer

Running mypy on the following code:

import numpy as np

object_dtype = [('field1', object)]
np.dtype(object_dtype)

Results in the following error:

$ mypy test_dtype.py
test_dtype.py:4: error: Argument 1 to "dtype" has incompatible type "List[Tuple[str, Type[object]]]"; expected "Union[dtype, None, type, str, Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Union[Tuple[Union[str, Tuple[str, str]], Any], Tuple[Union[str, Tuple[str, str]], Any, Union[int, Sequence[int]]]]], Dict[str, Union[Sequence[str], Sequence[Any], Sequence[int], Sequence[Union[bytes, str, None]], int]], Dict[str, Tuple[Any, int]], Tuple[Any, Any]]"

Note that putting object_dtype on the previous line is important; otherwise the context of the function signature:

https://mypy.readthedocs.io/en/latest/type_inference_and_annotations.html#context-in-type-inference

will broaden the type to List[Tuple[str, Any]] (I think.)

Context

Stumbled across this while working on typing in SciPy, with the current version of the NumPy stubs there's an error because of this line:

https://github.com/scipy/scipy/blob/master/scipy/io/matlab/tests/test_mio.py#L140

Speculation

I think this is happening because List is invariant and object is a subtype of Any, so despite the fact that _DtypeLikeNested is typed as any here:

https://github.com/numpy/numpy-stubs/blob/master/numpy-stubs/__init__.pyi#L41

object still isn't let through here:

https://github.com/numpy/numpy-stubs/blob/master/numpy-stubs/__init__.pyi#L61

person142 added a commit to person142/scipy that referenced this issue Mar 20, 2020
One error appears to be an issue in the NumPy stubs:

numpy/numpy-stubs#42

The other is fixed by modifying how we import `linalg_version` inside
the linalg setup.
person142 added a commit to person142/numpy-stubs that referenced this issue Mar 21, 2020
Closes numpy#42.

`np.dtype` accepts a wide variety of lists as dtypes, some examples
include:

- `[('R', 'u1'), ('G', np.unicode_, 1)]`
- `[('R', 'u1', (2, 2)), ('G', np.unicode_, 1)]`

Without further hints mypy is going to type those as `List[object]`,
so precise hints will require that users add annotations. In addition,
typing the above is challenging because the inputs have to be a list,
which is invariant, so you can't use `Union`s inside the list to
reduce the number of cases. Work around that by just accepting
`List[Any]`.
shoyer pushed a commit that referenced this issue Mar 24, 2020
Closes #42.

`np.dtype` accepts a wide variety of lists as dtypes, some examples
include:

- `[('R', 'u1'), ('G', np.unicode_, 1)]`
- `[('R', 'u1', (2, 2)), ('G', np.unicode_, 1)]`

Without further hints mypy is going to type those as `List[object]`,
so precise hints will require that users add annotations. In addition,
typing the above is challenging because the inputs have to be a list,
which is invariant, so you can't use `Union`s inside the list to
reduce the number of cases. Work around that by just accepting
`List[Any]`.
person142 added a commit to person142/scipy that referenced this issue Mar 25, 2020
Now that numpy/numpy-stubs#42 is resolved we
can remove an ignore from `test_mio.py`.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant