8000 ENH,API: Add a protocol for representing nested sequences by BvB93 · Pull Request #18155 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH,API: Add a protocol for representing nested sequences #18155

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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
REL: Added a release note for npt.NestedSequence
  • Loading branch information
Bas van Beek committed Jan 12, 2021
commit 03910f39f44b02baaaf37d5ac0aa648d582f628c
32 changes: 32 additions & 0 deletions doc/release/upcoming_changes/18155.new_feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Added a protocol for representing nested sequences
--------------------------------------------------

The new `~numpy.typing.NestedSequence` protocol has been added to `numpy.typing`.
Per its name, the protocol can be used in static type checking for
representing `sequences <collections.abc.Sequence>` with arbitrary levels of nesting.

For example:

.. code-block:: python

from __future__ import annotations
from typing import Any, List, TYPE_CHECKING
import numpy as np
import numpy.typing as npt

def get_dtype(seq: npt.NestedSequence[int]) -> np.dtype[np.int_]:
return np.asarray(seq).dtype

a = func([1])
b = func([[1]])
c = func([[[1]]])
d = func([[[[1]]]])

if TYPE_CHECKING:
reveal_locals()
# note: Revealed local types are:
# note: a: numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]
# note: b: numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]
# note: c: numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]
# note: d: numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]

0