Skip to content

Commit

Permalink
"numpy.typing.NDArray" x 7.
Browse files Browse the repository at this point in the history
This commit is the next in a commit chain adding portable support for
the third-party PEP-noncompliant `numpy.typing.NDArray` type hint newly
introduced with NumPy >= 1.21.0, en route to resolving issue #42 kindly
submitted by NumPy extraordinaire @Antyos. Specifically, this commit
significantly improves testing of our private
`beartype._util.hint.nonpep.mod.utilmodnumpy.reduce_hint_numpy_ndarray()`
function. Additional testing and documentation is still warranted,
because safety first saves codebases. (*Decorative oratory!*)
  • Loading branch information
leycec committed Aug 4, 2021
1 parent 3a5c3b5 commit 6be963e
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 379 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/python_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ env:
# dependencies, fully exercising all tests necessitates these pre-flight
# test-time dependencies. These include:
# * "tox", the only mandatory test-time dependency.
_PIP_PACKAGE_NAMES: |
tox
_PIP_PACKAGE_NAMES: tox
# _PIP_PACKAGE_NAMES: |
# tox

# ....................{ MAIN }....................
jobs:
Expand Down
3 changes: 2 additions & 1 deletion beartype/_decor/_code/_pep/_pephint.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,8 @@ def _enqueue_hint_child(pith_child_expr: str) -> str:
# converting hints we'd rather *NOT* explicitly support (e.g.,
# third-party "numpy.typing.NDArray" hints) into semantically
# equivalent hints we would (e.g., first-party beartype validators).
hint_curr = get_hint_reduced(hint_curr)
hint_curr = get_hint_reduced(
hint=hint_curr, hint_label=hint_curr_label)

#FIXME: Comment this sanity check out after we're sufficiently
#convinced this algorithm behaves as expected. While useful, this check
Expand Down
2 changes: 1 addition & 1 deletion beartype/_decor/_error/_errorsleuth.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def hint(self, hint: Any) -> None:

# Reduce the currently visited hint to a lower-level hint-like object
# associated with this hint if this hint satisfies a condition.
hint = get_hint_reduced(hint)
hint = get_hint_reduced(hint=hint, hint_label=self.exception_label)

# If this hint is PEP-compliant...
if is_hint_pep(hint):
Expand Down
296 changes: 0 additions & 296 deletions beartype/_util/data/hint/pep/proposal/datapep544.py

This file was deleted.

21 changes: 16 additions & 5 deletions beartype/_util/hint/nonpep/mod/utilnonpepnumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
#would just consume all available memory. *sigh*

# @callable_cached
def reduce_hint_numpy_ndarray(hint: Any) -> Any:
def reduce_hint_numpy_ndarray(
# Mandatory parameters.
hint: Any,

# Optional parameters.
hint_label: str = 'Annotated',
) -> Any:
'''
Beartype validator validating arbitrary objects to be NumPy arrays whose
**NumPy array data type** (i.e., :class:`numpy.dtype` instance) is equal to
Expand All @@ -64,6 +70,9 @@ def reduce_hint_numpy_ndarray(hint: Any) -> Any:
----------
hint : object
PEP-noncompliant typed NumPy array to return the data type of.
hint_label : Optional[str]
Human-readable label prefixing this object's representation in the
exception message raised by this function. Defaults to ``"Annotated"``.
Raises
----------
Expand Down Expand Up @@ -115,7 +124,9 @@ def reduce_hint_numpy_ndarray(hint: Any) -> Any:
# If this hint is *NOT* a typed NumPy array, raise an exception.
if hint_sign is not HintSignNumpyArray:
raise BeartypeDecorHintNonPepNumPyException(
f'Type hint {repr(hint)} not typed NumPy array.')
f'{hint_label} type hint {repr(hint)} not NumPy array type hint '
f'(i.e., "numpy.typing.NDArray" instance).'
)
# Else, this hint is a typed NumPy array.

# Objects subscripting this hint if any *OR* the empty tuple otherwise.
Expand All @@ -125,7 +136,7 @@ def reduce_hint_numpy_ndarray(hint: Any) -> Any:
# malformed as a typed NumPy array. In this case, raise an exception.
if len(hint_args) != 2:
raise BeartypeDecorHintNonPepNumPyException(
f'Typed NumPy array {repr(hint)} '
f'{hint_label} NumPy array type hint {repr(hint)} '
f'not subscripted by exactly two arguments.'
)
# Else, this hint was subscripted by exactly two arguments.
Expand All @@ -143,7 +154,7 @@ def reduce_hint_numpy_ndarray(hint: Any) -> Any:
# is malformed as a data type subhint. In this case, raise an exception.
if len(hint_dtype_subhint_args) != 1:
raise BeartypeDecorHintNonPepNumPyException(
f'Typed NumPy array {repr(hint)} '
f'{hint_label} NumPy array type hint {repr(hint)} '
f'data type subhint {repr(hint_dtype_subhint)} '
f'not subscripted by exactly one argument.'
)
Expand Down Expand Up @@ -173,7 +184,7 @@ def reduce_hint_numpy_ndarray(hint: Any) -> Any:
# numpy.ndarray[typing.Any, numpy.dtype['wut']] # <-- you kidding me?
except Exception as exception:
raise BeartypeDecorHintNonPepNumPyException(
f'Typed NumPy array {repr(hint)} '
f'{hint_label} NumPy array type hint {repr(hint)} '
f'data type {repr(hint_dtype_like)} invalid '
f'(i.e., neither data type nor coercible to data type).'
) from exception
Expand Down
Loading

0 comments on commit 6be963e

Please sign in to comment.