8000 ENH: make typing module available at runtime by person142 · Pull Request #16558 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: make typing module available at runtime #16558

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

Merged
merged 7 commits into from
Jun 17, 2020
Merged
Prev Previous commit
Next Next commit
DOC: clarify ArrayLike example in typing docs
  • Loading branch information
person142 committed Jun 16, 2020
commit c63f2333288772defcd84627986b035b6e7018ef
26 changes: 21 additions & 5 deletions numpy/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,26 @@

.. code-block:: python

np.array(x**2 for x in range(10))
>>> np.array(x**2 for x in range(10))
array(<generator object <genexpr> at 0x10c004cd0>, dtype=object)

is valid NumPy code which will create an object array. The types will
complain about this usage however.
is valid NumPy code which will create a 0-dimensional object
array. Type checkers will complain about the above example when using
the NumPy types however. If you really intended to do the above, then
you can either use a ``# type: ignore`` comment:

.. code-block:: python

>>> np.array(x**2 for x in range(10)) # type: ignore

or explicitly type the array like object as ``Any``:

.. code-block:: python

>>> from typing import Any
>>> array_like: Any = (x**2 for x in range(10))
>>> np.array(array_like)
array(<generator object <genexpr> at 0x1192741d0>, dtype=object)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a brief note on the suggested work-around?

The obvious way would be to add a comment disabling typing:

  np.array(x**2 for x in range(10))  # type: ignore

Are there other recommended options?

I think we've also discussed making checks less strict if dtype=object is specified, e.g.,

  np.array(x**2 for x in range(10), dtype=object)

I don't know if that works yet. If it does, perhaps we should mention it, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other recommended options?

The other way we test for:

https://github.com/numpy/numpy/blob/master/numpy/tests/typing/pass/array_like.py#L43)

is adding an explicit Any annotation. I've added examples of both methods to the docs.

I don't know if that works yet. If it does, perhaps we should mention it, too.

Seems like @seberg would probably know the answer to that?

ndarray
~~~~~~~
Expand All @@ -52,8 +68,8 @@

.. code-block:: python

x = np.array([1, 2])
x.dtype = np.bool_
x = np.array([1, 2])
x.dtype = np.bool_

This sort of mutation is not allowed by the types. Users who want to
write statically typed code should insted use the `numpy.ndarray.view`
Expand Down
0