GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ENH: Refactor the typing "reveal" tests using `typing.assert_type` by BvB93 · Pull Request #24637 · numpy/numpy

ENH: Refactor the typing "reveal" tests using typing.assert_type #24637

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.

Sign up for GitHub

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
charris merged 5 commits into numpy:maintenance/1.26.x from BvB93:assert_type_backport
Sep 4, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

ENH: Refactor the typing "reveal" tests using typing.assert_type #24637

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
7690063
TYP: Refactor the typing reveal-test cases using `typing.assert_type`
BvB93 Aug 31, 2023
8294350
TYP: Refactor the typing misc-tests cases using `typing.assert_type`
BvB93 Sep 1, 2023
c650ab3
TYP: Overhaul the typing test suite
BvB93 Sep 1, 2023
c141c2b
TYP: Bump mypy to 1.5.1
BvB93 Sep 4, 2023
6d4d5aa
TYP: More `np.ctypeslib` test fixes for windows
BvB93 Sep 4, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
TYP: Refactor the typing reveal-test cases using typing.assert_type
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@BvB93
BvB93 committed Sep 4, 2023
commit 76900632b84c27753d0322cd3122e57f773e0dbc

There are no files selected for viewing

3 changes: 2 additions & 1 deletion 3 numpy/random/mtrand.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import builtins
from collections.abc import Callable
from typing import Any, Union, overload, Literal

Expand Down Expand Up @@ -224,7 +225,7 @@ class RandomState:
size: None | _ShapeLike = ...,
dtype: dtype[uint] | type[uint] | _UIntCodes | _SupportsDType[dtype[uint]] = ...,
) -> ndarray[Any, dtype[uint]]: ...
def bytes(self, length: int) -> bytes: ...
def bytes(self, length: int) -> builtins.bytes: ...
@overload
def choice(
self,
Expand Down
906 changes: 456 additions & 450 deletions 906 numpy/typing/tests/data/reveal/arithmetic.pyi
Show comments
View file
Open in desktop

Large diffs are not rendered by default.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

380 changes: 193 additions & 187 deletions 380 numpy/typing/tests/data/reveal/array_constructors.pyi
Show comments
View file
Open in desktop

Large diffs are not rendered by default.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

14 changes: 10 additions & 4 deletions 14 numpy/typing/tests/data/reveal/arraypad.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import sys
from collections.abc import Mapping
from typing import Any, SupportsIndex

import numpy as np
import numpy.typing as npt

if sys.version_info >= (3, 11):
from typing import assert_type
else:
from typing_extensions import assert_type

def mode_func(
ar: npt.NDArray[np.number[Any]],
width: tuple[int, int],
Expand All @@ -15,8 +21,8 @@ AR_i8: npt.NDArray[np.int64]
AR_f8: npt.NDArray[np.float64]
AR_LIKE: list[int]

reveal_type(np.pad(AR_i8, (2, 3), "constant")) # E: ndarray[Any, dtype[{int64}]]
reveal_type(np.pad(AR_LIKE, (2, 3), "constant")) # E: ndarray[Any, dtype[Any]]
assert_type(np.pad(AR_i8, (2, 3), "constant"), npt.NDArray[np.int64])
assert_type(np.pad(AR_LIKE, (2, 3), "constant"), npt.NDArray[Any])

reveal_type(np.pad(AR_f8, (2, 3), mode_func)) # E: ndarray[Any, dtype[{float64}]]
reveal_type(np.pad(AR_f8, (2, 3), mode_func, a=1, b=2)) # E: ndarray[Any, dtype[{float64}]]
assert_type(np.pad(AR_f8, (2, 3), mode_func), npt.NDArray[np.float64])
assert_type(np.pad(AR_f8, (2, 3), mode_func, a=1, b=2), npt.NDArray[np.float64])
30 changes: 20 additions & 10 deletions 30 numpy/typing/tests/data/reveal/arrayprint.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import sys
import contextlib
from collections.abc import Callable
from typing import Any

import numpy as np
from numpy.core.arrayprint import _FormatOptions

if sys.version_info >= (3, 11):
from typing import assert_type
else:
from typing_extensions import assert_type

AR: np.ndarray[Any, Any]
func_float: Callable[[np.floating[Any]], str]
func_int: Callable[[np.integer[Any]], str]

reveal_type(np.get_printoptions()) # E: TypedDict
reveal_type(np.array2string( # E: str
AR, formatter={'float_kind': func_float, 'int_kind': func_int}
))
reveal_type(np.format_float_scientific(1.0)) # E: str
reveal_type(np.format_float_positional(1)) # E: str
reveal_type(np.array_repr(AR)) # E: str
reveal_type(np.array_str(AR)) # E: str
assert_type(np.get_printoptions(), _FormatOptions)
assert_type(
np.array2string(AR, formatter={'float_kind': func_float, 'int_kind': func_int}),
str,
)
assert_type(np.format_float_scientific(1.0), str)
assert_type(np.format_float_positional(1), str)
assert_type(np.array_repr(AR), str)
assert_type(np.array_str(AR), str)

reveal_type(np.printoptions()) # E: contextlib._GeneratorContextManager
assert_type(np.printoptions(), contextlib._GeneratorContextManager[_FormatOptions])
with np.printoptions() as dct:
reveal_type(dct) # E: TypedDict
assert_type(dct, _FormatOptions)
106 changes: 57 additions & 49 deletions 106 numpy/typing/tests/data/reveal/arraysetops.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import sys
from typing import Any

import numpy as np
import numpy.typing as npt

if sys.version_info >= (3, 11):
from typing import assert_type
else:
from typing_extensions import assert_type

AR_b: npt.NDArray[np.bool_]
AR_i8: npt.NDArray[np.int64]
AR_f8: npt.NDArray[np.float64]
Expand All @@ -9,52 +17,52 @@ AR_O: npt.NDArray[np.object_]

AR_LIKE_f8: list[float]

reveal_type(np.ediff1d(AR_b)) # E: ndarray[Any, dtype[{int8}]]
reveal_type(np.ediff1d(AR_i8, to_end=[1, 2, 3])) # E: ndarray[Any, dtype[{int64}]]
reveal_type(np.ediff1d(AR_M)) # E: ndarray[Any, dtype[timedelta64]]
reveal_type(np.ediff1d(AR_O)) # E: ndarray[Any, dtype[object_]]
reveal_type(np.ediff1d(AR_LIKE_f8, to_begin=[1, 1.5])) # E: ndarray[Any, dtype[Any]]

reveal_type(np.intersect1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]]
reveal_type(np.intersect1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[datetime64]]
reveal_type(np.intersect1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]]
reveal_type(np.intersect1d(AR_f8, AR_f8, return_indices=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]

reveal_type(np.setxor1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]]
reveal_type(np.setxor1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[datetime64]]
reveal_type(np.setxor1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]]

