diff options
author | Josh Wilson <person142@users.noreply.github.com> | 2020-06-15 20:56:57 -0700 |
---|---|---|
committer | Josh Wilson <person142@users.noreply.github.com> | 2020-06-15 20:56:57 -0700 |
commit | c63f2333288772defcd84627986b035b6e7018ef (patch) | |
tree | 06d3974df43db5ef8d9094ccd19b1b0675f623b8 /numpy | |
parent | c88f5a232222b777a00e40648a6121428858df8d (diff) | |
download | numpy-c63f2333288772defcd84627986b035b6e7018ef.tar.gz |
DOC: clarify `ArrayLike` example in typing docs
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/typing/__init__.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index 1d377f8c4..f2000823f 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -39,10 +39,26 @@ example, .. 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) ndarray ~~~~~~~ @@ -52,8 +68,8 @@ the following code is valid: .. 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` |