diff --git a/pyproject.toml b/pyproject.toml index 9ddd22fc..57f35c42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -62,31 +62,32 @@ packages = ["src/numpy-stubs"] [tool.mypy] -files = ["src/**/*.py", "src/**/*.pyi"] +files = ["src/**/*.py", "src/**/*.pyi", "tests/**/*.py", "tests/**/*.pyi"] python_version = "3.10" strict = true + disable_bytearray_promotion = true disable_memoryview_promotion = true enable_error_code = ["ignore-without-code", "truthy-bool"] -disable_error_code = ["explicit-override"] # TODO # basedmypy only warn_unreachable = false -warn_unused_ignores = false +warn_unused_ignores = true disallow_any_explicit = false +# TODO +disable_error_code = ["explicit-override"] [tool.pyright] -include = ["src/numpy-stubs"] +include = ["src/numpy-stubs", "tests"] ignore = [".venv"] stubPath = "src" -pythonVersion = "3.10" pythonPlatform = "All" -typeCheckingMode = "standard" +pythonVersion = "3.10" +typeCheckingMode = "standard" # TODO(jorenham): set to "all" deprecateTypingAliases = true enableReachabilityAnalysis = false enableTypeIgnoreComments = false reportCallInDefaultInitializer = true -reportImplicitOverride = false # TODO reportImportCycles = true reportMissingSuperCall = false reportPrivateUsage = false @@ -104,10 +105,12 @@ reportInvalidCast = true reportPrivateLocalImportUsage = true reportUnannotatedClassAttribute = true strictGenericNarrowing = true +# TODO +reportImplicitOverride = false [tool.ruff] -src = ["src"] +src = ["src", "tests"] extend-exclude = [".git", ".mypy_cache", ".tox", ".venv"] force-exclude = true # https://typing.readthedocs.io/en/latest/guides/writing_stubs.html#maximum-line-length @@ -121,6 +124,7 @@ preview = true [tool.ruff.lint] select = ["ALL"] ignore = [ + "FBT", # flake8-boolean-trap "COM812", # flake8-commas: missing-trailing-comma "CPY", # flake8-copyright "EM", # flake8-errmsg @@ -128,7 +132,6 @@ preview = true "T20", # flake8-print "TC001", # flake8-type-checking: typing-only-first-party-import "TC003", # flake8-type-checking: typing-only-standard-library-import - "TD003", # flake8-todo: Missing issue link for this TODO "FIX", # flake8-fixme ] @@ -140,6 +143,7 @@ preview = true "N", # pep8-naming "D", # pydocstyle "ANN401", # flake8-annotations: any-type + "S", # flake8-bandit "A", # flake8-builtins "DOC", # flake8-docstrings "PYI054", # flake8-pyi: numeric-literal-too-long @@ -153,10 +157,14 @@ preview = true "PLR6301", # pylint/R: no-self-use "PLW3201", # pylint/W: bad-dunder-method-name "FURB", # refurb - # TODO: - "PYI042", - "PYI046", - "PYI047", + # TODO(jorenham): re-enable + "TD", # flake8-todo + ] + "src/numpy-stubs/**/*.pyi" = [ + # TODO(jorenham): re-enable + "PYI042", # flake8-pyi: snake-case-type-alias + "PYI046", # flake8-pyi: unused-private-protocol + "PYI047", # flake8-pyi: unused-private-type-alias ] [tool.ruff.lint.flake8-annotations] diff --git a/src/numpy-stubs/f2py/__init__.pyi b/src/numpy-stubs/f2py/__init__.pyi index ce59c518..6f416d49 100644 --- a/src/numpy-stubs/f2py/__init__.pyi +++ b/src/numpy-stubs/f2py/__init__.pyi @@ -15,7 +15,13 @@ class _F2PyDict(_F2PyDictBase, total=False): fsrc: list[str] ltx: list[str] +# +def get_include() -> str: ... + +# def run_main(comline_list: Iterable[str]) -> dict[str, _F2PyDict]: ... + +# @overload def compile( source: str | bytes, @@ -37,4 +43,3 @@ def compile( *, full_output: L[True], ) -> subprocess.CompletedProcess[bytes]: ... -def get_include() -> str: ... diff --git a/src/numpy-stubs/polynomial/__init__.pyi b/src/numpy-stubs/polynomial/__init__.pyi index ed5b2936..6de8c2f6 100644 --- a/src/numpy-stubs/polynomial/__init__.pyi +++ b/src/numpy-stubs/polynomial/__init__.pyi @@ -1,5 +1,7 @@ from typing import Final, Literal +from numpy._pytesttester import PytestTester as _PytestTester + from . import chebyshev, hermite, hermite_e, laguerre, legendre, polynomial from .chebyshev import Chebyshev from .hermite import Hermite @@ -26,6 +28,4 @@ __all__ = [ def set_default_printstyle(style: Literal["ascii", "unicode"]) -> None: ... -from numpy._pytesttester import PytestTester as _PytestTester - -test: Final[_PytestTester] +test: Final[_PytestTester] = ... diff --git a/src/numpy-stubs/polynomial/_polybase.pyi b/src/numpy-stubs/polynomial/_polybase.pyi index edd0f7a4..b37fdc99 100644 --- a/src/numpy-stubs/polynomial/_polybase.pyi +++ b/src/numpy-stubs/polynomial/_polybase.pyi @@ -1,28 +1,11 @@ import abc -import decimal -import numbers from collections.abc import Iterator, Mapping, Sequence -from typing import ( - Any, - ClassVar, - Final, - Generic, - Literal, - SupportsIndex, - TypeAlias, - TypeGuard, - overload, -) -from typing_extensions import LiteralString, Self, TypeVar +from typing import Any, ClassVar, Final, Generic, Literal, SupportsIndex, TypeAlias, overload +from typing_extensions import LiteralString, Self, TypeIs, TypeVar import numpy as np import numpy.typing as npt -from numpy._typing import ( - _ArrayLikeComplex_co, - _ArrayLikeFloat_co, - _FloatLike_co, - _NumberLike_co, -) +from numpy._typing import _ArrayLikeNumber_co, _FloatLike_co, _NumberLike_co from ._polytypes import ( _AnyInt, @@ -39,15 +22,15 @@ from ._polytypes import ( __all__: Final[Sequence[str]] = ("ABCPolyBase",) -_NameCo = TypeVar("_NameCo", bound=LiteralString | None, covariant=True, default=LiteralString | None) +_NameT_co = TypeVar("_NameT_co", bound=LiteralString | None, default=LiteralString | None, covariant=True) _Self = TypeVar("_Self") -_Other = TypeVar("_Other", bound=ABCPolyBase) +_PolyT = TypeVar("_PolyT", bound=ABCPolyBase) _AnyOther: TypeAlias = ABCPolyBase | _CoefLike_co | _SeriesLikeCoef_co _Hundred: TypeAlias = Literal[100] -class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta): - __hash__: ClassVar[None] # type: ignore[assignment] +class ABCPolyBase(Generic[_NameT_co], metaclass=abc.ABCMeta): + __hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride] __array_ufunc__: ClassVar[None] maxpower: ClassVar[_Hundred] @@ -55,10 +38,10 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta): _subscript_mapping: ClassVar[Mapping[int, str]] _use_unicode: ClassVar[bool] - basis_name: _NameCo + basis_name: _NameT_co coef: _CoefSeries - domain: _Array2[np.inexact[Any] | np.object_] - window: _Array2[np.inexact[Any] | np.object_] + domain: _Array2[np.inexact] + window: _Array2[np.inexact] _symbol: LiteralString @property @@ -72,107 +55,82 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta): symbol: str = ..., ) -> None: ... @overload - def __call__(self, /, arg: _Other) -> _Other: ... + def __call__(self, /, arg: _PolyT) -> _PolyT: ... # TODO: Once `_ShapeType@ndarray` is covariant and bounded (see #26081), # additionally include 0-d arrays as input types with scalar return type. @overload - def __call__( - self, - /, - arg: _FloatLike_co | decimal.Decimal | numbers.Real | np.object_, - ) -> np.float64 | np.complex128: ... - @overload - def __call__( - self, - /, - arg: _NumberLike_co | numbers.Complex, - ) -> np.complex128: ... + def __call__(self, /, arg: _NumberLike_co) -> np.float64 | np.complex128: ... @overload - def __call__( - self, /, arg: _ArrayLikeFloat_co - ) -> npt.NDArray[np.float64] | npt.NDArray[np.complex128] | npt.NDArray[np.object_]: ... + def __call__(self, /, arg: _ArrayLikeNumber_co) -> npt.NDArray[np.float64 | np.complex128]: ... @overload - def __call__( - self, - /, - arg: _ArrayLikeComplex_co, - ) -> npt.NDArray[np.complex128] | npt.NDArray[np.object_]: ... - @overload - def __call__( - self, - /, - arg: _ArrayLikeCoefObject_co, - ) -> npt.NDArray[np.object_]: ... + def __call__(self, /, arg: _ArrayLikeCoefObject_co) -> npt.NDArray[np.object_]: ... def __format__(self, fmt_str: str, /) -> str: ... def __eq__(self, x: object, /) -> bool: ... def __ne__(self, x: object, /) -> bool: ... def __neg__(self, /) -> Self: ... def __pos__(self, /) -> Self: ... - def __add__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __sub__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __mul__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __truediv__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __floordiv__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __mod__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __divmod__(self: _Self, x: _AnyOther, /) -> _Tuple2[_Self]: ... - def __pow__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __radd__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __rsub__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __rmul__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __rtruediv__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __rfloordiv__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __rmod__(self: _Self, x: _AnyOther, /) -> _Self: ... - def __rdivmod__(self: _Self, x: _AnyOther, /) -> _Tuple2[_Self]: ... + def __add__(self, x: _AnyOther, /) -> Self: ... + def __sub__(self, x: _AnyOther, /) -> Self: ... + def __mul__(self, x: _AnyOther, /) -> Self: ... + def __truediv__(self, x: _AnyOther, /) -> Self: ... + def __floordiv__(self, x: _AnyOther, /) -> Self: ... + def __mod__(self, x: _AnyOther, /) -> Self: ... + def __divmod__(self, x: _AnyOther, /) -> _Tuple2[_Self]: ... + def __pow__(self, x: _AnyOther, /) -> Self: ... + def __radd__(self, x: _AnyOther, /) -> Self: ... + def __rsub__(self, x: _AnyOther, /) -> Self: ... + def __rmul__(self, x: _AnyOther, /) -> Self: ... + def __rtruediv__(self, x: _AnyOther, /) -> Self: ... + def __rfloordiv__(self, x: _AnyOther, /) -> Self: ... + def __rmod__(self, x: _AnyOther, /) -> Self: ... + def __rdivmod__(self, x: _AnyOther, /) -> _Tuple2[_Self]: ... def __len__(self, /) -> int: ... - def __iter__(self, /) -> Iterator[np.inexact[Any] | object]: ... + def __iter__(self, /) -> Iterator[np.inexact | object]: ... def __getstate__(self, /) -> dict[str, Any]: ... def __setstate__(self, dict: dict[str, Any], /) -> None: ... def has_samecoef(self, /, other: ABCPolyBase) -> bool: ... def has_samedomain(self, /, other: ABCPolyBase) -> bool: ... def has_samewindow(self, /, other: ABCPolyBase) -> bool: ... - @overload - def has_sametype(self: _Self, /, other: ABCPolyBase) -> TypeGuard[_Self]: ... - @overload - def has_sametype(self, /, other: object) -> Literal[False]: ... + def has_sametype(self, /, other: object) -> TypeIs[Self]: ... def copy(self, /) -> Self: ... def degree(self, /) -> int: ... def cutdeg(self, /) -> Self: ... - def trim(self: _Self, /, tol: _FloatLike_co = ...) -> _Self: ... - def truncate(self: _Self, /, size: _AnyInt) -> _Self: ... + def trim(self, /, tol: _FloatLike_co = ...) -> Self: ... + def truncate(self, /, size: _AnyInt) -> Self: ... @overload def convert( self, - domain: _SeriesLikeCoef_co | None, - kind: type[_Other], /, + domain: _SeriesLikeCoef_co | None, + kind: type[_PolyT], window: _SeriesLikeCoef_co | None = ..., - ) -> _Other: ... + ) -> _PolyT: ... @overload def convert( self, /, domain: _SeriesLikeCoef_co | None = ..., *, - kind: type[_Other], + kind: type[_PolyT], window: _SeriesLikeCoef_co | None = ..., - ) -> _Other: ... + ) -> _PolyT: ... @overload def convert( - self: _Self, + self, /, domain: _SeriesLikeCoef_co | None = ..., - kind: type[_Self] | None = ..., + kind: None = ..., window: _SeriesLikeCoef_co | None = ..., - ) -> _Self: ... + ) -> Self: ... def mapparms(self, /) -> _Tuple2[Any]: ... def integ( - self: _Self, + self, /, m: SupportsIndex = ..., k: _CoefLike_co | _SeriesLikeCoef_co = ..., lbnd: _CoefLike_co | None = ..., - ) -> _Self: ... - def deriv(self: _Self, /, m: SupportsIndex = ...) -> _Self: ... + ) -> Self: ... + def deriv(self, /, m: SupportsIndex = ...) -> Self: ... def roots(self, /) -> _CoefSeries: ... def linspace( self, @@ -183,8 +141,7 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta): @overload @classmethod def fit( - cls: type[_Self], - /, + cls, x: _SeriesLikeCoef_co, y: _SeriesLikeCoef_co, deg: int | _SeriesLikeInt_co, @@ -194,12 +151,11 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta): w: _SeriesLikeCoef_co | None = ..., window: _SeriesLikeCoef_co | None = ..., symbol: str = ..., - ) -> _Self: ... + ) -> Self: ... @overload @classmethod def fit( - cls: type[_Self], - /, + cls, x: _SeriesLikeCoef_co, y: _SeriesLikeCoef_co, deg: int | _SeriesLikeInt_co, @@ -210,56 +166,51 @@ class ABCPolyBase(Generic[_NameCo], metaclass=abc.ABCMeta): w: _SeriesLikeCoef_co | None = ..., window: _SeriesLikeCoef_co | None = ..., symbol: str = ..., - ) -> tuple[_Self, Sequence[np.inexact[Any] | np.int32]]: ... + ) -> tuple[_Self, Sequence[np.inexact | np.int32]]: ... @overload @classmethod def fit( - cls: type[_Self], + cls, x: _SeriesLikeCoef_co, y: _SeriesLikeCoef_co, deg: int | _SeriesLikeInt_co, domain: _SeriesLikeCoef_co | None, rcond: _FloatLike_co, full: Literal[True], - /, w: _SeriesLikeCoef_co | None = ..., window: _SeriesLikeCoef_co | None = ..., symbol: str = ..., - ) -> tuple[_Self, Sequence[np.inexact[Any] | np.int32]]: ... + ) -> tuple[_Self, Sequence[np.inexact | np.int32]]: ... @classmethod def fromroots( - cls: type[_Self], - /, + cls, roots: _ArrayLikeCoef_co, domain: _SeriesLikeCoef_co | None = ..., window: _SeriesLikeCoef_co | None = ..., symbol: str = ..., - ) -> _Self: ... + ) -> Self: ... @classmethod def identity( - cls: type[_Self], - /, + cls, domain: _SeriesLikeCoef_co | None = ..., window: _SeriesLikeCoef_co | None = ..., symbol: str = ..., - ) -> _Self: ... + ) -> Self: ... @classmethod def basis( - cls: type[_Self], - /, + cls, deg: _AnyInt, domain: _SeriesLikeCoef_co | None = ..., window: _SeriesLikeCoef_co | None = ..., symbol: str = ..., - ) -> _Self: ... + ) -> Self: ... @classmethod def cast( - cls: type[_Self], - /, + cls, series: ABCPolyBase, domain: _SeriesLikeCoef_co | None = ..., window: _SeriesLikeCoef_co | None = ..., - ) -> _Self: ... + ) -> Self: ... @classmethod def _str_term_unicode(cls, /, i: str, arg_str: str) -> str: ... @staticmethod diff --git a/src/numpy-stubs/polynomial/_polytypes.pyi b/src/numpy-stubs/polynomial/_polytypes.pyi index f86e3840..727fdb3d 100644 --- a/src/numpy-stubs/polynomial/_polytypes.pyi +++ b/src/numpy-stubs/polynomial/_polytypes.pyi @@ -30,9 +30,8 @@ from numpy._typing import ( ) _T = TypeVar("_T") -_T_contra = TypeVar("_T_contra", contravariant=True) -_Self = TypeVar("_Self") -_SCT = TypeVar("_SCT", bound=np.number[Any] | np.bool | np.object_) +_T_contra = TypeVar("_T_contra", contravariant=True, default=Any) +_SCT = TypeVar("_SCT", bound=np.bool | np.number | np.object_) # compatible with e.g. int, float, complex, Decimal, Fraction, and ABCPolyBase @type_check_only @@ -41,25 +40,25 @@ class _SupportsCoefOps(Protocol[_T_contra]): def __ne__(self, x: object, /) -> bool: ... def __neg__(self, /) -> Self: ... def __pos__(self, /) -> Self: ... - def __add__(self: _Self, x: _T_contra, /) -> _Self: ... - def __sub__(self: _Self, x: _T_contra, /) -> _Self: ... - def __mul__(self: _Self, x: _T_contra, /) -> _Self: ... - def __pow__(self: _Self, x: _T_contra, /) -> _Self | float: ... - def __radd__(self: _Self, x: _T_contra, /) -> _Self: ... - def __rsub__(self: _Self, x: _T_contra, /) -> _Self: ... - def __rmul__(self: _Self, x: _T_contra, /) -> _Self: ... + def __add__(self, x: _T_contra, /) -> Self: ... + def __sub__(self, x: _T_contra, /) -> Self: ... + def __mul__(self, x: _T_contra, /) -> Self: ... + def __pow__(self, x: _T_contra, /) -> Self | float: ... + def __radd__(self, x: _T_contra, /) -> Self: ... + def __rsub__(self, x: _T_contra, /) -> Self: ... + def __rmul__(self, x: _T_contra, /) -> Self: ... _Series: TypeAlias = np.ndarray[tuple[int], np.dtype[_SCT]] -_FloatSeries: TypeAlias = _Series[np.floating[Any]] -_ComplexSeries: TypeAlias = _Series[np.complexfloating[Any, Any]] +_FloatSeries: TypeAlias = _Series[np.floating] +_ComplexSeries: TypeAlias = _Series[np.complexfloating] _ObjectSeries: TypeAlias = _Series[np.object_] -_CoefSeries: TypeAlias = _Series[np.inexact[Any] | np.object_] +_CoefSeries: TypeAlias = _Series[np.inexact | np.object_] -_FloatArray: TypeAlias = npt.NDArray[np.floating[Any]] -_ComplexArray: TypeAlias = npt.NDArray[np.complexfloating[Any, Any]] +_FloatArray: TypeAlias = npt.NDArray[np.floating] +_ComplexArray: TypeAlias = npt.NDArray[np.complexfloating] _ObjectArray: TypeAlias = npt.NDArray[np.object_] -_CoefArray: TypeAlias = npt.NDArray[np.inexact[Any] | np.object_] +_CoefArray: TypeAlias = npt.NDArray[np.inexact | np.object_] _Tuple2: TypeAlias = tuple[_T, _T] _Array1: TypeAlias = np.ndarray[tuple[Literal[1]], np.dtype[_SCT]] @@ -67,33 +66,31 @@ _Array2: TypeAlias = np.ndarray[tuple[Literal[2]], np.dtype[_SCT]] _AnyInt: TypeAlias = SupportsInt | SupportsIndex -_CoefObjectLike_co: TypeAlias = np.object_ | _SupportsCoefOps[Any] +_CoefObjectLike_co: TypeAlias = np.object_ | _SupportsCoefOps _CoefLike_co: TypeAlias = _NumberLike_co | _CoefObjectLike_co # The term "series" is used here to refer to 1-d arrays of numeric scalars. _SeriesLikeBool_co: TypeAlias = _SupportsArray[np.dtype[np.bool]] | Sequence[bool | np.bool] -_SeriesLikeInt_co: TypeAlias = _SupportsArray[np.dtype[np.integer[Any] | np.bool]] | Sequence[_IntLike_co] -_SeriesLikeFloat_co: TypeAlias = _SupportsArray[np.dtype[np.floating[Any] | np.integer[Any] | np.bool]] | Sequence[_FloatLike_co] -_SeriesLikeComplex_co: TypeAlias = ( - _SupportsArray[np.dtype[np.inexact[Any] | np.integer[Any] | np.bool]] | Sequence[_ComplexLike_co] -) +_SeriesLikeInt_co: TypeAlias = _SupportsArray[np.dtype[np.integer | np.bool]] | Sequence[_IntLike_co] +_SeriesLikeFloat_co: TypeAlias = _SupportsArray[np.dtype[np.floating | np.integer | np.bool]] | Sequence[_FloatLike_co] +_SeriesLikeComplex_co: TypeAlias = _SupportsArray[np.dtype[np.inexact | np.integer | np.bool]] | Sequence[_ComplexLike_co] _SeriesLikeObject_co: TypeAlias = _SupportsArray[np.dtype[np.object_]] | Sequence[_CoefObjectLike_co] -_SeriesLikeCoef_co: TypeAlias = _SupportsArray[np.dtype[np.number[Any] | np.bool | np.object_]] | Sequence[_CoefLike_co] +_SeriesLikeCoef_co: TypeAlias = _SupportsArray[np.dtype[np.number | np.bool | np.object_]] | Sequence[_CoefLike_co] _ArrayLikeCoefObject_co: TypeAlias = _CoefObjectLike_co | _SeriesLikeObject_co | _NestedSequence[_SeriesLikeObject_co] -_ArrayLikeCoef_co: TypeAlias = npt.NDArray[np.number[Any] | np.bool | np.object_] | _ArrayLikeNumber_co | _ArrayLikeCoefObject_co +_ArrayLikeCoef_co: TypeAlias = npt.NDArray[np.number | np.bool | np.object_] | _ArrayLikeNumber_co | _ArrayLikeCoefObject_co -_Name_co = TypeVar("_Name_co", bound=LiteralString, covariant=True, default=LiteralString) +_NameT_co = TypeVar("_NameT_co", bound=LiteralString, default=LiteralString, covariant=True) @type_check_only -class _Named(Protocol[_Name_co]): +class _Named(Protocol[_NameT_co]): @property - def __name__(self, /) -> _Name_co: ... + def __name__(self, /) -> _NameT_co: ... _Line: TypeAlias = np.ndarray[tuple[Literal[1, 2]], np.dtype[_SCT]] @type_check_only -class _FuncLine(_Named[_Name_co], Protocol[_Name_co]): +class _FuncLine(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__(self, /, off: _SCT, scl: _SCT) -> _Line[_SCT]: ... @overload @@ -101,22 +98,12 @@ class _FuncLine(_Named[_Name_co], Protocol[_Name_co]): @overload def __call__(self, /, off: float, scl: float) -> _Line[np.float64]: ... @overload - def __call__( - self, - /, - off: complex, - scl: complex, - ) -> _Line[np.complex128]: ... + def __call__(self, /, off: complex, scl: complex) -> _Line[np.complex128]: ... @overload - def __call__( - self, - /, - off: _SupportsCoefOps[Any], - scl: _SupportsCoefOps[Any], - ) -> _Line[np.object_]: ... + def __call__(self, /, off: _SupportsCoefOps, scl: _SupportsCoefOps) -> _Line[np.object_]: ... @type_check_only -class _FuncFromRoots(_Named[_Name_co], Protocol[_Name_co]): +class _FuncFromRoots(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__(self, /, roots: _SeriesLikeFloat_co) -> _FloatSeries: ... @overload @@ -125,38 +112,18 @@ class _FuncFromRoots(_Named[_Name_co], Protocol[_Name_co]): def __call__(self, /, roots: _SeriesLikeCoef_co) -> _ObjectSeries: ... @type_check_only -class _FuncBinOp(_Named[_Name_co], Protocol[_Name_co]): +class _FuncBinOp(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - c1: _SeriesLikeBool_co, - c2: _SeriesLikeBool_co, - ) -> NoReturn: ... + def __call__(self, /, c1: _SeriesLikeBool_co, c2: _SeriesLikeBool_co) -> NoReturn: ... @overload - def __call__( - self, - /, - c1: _SeriesLikeFloat_co, - c2: _SeriesLikeFloat_co, - ) -> _FloatSeries: ... + def __call__(self, /, c1: _SeriesLikeFloat_co, c2: _SeriesLikeFloat_co) -> _FloatSeries: ... @overload - def __call__( - self, - /, - c1: _SeriesLikeComplex_co, - c2: _SeriesLikeComplex_co, - ) -> _ComplexSeries: ... + def __call__(self, /, c1: _SeriesLikeComplex_co, c2: _SeriesLikeComplex_co) -> _ComplexSeries: ... @overload - def __call__( - self, - /, - c1: _SeriesLikeCoef_co, - c2: _SeriesLikeCoef_co, - ) -> _ObjectSeries: ... + def __call__(self, /, c1: _SeriesLikeCoef_co, c2: _SeriesLikeCoef_co) -> _ObjectSeries: ... @type_check_only -class _FuncUnOp(_Named[_Name_co], Protocol[_Name_co]): +class _FuncUnOp(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__(self, /, c: _SeriesLikeFloat_co) -> _FloatSeries: ... @overload @@ -165,7 +132,7 @@ class _FuncUnOp(_Named[_Name_co], Protocol[_Name_co]): def __call__(self, /, c: _SeriesLikeCoef_co) -> _ObjectSeries: ... @type_check_only -class _FuncPoly2Ortho(_Named[_Name_co], Protocol[_Name_co]): +class _FuncPoly2Ortho(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__(self, /, pol: _SeriesLikeFloat_co) -> _FloatSeries: ... @overload @@ -174,34 +141,16 @@ class _FuncPoly2Ortho(_Named[_Name_co], Protocol[_Name_co]): def __call__(self, /, pol: _SeriesLikeCoef_co) -> _ObjectSeries: ... @type_check_only -class _FuncPow(_Named[_Name_co], Protocol[_Name_co]): +class _FuncPow(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - c: _SeriesLikeFloat_co, - pow: _IntLike_co, - maxpower: _IntLike_co | None = ..., - ) -> _FloatSeries: ... + def __call__(self, /, c: _SeriesLikeFloat_co, pow: _IntLike_co, maxpower: _IntLike_co | None = ...) -> _FloatSeries: ... @overload - def __call__( - self, - /, - c: _SeriesLikeComplex_co, - pow: _IntLike_co, - maxpower: _IntLike_co | None = ..., - ) -> _ComplexSeries: ... + def __call__(self, /, c: _SeriesLikeComplex_co, pow: _IntLike_co, maxpower: _IntLike_co | None = ...) -> _ComplexSeries: ... @overload - def __call__( - self, - /, - c: _SeriesLikeCoef_co, - pow: _IntLike_co, - maxpower: _IntLike_co | None = ..., - ) -> _ObjectSeries: ... + def __call__(self, /, c: _SeriesLikeCoef_co, pow: _IntLike_co, maxpower: _IntLike_co | None = ...) -> _ObjectSeries: ... @type_check_only -class _FuncDer(_Named[_Name_co], Protocol[_Name_co]): +class _FuncDer(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__( self, @@ -231,7 +180,7 @@ class _FuncDer(_Named[_Name_co], Protocol[_Name_co]): ) -> _ObjectArray: ... @type_check_only -class _FuncInteg(_Named[_Name_co], Protocol[_Name_co]): +class _FuncInteg(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__( self, @@ -267,31 +216,13 @@ class _FuncInteg(_Named[_Name_co], Protocol[_Name_co]): ) -> _ObjectArray: ... @type_check_only -class _FuncValFromRoots(_Named[_Name_co], Protocol[_Name_co]): +class _FuncValFromRoots(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - x: _FloatLike_co, - r: _FloatLike_co, - tensor: bool = ..., - ) -> np.floating[Any]: ... + def __call__(self, /, x: _FloatLike_co, r: _FloatLike_co, tensor: bool = ...) -> np.floating: ... @overload - def __call__( - self, - /, - x: _NumberLike_co, - r: _NumberLike_co, - tensor: bool = ..., - ) -> np.complexfloating[Any, Any]: ... + def __call__(self, /, x: _NumberLike_co, r: _NumberLike_co, tensor: bool = ...) -> np.complexfloating: ... @overload - def __call__( - self, - /, - x: _FloatLike_co | _ArrayLikeFloat_co, - r: _ArrayLikeFloat_co, - tensor: bool = ..., - ) -> _FloatArray: ... + def __call__(self, /, x: _FloatLike_co | _ArrayLikeFloat_co, r: _ArrayLikeFloat_co, tensor: bool = ...) -> _FloatArray: ... @overload def __call__( self, @@ -301,128 +232,44 @@ class _FuncValFromRoots(_Named[_Name_co], Protocol[_Name_co]): tensor: bool = ..., ) -> _ComplexArray: ... @overload - def __call__( - self, - /, - x: _CoefLike_co | _ArrayLikeCoef_co, - r: _ArrayLikeCoef_co, - tensor: bool = ..., - ) -> _ObjectArray: ... + def __call__(self, /, x: _CoefLike_co | _ArrayLikeCoef_co, r: _ArrayLikeCoef_co, tensor: bool = ...) -> _ObjectArray: ... @overload - def __call__( - self, - /, - x: _CoefLike_co, - r: _CoefLike_co, - tensor: bool = ..., - ) -> _SupportsCoefOps[Any]: ... + def __call__(self, /, x: _CoefLike_co, r: _CoefLike_co, tensor: bool = ...) -> _SupportsCoefOps: ... @type_check_only -class _FuncVal(_Named[_Name_co], Protocol[_Name_co]): +class _FuncVal(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - x: _FloatLike_co, - c: _SeriesLikeFloat_co, - tensor: bool = ..., - ) -> np.floating[Any]: ... + def __call__(self, /, x: _FloatLike_co, c: _SeriesLikeFloat_co, tensor: bool = ...) -> np.floating: ... @overload - def __call__( - self, - /, - x: _NumberLike_co, - c: _SeriesLikeComplex_co, - tensor: bool = ..., - ) -> np.complexfloating[Any, Any]: ... + def __call__(self, /, x: _NumberLike_co, c: _SeriesLikeComplex_co, tensor: bool = ...) -> np.complexfloating: ... @overload - def __call__( - self, - /, - x: _ArrayLikeFloat_co, - c: _ArrayLikeFloat_co, - tensor: bool = ..., - ) -> _FloatArray: ... + def __call__(self, /, x: _ArrayLikeFloat_co, c: _ArrayLikeFloat_co, tensor: bool = ...) -> _FloatArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeComplex_co, - c: _ArrayLikeComplex_co, - tensor: bool = ..., - ) -> _ComplexArray: ... + def __call__(self, /, x: _ArrayLikeComplex_co, c: _ArrayLikeComplex_co, tensor: bool = ...) -> _ComplexArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeCoef_co, - c: _ArrayLikeCoef_co, - tensor: bool = ..., - ) -> _ObjectArray: ... + def __call__(self, /, x: _ArrayLikeCoef_co, c: _ArrayLikeCoef_co, tensor: bool = ...) -> _ObjectArray: ... @overload - def __call__( - self, - /, - x: _CoefLike_co, - c: _SeriesLikeObject_co, - tensor: bool = ..., - ) -> _SupportsCoefOps[Any]: ... + def __call__(self, /, x: _CoefLike_co, c: _SeriesLikeObject_co, tensor: bool = ...) -> _SupportsCoefOps: ... @type_check_only -class _FuncVal2D(_Named[_Name_co], Protocol[_Name_co]): +class _FuncVal2D(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - x: _FloatLike_co, - y: _FloatLike_co, - c: _SeriesLikeFloat_co, - ) -> np.floating[Any]: ... + def __call__(self, /, x: _FloatLike_co, y: _FloatLike_co, c: _SeriesLikeFloat_co) -> np.floating: ... @overload - def __call__( - self, - /, - x: _NumberLike_co, - y: _NumberLike_co, - c: _SeriesLikeComplex_co, - ) -> np.complexfloating[Any, Any]: ... + def __call__(self, /, x: _NumberLike_co, y: _NumberLike_co, c: _SeriesLikeComplex_co) -> np.complexfloating: ... @overload - def __call__( - self, - /, - x: _ArrayLikeFloat_co, - y: _ArrayLikeFloat_co, - c: _ArrayLikeFloat_co, - ) -> _FloatArray: ... + def __call__(self, /, x: _ArrayLikeFloat_co, y: _ArrayLikeFloat_co, c: _ArrayLikeFloat_co) -> _FloatArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeComplex_co, - y: _ArrayLikeComplex_co, - c: _ArrayLikeComplex_co, - ) -> _ComplexArray: ... + def __call__(self, /, x: _ArrayLikeComplex_co, y: _ArrayLikeComplex_co, c: _ArrayLikeComplex_co) -> _ComplexArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeCoef_co, - y: _ArrayLikeCoef_co, - c: _ArrayLikeCoef_co, - ) -> _ObjectArray: ... + def __call__(self, /, x: _ArrayLikeCoef_co, y: _ArrayLikeCoef_co, c: _ArrayLikeCoef_co) -> _ObjectArray: ... @overload - def __call__( - self, - /, - x: _CoefLike_co, - y: _CoefLike_co, - c: _SeriesLikeCoef_co, - ) -> _SupportsCoefOps[Any]: ... + def __call__(self, /, x: _CoefLike_co, y: _CoefLike_co, c: _SeriesLikeCoef_co) -> _SupportsCoefOps: ... @type_check_only -class _FuncVal3D(_Named[_Name_co], Protocol[_Name_co]): +class _FuncVal3D(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__(self, /, x: _FloatLike_co, y: _FloatLike_co, z: _FloatLike_co, c: _SeriesLikeFloat_co) -> np.floating[Any]: ... + def __call__(self, /, x: _FloatLike_co, y: _FloatLike_co, z: _FloatLike_co, c: _SeriesLikeFloat_co) -> np.floating: ... @overload def __call__( self, @@ -431,7 +278,7 @@ class _FuncVal3D(_Named[_Name_co], Protocol[_Name_co]): y: _NumberLike_co, z: _NumberLike_co, c: _SeriesLikeComplex_co, - ) -> np.complexfloating[Any, Any]: ... + ) -> np.complexfloating: ... @overload def __call__( self, @@ -467,134 +314,51 @@ class _FuncVal3D(_Named[_Name_co], Protocol[_Name_co]): y: _CoefLike_co, z: _CoefLike_co, c: _SeriesLikeCoef_co, - ) -> _SupportsCoefOps[Any]: ... + ) -> _SupportsCoefOps: ... -_AnyValF: TypeAlias = Callable[ - [npt.ArrayLike, npt.ArrayLike, bool], - _CoefArray, -] +_AnyValF: TypeAlias = Callable[[npt.ArrayLike, npt.ArrayLike, bool], _CoefArray] @type_check_only -class _FuncValND(_Named[_Name_co], Protocol[_Name_co]): +class _FuncValND(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - val_f: _AnyValF, - c: _SeriesLikeFloat_co, - /, - *args: _FloatLike_co, - ) -> np.floating[Any]: ... + def __call__(self, val_f: _AnyValF, c: _SeriesLikeFloat_co, /, *args: _FloatLike_co) -> np.floating: ... @overload - def __call__( - self, - val_f: _AnyValF, - c: _SeriesLikeComplex_co, - /, - *args: _NumberLike_co, - ) -> np.complexfloating[Any, Any]: ... + def __call__(self, val_f: _AnyValF, c: _SeriesLikeComplex_co, /, *args: _NumberLike_co) -> np.complexfloating: ... @overload - def __call__( - self, - val_f: _AnyValF, - c: _ArrayLikeFloat_co, - /, - *args: _ArrayLikeFloat_co, - ) -> _FloatArray: ... + def __call__(self, val_f: _AnyValF, c: _ArrayLikeFloat_co, /, *args: _ArrayLikeFloat_co) -> _FloatArray: ... @overload - def __call__( - self, - val_f: _AnyValF, - c: _ArrayLikeComplex_co, - /, - *args: _ArrayLikeComplex_co, - ) -> _ComplexArray: ... + def __call__(self, val_f: _AnyValF, c: _ArrayLikeComplex_co, /, *args: _ArrayLikeComplex_co) -> _ComplexArray: ... @overload - def __call__( - self, - val_f: _AnyValF, - c: _SeriesLikeObject_co, - /, - *args: _CoefObjectLike_co, - ) -> _SupportsCoefOps[Any]: ... + def __call__(self, val_f: _AnyValF, c: _SeriesLikeObject_co, /, *args: _CoefObjectLike_co) -> _SupportsCoefOps: ... @overload - def __call__( - self, - val_f: _AnyValF, - c: _ArrayLikeCoef_co, - /, - *args: _ArrayLikeCoef_co, - ) -> _ObjectArray: ... + def __call__(self, val_f: _AnyValF, c: _ArrayLikeCoef_co, /, *args: _ArrayLikeCoef_co) -> _ObjectArray: ... @type_check_only -class _FuncVander(_Named[_Name_co], Protocol[_Name_co]): +class _FuncVander(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - x: _ArrayLikeFloat_co, - deg: SupportsIndex, - ) -> _FloatArray: ... + def __call__(self, /, x: _ArrayLikeFloat_co, deg: SupportsIndex) -> _FloatArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeComplex_co, - deg: SupportsIndex, - ) -> _ComplexArray: ... + def __call__(self, /, x: _ArrayLikeComplex_co, deg: SupportsIndex) -> _ComplexArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeCoef_co, - deg: SupportsIndex, - ) -> _ObjectArray: ... + def __call__(self, /, x: _ArrayLikeCoef_co, deg: SupportsIndex) -> _ObjectArray: ... @overload - def __call__( - self, - /, - x: npt.ArrayLike, - deg: SupportsIndex, - ) -> _CoefArray: ... + def __call__(self, /, x: npt.ArrayLike, deg: SupportsIndex) -> _CoefArray: ... _AnyDegrees: TypeAlias = Sequence[SupportsIndex] @type_check_only -class _FuncVander2D(_Named[_Name_co], Protocol[_Name_co]): +class _FuncVander2D(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - x: _ArrayLikeFloat_co, - y: _ArrayLikeFloat_co, - deg: _AnyDegrees, - ) -> _FloatArray: ... + def __call__(self, /, x: _ArrayLikeFloat_co, y: _ArrayLikeFloat_co, deg: _AnyDegrees) -> _FloatArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeComplex_co, - y: _ArrayLikeComplex_co, - deg: _AnyDegrees, - ) -> _ComplexArray: ... + def __call__(self, /, x: _ArrayLikeComplex_co, y: _ArrayLikeComplex_co, deg: _AnyDegrees) -> _ComplexArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeCoef_co, - y: _ArrayLikeCoef_co, - deg: _AnyDegrees, - ) -> _ObjectArray: ... + def __call__(self, /, x: _ArrayLikeCoef_co, y: _ArrayLikeCoef_co, deg: _AnyDegrees) -> _ObjectArray: ... @overload - def __call__( - self, - /, - x: npt.ArrayLike, - y: npt.ArrayLike, - deg: _AnyDegrees, - ) -> _CoefArray: ... + def __call__(self, /, x: npt.ArrayLike, y: npt.ArrayLike, deg: _AnyDegrees) -> _CoefArray: ... @type_check_only -class _FuncVander3D(_Named[_Name_co], Protocol[_Name_co]): +class _FuncVander3D(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__( self, @@ -614,32 +378,15 @@ class _FuncVander3D(_Named[_Name_co], Protocol[_Name_co]): deg: _AnyDegrees, ) -> _ComplexArray: ... @overload - def __call__( - self, - /, - x: _ArrayLikeCoef_co, - y: _ArrayLikeCoef_co, - z: _ArrayLikeCoef_co, - deg: _AnyDegrees, - ) -> _ObjectArray: ... + def __call__(self, /, x: _ArrayLikeCoef_co, y: _ArrayLikeCoef_co, z: _ArrayLikeCoef_co, deg: _AnyDegrees) -> _ObjectArray: ... @overload - def __call__( - self, - /, - x: npt.ArrayLike, - y: npt.ArrayLike, - z: npt.ArrayLike, - deg: _AnyDegrees, - ) -> _CoefArray: ... + def __call__(self, /, x: npt.ArrayLike, y: npt.ArrayLike, z: npt.ArrayLike, deg: _AnyDegrees) -> _CoefArray: ... # keep in sync with the broadest overload of `._FuncVander` -_AnyFuncVander: TypeAlias = Callable[ - [npt.ArrayLike, SupportsIndex], - _CoefArray, -] +_AnyFuncVander: TypeAlias = Callable[[npt.ArrayLike, SupportsIndex], _CoefArray] @type_check_only -class _FuncVanderND(_Named[_Name_co], Protocol[_Name_co]): +class _FuncVanderND(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__( self, @@ -673,10 +420,10 @@ class _FuncVanderND(_Named[_Name_co], Protocol[_Name_co]): degrees: Sequence[SupportsIndex], ) -> _CoefArray: ... -_FullFitResult: TypeAlias = Sequence[np.inexact[Any] | np.int32] +_FullFitResult: TypeAlias = Sequence[np.inexact | np.int32] @type_check_only -class _FuncFit(_Named[_Name_co], Protocol[_Name_co]): +class _FuncFit(_Named[_NameT_co], Protocol[_NameT_co]): @overload def __call__( self, @@ -781,66 +528,38 @@ class _FuncFit(_Named[_Name_co], Protocol[_Name_co]): ) -> tuple[_ObjectArray, _FullFitResult]: ... @type_check_only -class _FuncRoots(_Named[_Name_co], Protocol[_Name_co]): +class _FuncRoots(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - c: _SeriesLikeFloat_co, - ) -> _Series[np.float64]: ... + def __call__(self, /, c: _SeriesLikeFloat_co) -> _Series[np.float64]: ... @overload - def __call__( - self, - /, - c: _SeriesLikeComplex_co, - ) -> _Series[np.complex128]: ... + def __call__(self, /, c: _SeriesLikeComplex_co) -> _Series[np.complex128]: ... @overload def __call__(self, /, c: _SeriesLikeCoef_co) -> _ObjectSeries: ... _Companion: TypeAlias = np.ndarray[tuple[int, int], np.dtype[_SCT]] @type_check_only -class _FuncCompanion(_Named[_Name_co], Protocol[_Name_co]): +class _FuncCompanion(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - c: _SeriesLikeFloat_co, - ) -> _Companion[np.float64]: ... + def __call__(self, /, c: _SeriesLikeFloat_co) -> _Companion[np.float64]: ... @overload - def __call__( - self, - /, - c: _SeriesLikeComplex_co, - ) -> _Companion[np.complex128]: ... + def __call__(self, /, c: _SeriesLikeComplex_co) -> _Companion[np.complex128]: ... @overload def __call__(self, /, c: _SeriesLikeCoef_co) -> _Companion[np.object_]: ... @type_check_only -class _FuncGauss(_Named[_Name_co], Protocol[_Name_co]): - def __call__( - self, - /, - deg: SupportsIndex, - ) -> _Tuple2[_Series[np.float64]]: ... +class _FuncGauss(_Named[_NameT_co], Protocol[_NameT_co]): + def __call__(self, /, deg: SupportsIndex) -> _Tuple2[_Series[np.float64]]: ... @type_check_only -class _FuncWeight(_Named[_Name_co], Protocol[_Name_co]): +class _FuncWeight(_Named[_NameT_co], Protocol[_NameT_co]): @overload - def __call__( - self, - /, - c: _ArrayLikeFloat_co, - ) -> npt.NDArray[np.float64]: ... + def __call__(self, /, c: _ArrayLikeFloat_co) -> npt.NDArray[np.float64]: ... @overload - def __call__( - self, - /, - c: _ArrayLikeComplex_co, - ) -> npt.NDArray[np.complex128]: ... + def __call__(self, /, c: _ArrayLikeComplex_co) -> npt.NDArray[np.complex128]: ... @overload def __call__(self, /, c: _ArrayLikeCoef_co) -> _ObjectArray: ... @type_check_only -class _FuncPts(_Named[_Name_co], Protocol[_Name_co]): +class _FuncPts(_Named[_NameT_co], Protocol[_NameT_co]): def __call__(self, /, npts: _AnyInt) -> _Series[np.float64]: ... diff --git a/tests/data/fail/bitwise_ops.pyi b/tests/data/fail/bitwise_ops.pyi index ddd2d91f..13028330 100644 --- a/tests/data/fail/bitwise_ops.pyi +++ b/tests/data/fail/bitwise_ops.pyi @@ -1,10 +1,12 @@ +from typing import Final + import numpy as np -i8 = np.int64() -i4 = np.int32() -u8 = np.uint64() -b_ = np.bool() -i = 0 +i8: Final[np.int64] = ... +i4: Final[np.int32] = ... +u8: Final[np.uint64] = ... +b_: Final[np.bool] = ... +i: Final[int] = 0 f8 = np.float64() diff --git a/tests/data/fail/random.pyi b/tests/data/fail/random.pyi index 5688eb79..f4935acc 100644 --- a/tests/data/fail/random.pyi +++ b/tests/data/fail/random.pyi @@ -1,62 +1,64 @@ +from typing import Final + import numpy as np import numpy.typing as npt -SEED_FLOAT: float = 457.3 -SEED_ARR_FLOAT: npt.NDArray[np.float64] = ... -SEED_ARRLIKE_FLOAT: list[float] = [1.0, 2.0, 3.0, 4.0] -SEED_SEED_SEQ: np.random.SeedSequence = ... -SEED_STR: str = "String seeding not allowed" +SEED_FLOAT: Final[float] = ... +SEED_ARR_FLOAT: Final[npt.NDArray[np.float64]] = ... +SEED_ARRLIKE_FLOAT: Final[list[float]] = ... +SEED_SEED_SEQ: Final[np.random.SeedSequence] = ... +SEED_STR: Final[str] = ... # default rng -np.random.default_rng(SEED_FLOAT) # E: incompatible type -np.random.default_rng(SEED_ARR_FLOAT) # E: incompatible type -np.random.default_rng(SEED_ARRLIKE_FLOAT) # E: incompatible type -np.random.default_rng(SEED_STR) # E: incompatible type +np.random.default_rng(SEED_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.default_rng(SEED_ARR_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.default_rng(SEED_ARRLIKE_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.default_rng(SEED_STR) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] # Seed Sequence -np.random.SeedSequence(SEED_FLOAT) # E: incompatible type -np.random.SeedSequence(SEED_ARR_FLOAT) # E: incompatible type -np.random.SeedSequence(SEED_ARRLIKE_FLOAT) # E: incompatible type -np.random.SeedSequence(SEED_SEED_SEQ) # E: incompatible type -np.random.SeedSequence(SEED_STR) # E: incompatible type +np.random.SeedSequence(SEED_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.SeedSequence(SEED_ARR_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.SeedSequence(SEED_ARRLIKE_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.SeedSequence(SEED_SEED_SEQ) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.SeedSequence(SEED_STR) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] seed_seq: np.random.bit_generator.SeedSequence = ... -seed_seq.spawn(11.5) # E: incompatible type -seed_seq.generate_state(3.14) # E: incompatible type -seed_seq.generate_state(3, np.uint8) # E: incompatible type -seed_seq.generate_state(3, "uint8") # E: incompatible type -seed_seq.generate_state(3, "u1") # E: incompatible type -seed_seq.generate_state(3, np.uint16) # E: incompatible type -seed_seq.generate_state(3, "uint16") # E: incompatible type -seed_seq.generate_state(3, "u2") # E: incompatible type -seed_seq.generate_state(3, np.int32) # E: incompatible type -seed_seq.generate_state(3, "int32") # E: incompatible type -seed_seq.generate_state(3, "i4") # E: incompatible type +seed_seq.spawn(11.5) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3.14) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, np.uint8) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, "uint8") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, "u1") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, np.uint16) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, "uint16") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, "u2") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, np.int32) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, "int32") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +seed_seq.generate_state(3, "i4") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] # Bit Generators -np.random.MT19937(SEED_FLOAT) # E: incompatible type -np.random.MT19937(SEED_ARR_FLOAT) # E: incompatible type -np.random.MT19937(SEED_ARRLIKE_FLOAT) # E: incompatible type -np.random.MT19937(SEED_STR) # E: incompatible type - -np.random.PCG64(SEED_FLOAT) # E: incompatible type -np.random.PCG64(SEED_ARR_FLOAT) # E: incompatible type -np.random.PCG64(SEED_ARRLIKE_FLOAT) # E: incompatible type -np.random.PCG64(SEED_STR) # E: incompatible type - -np.random.Philox(SEED_FLOAT) # E: incompatible type -np.random.Philox(SEED_ARR_FLOAT) # E: incompatible type -np.random.Philox(SEED_ARRLIKE_FLOAT) # E: incompatible type -np.random.Philox(SEED_STR) # E: incompatible type - -np.random.SFC64(SEED_FLOAT) # E: incompatible type -np.random.SFC64(SEED_ARR_FLOAT) # E: incompatible type -np.random.SFC64(SEED_ARRLIKE_FLOAT) # E: incompatible type -np.random.SFC64(SEED_STR) # E: incompatible type +np.random.MT19937(SEED_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.MT19937(SEED_ARR_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.MT19937(SEED_ARRLIKE_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.MT19937(SEED_STR) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] + +np.random.PCG64(SEED_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.PCG64(SEED_ARR_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.PCG64(SEED_ARRLIKE_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.PCG64(SEED_STR) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] + +np.random.Philox(SEED_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.Philox(SEED_ARR_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.Philox(SEED_ARRLIKE_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.Philox(SEED_STR) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] + +np.random.SFC64(SEED_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.SFC64(SEED_ARR_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.SFC64(SEED_ARRLIKE_FLOAT) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.SFC64(SEED_STR) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] # Generator -np.random.Generator(None) # E: incompatible type -np.random.Generator(12333283902830213) # E: incompatible type -np.random.Generator("OxFEEDF00D") # E: incompatible type -np.random.Generator([123, 234]) # E: incompatible type -np.random.Generator(np.array([123, 234], dtype="u4")) # E: incompatible type +np.random.Generator(None) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.Generator(12333283902830213) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.Generator("OxFEEDF00D") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.Generator([123, 234]) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] +np.random.Generator(np.array([123, 234], dtype="u4")) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] diff --git a/tests/data/reveal/array_constructors.pyi b/tests/data/reveal/array_constructors.pyi index 7ae7b9f6..89035a0d 100644 --- a/tests/data/reveal/array_constructors.pyi +++ b/tests/data/reveal/array_constructors.pyi @@ -7,9 +7,9 @@ from typing_extensions import TypeVar, assert_type import numpy as np import numpy.typing as npt -_SCT = TypeVar("_SCT", bound=np.generic, covariant=True) +_SCT_co = TypeVar("_SCT_co", bound=np.generic, covariant=True) -class SubClass(npt.NDArray[_SCT]): ... +class SubClass(npt.NDArray[_SCT_co]): ... i8: np.int64 @@ -48,7 +48,7 @@ assert_type(np.empty([1, 5, 6], dtype=np.int64), npt.NDArray[np.int64]) assert_type(np.empty([1, 5, 6], dtype="c16"), npt.NDArray[Any]) assert_type(np.concatenate(A), npt.NDArray[np.float64]) -assert_type(np.concatenate([A, A]), npt.NDArray[Any]) +assert_type(np.concatenate([A, A]), npt.NDArray[Any]) # pyright: ignore[reportAssertTypeFailure] # npt.NDArray[np.float64] assert_type(np.concatenate([[1], A]), npt.NDArray[Any]) assert_type(np.concatenate([[1], [1]]), npt.NDArray[Any]) assert_type(np.concatenate((A, A)), npt.NDArray[np.float64]) @@ -227,7 +227,7 @@ assert_type(np.stack([C, C]), npt.NDArray[Any]) assert_type(np.stack([A, A], axis=0), npt.NDArray[np.float64]) assert_type(np.stack([A, A], out=B), SubClass[np.float64]) -assert_type(np.block([[A, A], [A, A]]), npt.NDArray[Any]) +assert_type(np.block([[A, A], [A, A]]), npt.NDArray[Any]) # pyright: ignore[reportAssertTypeFailure] # npt.NDArray[np.float64] assert_type(np.block(C), npt.NDArray[Any]) if sys.version_info >= (3, 12): diff --git a/tests/data/reveal/comparisons.pyi b/tests/data/reveal/comparisons.pyi index 6630628f..7145931e 100644 --- a/tests/data/reveal/comparisons.pyi +++ b/tests/data/reveal/comparisons.pyi @@ -1,5 +1,6 @@ import decimal import fractions +from typing import Final from typing_extensions import assert_type import numpy as np @@ -20,15 +21,15 @@ td = np.timedelta64(0, "D") b_ = np.bool() -b = False -c = complex() -f = 0.0 -i = 0 +b: Final = True +c: Final = 1j +f: Final = 1.0 +i: Final = 1 -AR = np.array([0], dtype=np.int64) +AR: Final = np.array([0], dtype=np.int64) # noqa: PYI015 AR.setflags(write=False) -SEQ = (0, 1, 2, 3, 4) +SEQ: Final = (0, 1, 2, 3, 4) # object-like comparisons @@ -150,7 +151,7 @@ assert_type(i4 > f8, np.bool) assert_type(b_ > f8, np.bool) assert_type(b > f8, np.bool) assert_type(c > f8, np.bool) -assert_type(f > f8, np.bool) +assert_type(f > f8, np.bool) # pyright: ignore[reportAssertTypeFailure] # builtins.bool assert_type(i > f8, np.bool) assert_type(f8 < AR, npt.NDArray[np.bool]) assert_type(f8 < SEQ, npt.NDArray[np.bool]) diff --git a/tests/data/reveal/lib_polynomial.pyi b/tests/data/reveal/lib_polynomial.pyi index d18c5a21..f0cdca2b 100644 --- a/tests/data/reveal/lib_polynomial.pyi +++ b/tests/data/reveal/lib_polynomial.pyi @@ -1,6 +1,6 @@ from collections.abc import Iterator from typing import Any, NoReturn -from typing_extensions import assert_type +from typing_extensions import LiteralString, assert_type import numpy as np import numpy.typing as npt @@ -14,7 +14,7 @@ AR_O: npt.NDArray[np.object_] poly_obj: np.poly1d -assert_type(poly_obj.variable, str) +assert_type(poly_obj.variable, LiteralString) assert_type(poly_obj.order, int) assert_type(poly_obj.o, int) assert_type(poly_obj.roots, npt.NDArray[Any]) diff --git a/tests/data/reveal/mod.pyi b/tests/data/reveal/mod.pyi index b14b54c5..45170d84 100644 --- a/tests/data/reveal/mod.pyi +++ b/tests/data/reveal/mod.pyi @@ -1,24 +1,25 @@ import datetime as dt +from typing import Literal as L from typing_extensions import assert_type import numpy as np import numpy.typing as npt from numpy._typing import _32Bit, _64Bit -f8 = np.float64() -i8 = np.int64() -u8 = np.uint64() +f8: np.float64 +i8: np.int64 +u8: np.uint64 -f4 = np.float32() -i4 = np.int32() -u4 = np.uint32() +f4: np.float32 +i4: np.int32 +u4: np.uint32 -td = np.timedelta64(0, "D") -b_ = np.bool() +td: np.timedelta64 +b_: np.bool[L[False]] -b = False -f = 0.0 -i = 0 +b: bool +i: int +f: float AR_b: npt.NDArray[np.bool] AR_m: npt.NDArray[np.timedelta64] diff --git a/tests/data/reveal/multiarray.pyi b/tests/data/reveal/multiarray.pyi index 052c7f1a..acbe106a 100644 --- a/tests/data/reveal/multiarray.pyi +++ b/tests/data/reveal/multiarray.pyi @@ -5,9 +5,9 @@ from typing_extensions import TypeVar, assert_type import numpy as np import numpy.typing as npt -_SCT = TypeVar("_SCT", bound=np.generic, covariant=True) +_SCT_co = TypeVar("_SCT_co", bound=np.generic, covariant=True) -class SubClass(npt.NDArray[_SCT]): ... +class SubClass(npt.NDArray[_SCT_co]): ... subclass: SubClass[np.float64] diff --git a/tests/data/reveal/nbit_base_example.pyi b/tests/data/reveal/nbit_base_example.pyi index bcb7a6ec..ad9e4cbe 100644 --- a/tests/data/reveal/nbit_base_example.pyi +++ b/tests/data/reveal/nbit_base_example.pyi @@ -4,10 +4,10 @@ import numpy as np import numpy.typing as npt from numpy._typing import _32Bit, _64Bit -T1 = TypeVar("T1", bound=npt.NBitBase) -T2 = TypeVar("T2", bound=npt.NBitBase) +_NBT1 = TypeVar("_NBT1", bound=npt.NBitBase) +_NBT2 = TypeVar("_NBT2", bound=npt.NBitBase) -def add(a: np.floating[T1], b: np.integer[T2]) -> np.floating[T1 | T2]: ... +def add(a: np.floating[_NBT1], b: np.integer[_NBT2]) -> np.floating[_NBT1 | _NBT2]: ... i8: np.int64 i4: np.int32 diff --git a/tests/data/reveal/ndarray_conversion.pyi b/tests/data/reveal/ndarray_conversion.pyi index c3e5d2bb..e0ec60b5 100644 --- a/tests/data/reveal/ndarray_conversion.pyi +++ b/tests/data/reveal/ndarray_conversion.pyi @@ -54,8 +54,14 @@ assert_type(i4_2d.astype(np.uint16), np.ndarray[tuple[int, int], np.dtype[np.uin assert_type(np.astype(i4_2d, np.uint16), np.ndarray[tuple[int, int], np.dtype[np.uint16]]) assert_type(f8_3d.astype(np.int16), np.ndarray[tuple[int, int, int], np.dtype[np.int16]]) assert_type(np.astype(f8_3d, np.int16), np.ndarray[tuple[int, int, int], np.dtype[np.int16]]) -assert_type(i4_2d.astype(uncertain_dtype), np.ndarray[tuple[int, int], np.dtype[np.generic[Any]]]) -assert_type(np.astype(i4_2d, uncertain_dtype), np.ndarray[tuple[int, int], np.dtype[Any]]) +assert_type( + i4_2d.astype(uncertain_dtype), # pyright: ignore[reportAssertTypeFailure] # ndarray[(int, int), np.dtype[Any]] + np.ndarray[tuple[int, int], np.dtype[np.int32 | np.float64 | np.str_]], +) +assert_type( + np.astype(i4_2d, uncertain_dtype), # pyright: ignore[reportAssertTypeFailure] # ndarray[(int, int), np.dtype[Any]] + np.ndarray[tuple[int, int], np.dtype[np.int32 | np.float64 | np.str_]], +) # byteswap assert_type(i0_nd.byteswap(), npt.NDArray[np.int_]) @@ -68,7 +74,10 @@ assert_type(i0_nd.copy("C"), npt.NDArray[np.int_]) assert_type(i0_nd.view(), npt.NDArray[np.int_]) assert_type(i0_nd.view(np.float64), npt.NDArray[np.float64]) assert_type(i0_nd.view(float), npt.NDArray[Any]) -assert_type(i0_nd.view(np.float64, np.matrix), np.matrix[tuple[int, int], Any]) +assert_type( + i0_nd.view(np.float64, np.matrix), # pyright: ignore[reportAssertTypeFailure] # matrix[Unknown, Unknown] + np.matrix[tuple[int, int], Any], +) # getfield assert_type(i0_nd.getfield("float"), npt.NDArray[Any]) diff --git a/tests/data/reveal/polynomial_polybase.pyi b/tests/data/reveal/polynomial_polybase.pyi index df68ed63..f534d278 100644 --- a/tests/data/reveal/polynomial_polybase.pyi +++ b/tests/data/reveal/polynomial_polybase.pyi @@ -8,25 +8,13 @@ import numpy as np import numpy.polynomial as npp import numpy.typing as npt -_Ar_x: TypeAlias = npt.NDArray[np.inexact[Any] | np.object_] -_Ar_f: TypeAlias = npt.NDArray[np.floating[Any]] -_Ar_c: TypeAlias = npt.NDArray[np.complexfloating[Any, Any]] -_Ar_O: TypeAlias = npt.NDArray[np.object_] - -_Ar_x_n: TypeAlias = np.ndarray[tuple[int], np.dtype[np.inexact[Any] | np.object_]] -_Ar_f_n: TypeAlias = np.ndarray[tuple[int], np.dtype[np.floating[Any]]] -_Ar_c_n: TypeAlias = np.ndarray[tuple[int], np.dtype[np.complexfloating[Any, Any]]] -_Ar_O_n: TypeAlias = np.ndarray[tuple[int], np.dtype[np.object_]] - -_Ar_x_2: TypeAlias = np.ndarray[tuple[L[2]], np.dtype[np.inexact[Any] | np.object_]] -_Ar_f_2: TypeAlias = np.ndarray[tuple[L[2]], np.dtype[np.floating[Any]]] -_Ar_c_2: TypeAlias = np.ndarray[tuple[L[2]], np.dtype[np.complexfloating[Any, Any]]] -_Ar_O_2: TypeAlias = np.ndarray[tuple[L[2]], np.dtype[np.object_]] +_Ar_x_n: TypeAlias = np.ndarray[tuple[int], np.dtype[np.inexact | np.object_]] +_Ar_x_2: TypeAlias = np.ndarray[tuple[L[2]], np.dtype[np.inexact | np.object_]] _SCT = TypeVar("_SCT", bound=np.generic) _Ar_1d: TypeAlias = np.ndarray[tuple[int], np.dtype[_SCT]] -_BasisName: TypeAlias = L["X"] +### SC_i: np.int_ SC_i_co: int | np.int_ @@ -42,7 +30,7 @@ AR_f_co: npt.NDArray[np.float64] | npt.NDArray[np.int_] AR_c: npt.NDArray[np.complex128] AR_c_co: npt.NDArray[np.complex128] | npt.NDArray[np.float64] | npt.NDArray[np.int_] AR_O: npt.NDArray[np.object_] -AR_O_co: npt.NDArray[np.object_ | np.number[Any]] +AR_O_co: npt.NDArray[np.object_ | np.number] SQ_i: Sequence[int] SQ_f: Sequence[float] @@ -57,6 +45,8 @@ PS_lag: npp.Laguerre PS_leg: npp.Legendre PS_all: npp.Polynomial | npp.Chebyshev | npp.Hermite | npp.HermiteE | npp.Laguerre | npp.Legendre +### + # static- and classmethods assert_type(type(PS_poly).basis_name, None) @@ -153,7 +143,7 @@ assert_type(PS_poly.fit(AR_c_co, SQ_c, SQ_i), npp.Polynomial) assert_type(PS_lag.fit(SQ_c, SQ_c, SQ_i, full=False), npp.Laguerre) assert_type( PS_herme.fit(SQ_c, AR_c_co, SC_i_co, full=True), - tuple[npp.HermiteE, Sequence[np.inexact[Any] | np.int32]], + tuple[npp.HermiteE, Sequence[np.inexact | np.int32]], ) # custom operations @@ -166,7 +156,7 @@ assert_type(repr(PS_all), str) assert_type(format(PS_all), str) assert_type(len(PS_all), int) -assert_type(next(iter(PS_all)), np.inexact[Any] | object) +assert_type(next(iter(PS_all)), np.inexact | object) assert_type(PS_all(SC_f_co), np.float64 | np.complex128) assert_type(PS_all(SC_c_co), np.complex128) diff --git a/tests/data/reveal/scalars.pyi b/tests/data/reveal/scalars.pyi index 7e3fe24d..e968853f 100644 --- a/tests/data/reveal/scalars.pyi +++ b/tests/data/reveal/scalars.pyi @@ -3,7 +3,7 @@ from typing_extensions import assert_type import numpy as np -_1: TypeAlias = Literal[1] +_1: TypeAlias = Literal[1] # noqa: PYI042 b: np.bool u8: np.uint64 diff --git a/tests/data/reveal/testing.pyi b/tests/data/reveal/testing.pyi index c0c3b719..041cf33f 100644 --- a/tests/data/reveal/testing.pyi +++ b/tests/data/reveal/testing.pyi @@ -17,13 +17,10 @@ AR_i8: npt.NDArray[np.int64] bool_obj: bool suppress_obj: np.testing.suppress_warnings -FT = TypeVar("FT", bound=Callable[..., Any]) +_FT = TypeVar("_FT", bound=Callable[..., object]) def func() -> int: ... -def func2( - x: npt.NDArray[np.number[Any]], - y: npt.NDArray[np.number[Any]], -) -> npt.NDArray[np.bool]: ... +def func2(x: npt.NDArray[np.number], y: npt.NDArray[np.number]) -> npt.NDArray[np.bool]: ... assert_type(np.testing.KnownFailureException(), np.testing.KnownFailureException) assert_type(np.testing.IgnoreException(), np.testing.IgnoreException) @@ -144,11 +141,11 @@ assert_type( unittest.case._AssertRaisesContext[RuntimeWarning], ) assert_type(np.testing.assert_raises_regex(RuntimeWarning, b"test", func3, 5), None) -assert_type(np.testing.assert_raises_regex(RuntimeWarning, re.compile(b"test"), func3, 5), None) +assert_type(np.testing.assert_raises_regex(RuntimeWarning, re.compile(rb"test"), func3, 5), None) class Test: ... -def decorate(a: FT) -> FT: ... +def decorate(a: _FT) -> _FT: ... assert_type(np.testing.decorate_methods(Test, decorate), None) assert_type(np.testing.decorate_methods(Test, decorate, None), None) diff --git a/tests/ruff.toml b/tests/ruff.toml new file mode 100644 index 00000000..9359200b --- /dev/null +++ b/tests/ruff.toml @@ -0,0 +1,15 @@ +extend = "../pyproject.toml" + +builtins = ["reveal_type"] + +[lint] +extend-ignore = [ + "ERA001", # eradicate: commented-out-code + "B015", # flake8-bugbear: useless-comparison + "B018", # flake8-bugbear: useless-expression + "PYI017", # flake8-pyi: complex-assignment-in-stub + "PT009", # flake8-pytest-style: pytest-unittest-assertion + "PTH", # flake8-use-pathlib + "PLC2801", # pylint/C: unnecessary-dunder-call + "PLR0124", # pylint/R: comparison-with-itself +] diff --git a/tools/codemods.py b/tools/codemods.py index 32005185..e5a49f26 100644 --- a/tools/codemods.py +++ b/tools/codemods.py @@ -42,7 +42,7 @@ class AnnotateIncomplete(VisitorBasedCodemodCommand): To run this codemod, run the following command from the root directory: ```shell - uv run -m libcst.tool codemod -x --include-stubs tools.codemods.AnnotateIncomplete src/numpy-stubs + uv run -m libcst.tool codemod -x --include-stubs tools.codemods.AnnotateIncomplete . ``` """ @@ -50,20 +50,31 @@ def __init__(self, /, context: CodemodContext) -> None: super().__init__(context) @override - def leave_Param(self, /, original_node: cst.Param, updated_node: cst.Param) -> cst.Param: - if updated_node.annotation is not None or updated_node.name.value in {"self", "cls", "_cls"}: + def leave_Param( + self, /, original_node: cst.Param, updated_node: cst.Param + ) -> cst.Param: + if ( + updated_node.annotation is not None + or updated_node.name.value in {"self", "cls", "_cls"} + ): # fmt: skip return updated_node AddImportsVisitor.add_needed_import(self.context, "_typeshed", "Incomplete") - return updated_node.with_changes(annotation=cst.Annotation(cst.Name("Incomplete"))) + return updated_node.with_changes( + annotation=cst.Annotation(cst.Name("Incomplete")) + ) @override - def leave_FunctionDef(self, /, original_node: cst.FunctionDef, updated_node: cst.FunctionDef) -> cst.FunctionDef: + def leave_FunctionDef( + self, /, original_node: cst.FunctionDef, updated_node: cst.FunctionDef + ) -> cst.FunctionDef: if updated_node.returns is not None: return updated_node if (name := updated_node.name.value) in _DUNDER_RETURN: - return updated_node.with_changes(returns=cst.Annotation(cst.Name(_DUNDER_RETURN[name]))) + return updated_node.with_changes( + returns=cst.Annotation(cst.Name(_DUNDER_RETURN[name])) + ) AddImportsVisitor.add_needed_import(self.context, "_typeshed", "Incomplete") return updated_node.with_changes(returns=cst.Annotation(cst.Name("Incomplete"))) @@ -71,17 +82,13 @@ def leave_FunctionDef(self, /, original_node: cst.FunctionDef, updated_node: cst class MoveNoneToEnd(VisitorBasedCodemodCommand): """ - A codemod that moves `None` to the end of union types when using the `|` operator, fixing `RUF036`. + Moves `None` to the end, fixing `RUF036`, e.g. `None | T | D` => `T | D | None`. To run this codemod, run the following command from the root directory: ```shell - uv run -m libcst.tool codemod -x --include-stubs tools.codemods.MoveNoneToEnd src/numpy-stubs + uv run -m libcst.tool codemod -x --include-stubs tools.codemods.MoveNoneToEnd . ``` - - Example: - - Before: None | str | int - - After: str | int | None """ @override @@ -96,7 +103,10 @@ def leave_BinaryOperation( # Collect all types in the union def collect_types(node: cst.BaseExpression) -> list[cst.BaseExpression]: - if isinstance(node, cst.BinaryOperation) and isinstance(node.operator, cst.BitOr): + if ( + isinstance(node, cst.BinaryOperation) + and isinstance(node.operator, cst.BitOr) + ): # fmt: skip return collect_types(node.left) + collect_types(node.right) return [node] @@ -108,7 +118,9 @@ def collect_types(node: cst.BaseExpression) -> list[cst.BaseExpression]: return updated_node # Remove None and add it to the ends - other_types = [t for t in types if not (isinstance(t, cst.Name) and t.value == "None")] + other_types = [ + t for t in types if not (isinstance(t, cst.Name) and t.value == "None") + ] reordered_types = other_types + none_types # Rebuild the union expression @@ -121,7 +133,7 @@ def build_union(exprs: Sequence[cst.BaseExpression]) -> cst.BaseExpression: class FixTypingImports310(VisitorBasedCodemodCommand): - """Imports `typing` imports from `typing_extensions` instead, if they aren't available on Python 3.10.""" + """Imports unavailable py310 `typing` imports from `typing_extensions`.""" _UNAVAILABLE: ClassVar[set[str]] = { "LiteralString", @@ -144,7 +156,10 @@ def leave_ImportFrom( original_node: cst.ImportFrom, updated_node: cst.ImportFrom, ) -> cst.ImportFrom | cst.RemovalSentinel: - if isinstance(module := updated_node.module, cst.Name) and module.value == "typing": + if ( + isinstance(module := updated_node.module, cst.Name) + and module.value == "typing" + ): aliases = updated_node.names assert isinstance(aliases, Sequence) @@ -158,17 +173,29 @@ def leave_ImportFrom( asname = alias.asname.name assert isinstance(asname, cst.Name) - AddImportsVisitor.add_needed_import(self.context, "typing_extensions", name.value, asname.value) + AddImportsVisitor.add_needed_import( + self.context, + "typing_extensions", + name.value, + asname.value, + ) else: - AddImportsVisitor.add_needed_import(self.context, "typing_extensions", name.value) + AddImportsVisitor.add_needed_import( + self.context, + "typing_extensions", + name.value, + ) else: aliases_out.append(alias) if not aliases_out: return cst.RemovalSentinel.REMOVE + if len(aliases_out) != len(aliases): # prevent trailing comma - aliases_out[-1] = aliases[-1].with_changes(name=aliases_out[-1].name, asname=aliases_out[-1].asname) + aliases_out[-1] = aliases[-1].with_changes( + name=aliases_out[-1].name, asname=aliases_out[-1].asname + ) return updated_node.with_changes(names=aliases_out) return updated_node diff --git a/tools/ruff.toml b/tools/ruff.toml new file mode 100644 index 00000000..705590b7 --- /dev/null +++ b/tools/ruff.toml @@ -0,0 +1,3 @@ +extend = "../pyproject.toml" + +line-length = 88