reveal_type(np.in1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[bool_]]
reveal_type(np.in1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[bool_]]
reveal_type(np.in1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[bool_]]
reveal_type(np.in1d(AR_f8, AR_LIKE_f8, invert=True)) # E: ndarray[Any, dtype[bool_]]

reveal_type(np.isin(AR_i8, AR_i8)) # E: ndarray[Any, dtype[bool_]]
reveal_type(np.isin(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[bool_]]
reveal_type(np.isin(AR_f8, AR_i8)) # E: ndarray[Any, dtype[bool_]]
reveal_type(np.isin(AR_f8, AR_LIKE_f8, invert=True)) # E: ndarray[Any, dtype[bool_]]

reveal_type(np.union1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]]
reveal_type(np.union1d(AR_M, AR_M)) # E: ndarray[Any, dtype[datetime64]]
reveal_type(np.union1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]]

reveal_type(np.setdiff1d(AR_i8, AR_i8)) # E: ndarray[Any, dtype[{int64}]]
reveal_type(np.setdiff1d(AR_M, AR_M, assume_unique=True)) # E: ndarray[Any, dtype[datetime64]]
reveal_type(np.setdiff1d(AR_f8, AR_i8)) # E: ndarray[Any, dtype[Any]]

reveal_type(np.unique(AR_f8)) # E: ndarray[Any, dtype[{float64}]]
reveal_type(np.unique(AR_LIKE_f8, axis=0)) # E: ndarray[Any, dtype[Any]]
reveal_type(np.unique(AR_f8, return_index=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_LIKE_f8, return_index=True)) # E: tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_f8, return_inverse=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_LIKE_f8, return_inverse=True)) # E: tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_f8, return_counts=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_LIKE_f8, return_counts=True)) # E: tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_f8, return_index=True, return_inverse=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True)) # E: tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_f8, return_index=True, return_counts=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_counts=True)) # E: tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_f8, return_inverse=True, return_counts=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_LIKE_f8, return_inverse=True, return_counts=True)) # E: tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_f8, return_index=True, return_inverse=True, return_counts=True)) # E: tuple[ndarray[Any, dtype[{float64}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
reveal_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True, return_counts=True)) # E: tuple[ndarray[Any, dtype[Any]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]], ndarray[Any, dtype[{intp}]]]
assert_type(np.ediff1d(AR_b), npt.NDArray[np.int8])
assert_type(np.ediff1d(AR_i8, to_end=[1, 2, 3]), npt.NDArray[np.int64])
assert_type(np.ediff1d(AR_M), npt.NDArray[np.timedelta64])
assert_type(np.ediff1d(AR_O), npt.NDArray[np.object_])
assert_type(np.ediff1d(AR_LIKE_f8, to_begin=[1, 1.5]), npt.NDArray[Any])

assert_type(np.intersect1d(AR_i8, AR_i8), npt.NDArray[np.int64])
assert_type(np.intersect1d(AR_M, AR_M, assume_unique=True), npt.NDArray[np.datetime64])
assert_type(np.intersect1d(AR_f8, AR_i8), npt.NDArray[Any])
assert_type(np.intersect1d(AR_f8, AR_f8, return_indices=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp], npt.NDArray[np.intp]])

assert_type(np.setxor1d(AR_i8, AR_i8), npt.NDArray[np.int64])
assert_type(np.setxor1d(AR_M, AR_M, assume_unique=True), npt.NDArray[np.datetime64])
assert_type(np.setxor1d(AR_f8, AR_i8), npt.NDArray[Any])

assert_type(np.in1d(AR_i8, AR_i8), npt.NDArray[np.bool_])
assert_type(np.in1d(AR_M, AR_M, assume_unique=True), npt.NDArray[np.bool_])
assert_type(np.in1d(AR_f8, AR_i8), npt.NDArray[np.bool_])
assert_type(np.in1d(AR_f8, AR_LIKE_f8, invert=True), npt.NDArray[np.bool_])

assert_type(np.isin(AR_i8, AR_i8), npt.NDArray[np.bool_])
assert_type(np.isin(AR_M, AR_M, assume_unique=True), npt.NDArray[np.bool_])
assert_type(np.isin(AR_f8, AR_i8), npt.NDArray[np.bool_])
assert_type(np.isin(AR_f8, AR_LIKE_f8, invert=True), npt.NDArray[np.bool_])

assert_type(np.union1d(AR_i8, AR_i8), npt.NDArray[np.int64])
assert_type(np.union1d(AR_M, AR_M), npt.NDArray[np.datetime64])
assert_type(np.union1d(AR_f8, AR_i8), npt.NDArray[Any])

assert_type(np.setdiff1d(AR_i8, AR_i8), npt.NDArray[np.int64])
assert_type(np.setdiff1d(AR_M, AR_M, assume_unique=True), npt.NDArray[np.datetime64])
assert_type(np.setdiff1d(AR_f8, AR_i8), npt.NDArray[Any])

assert_type(np.unique(AR_f8), npt.NDArray[np.float64])
assert_type(np.unique(AR_LIKE_f8, axis=0), npt.NDArray[Any])
assert_type(np.unique(AR_f8, return_index=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp]])
assert_type(np.unique(AR_LIKE_f8, return_index=True), tuple[npt.NDArray[Any], npt.NDArray[np.intp]])
assert_type(np.unique(AR_f8, return_inverse=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp]])
assert_type(np.unique(AR_LIKE_f8, return_inverse=True), tuple[npt.NDArray[Any], npt.NDArray[np.intp]])
assert_type(np.unique(AR_f8, return_counts=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp]])
assert_type(np.unique(AR_LIKE_f8, return_counts=True), tuple[npt.NDArray[Any], npt.NDArray[np.intp]])
assert_type(np.unique(AR_f8, return_index=True, return_inverse=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp], npt.NDArray[np.intp]])
assert_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True), tuple[npt.NDArray[Any], npt.NDArray[np.intp], npt.NDArray[np.intp]])
assert_type(np.unique(AR_f8, return_index=True, return_counts=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp], npt.NDArray[np.intp]])
assert_type(np.unique(AR_LIKE_f8, return_index=True, return_counts=True), tuple[npt.NDArray[Any], npt.NDArray[np.intp], npt.NDArray[np.intp]])
assert_type(np.unique(AR_f8, return_inverse=True, return_counts=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp], npt.NDArray[np.intp]])
assert_type(np.unique(AR_LIKE_f8, return_inverse=True, return_counts=True), tuple[npt.NDArray[Any], npt.NDArray[np.intp], npt.NDArray[np.intp]])
assert_type(np.unique(AR_f8, return_index=True, return_inverse=True, return_counts=True), tuple[npt.NDArray[np.float64], npt.NDArray[np.intp], npt.NDArray[np.intp], npt.NDArray[np.intp]])
assert_type(np.unique(AR_LIKE_f8, return_index=True, return_inverse=True, return_counts=True), tuple[npt.NDArray[Any], npt.NDArray[np.intp], npt.NDArray[np.intp], npt.NDArray[np.intp]])
37 changes: 23 additions & 14 deletions 37 numpy/typing/tests/data/reveal/arrayterator.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import sys
from typing import Any
from collections.abc import Generator

import numpy as np
import numpy.typing as npt

if sys.version_info >= (3, 11):
from typing import assert_type
else:
from typing_extensions import assert_type

AR_i8: np.ndarray[Any, np.dtype[np.int64]]
ar_iter = np.lib.Arrayterator(AR_i8)

reveal_type(ar_iter.var) # E: ndarray[Any, dtype[{int64}]]
reveal_type(ar_iter.buf_size) # E: Union[None, builtins.int]
reveal_type(ar_iter.start) # E: builtins.list[builtins.int]
reveal_type(ar_iter.stop) # E: builtins.list[builtins.int]
reveal_type(ar_iter.step) # E: builtins.list[builtins.int]
reveal_type(ar_iter.shape) # E: builtins.tuple[builtins.int, ...]
reveal_type(ar_iter.flat) # E: typing.Generator[{int64}, None, None]
assert_type(ar_iter.var, npt.NDArray[np.int64])
assert_type(ar_iter.buf_size, None | int)
assert_type(ar_iter.start, list[int])
assert_type(ar_iter.stop, list[int])
assert_type(ar_iter.step, list[int])
assert_type(ar_iter.shape, tuple[int, ...])
assert_type(ar_iter.flat, Generator[np.int64, None, None])

reveal_type(ar_iter.__array__()) # E: ndarray[Any, dtype[{int64}]]
assert_type(ar_iter.__array__(), npt.NDArray[np.int64])

for i in ar_iter:
reveal_type(i) # E: ndarray[Any, dtype[{int64}]]
assert_type(i, npt.NDArray[np.int64])

reveal_type(ar_iter[0]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]]
reveal_type(ar_iter[...]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]]
reveal_type(ar_iter[:]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]]
reveal_type(ar_iter[0, 0, 0]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]]
reveal_type(ar_iter[..., 0, :]) # E: lib.arrayterator.Arrayterator[Any, dtype[{int64}]]
assert_type(ar_iter[0], np.lib.Arrayterator[Any, np.dtype[np.int64]])
assert_type(ar_iter[...], np.lib.Arrayterator[Any, np.dtype[np.int64]])
assert_type(ar_iter[:], np.lib.Arrayterator[Any, np.dtype[np.int64]])
assert_type(ar_iter[0, 0, 0], np.lib.Arrayterator[Any, np.dtype[np.int64]])
assert_type(ar_iter[..., 0, :], np.lib.Arrayterator[Any, np.dtype[np.int64]])
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.