From 60fbb9de6eec989b885fa1cb13db3b4ee07c143d Mon Sep 17 00:00:00 2001 From: jorenham Date: Mon, 5 Aug 2024 23:08:39 +0200 Subject: [PATCH 1/2] preliminary mypy compatibility --- .vscode/settings.json | 5 ++- optype/_can.py | 28 +++++------- optype/_do.py | 4 +- optype/_does.py | 89 ++------------------------------------ optype/_has.py | 39 ++++++++--------- optype/_utils.py | 4 +- optype/inspect.py | 6 +-- optype/numpy/_any_array.py | 3 +- optype/numpy/_any_dtype.py | 1 - optype/numpy/_scalar.py | 12 ++--- optype/pickle.py | 2 +- poetry.lock | 60 ++++++++++++++++++++++++- pyproject.toml | 9 ++++ 13 files changed, 119 insertions(+), 143 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 82753f1..094fabf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,7 @@ { "git.branchProtection": ["master"], - "githubPullRequests.overrideDefaultBranch": "dev" + "githubPullRequests.overrideDefaultBranch": "dev", + "mypy-type-checker.args": [ + "--config-file=pyproject.toml" + ] } diff --git a/optype/_can.py b/optype/_can.py index 8874fbc..7375d7d 100644 --- a/optype/_can.py +++ b/optype/_can.py @@ -1,3 +1,5 @@ +# mypy: disable-error-code="override" + from __future__ import annotations import sys @@ -168,7 +170,7 @@ class CanFormat(Protocol[_StrT_contra, _StrT_co]): or `str` subtypes. Note that `format()` *does not* upcast `Y` to `str`. """ @override - def __format__(self, fmt: _StrT_contra, /) -> _StrT_co: ... + def __format__(self, fmt: _StrT_contra, /) -> _StrT_co: ... # type: ignore[override] # Iteration @@ -310,7 +312,7 @@ def __call__(self, /, *args: _P.args, **kwargs: _P.kwargs) -> _AnyT_co: ... @set_module('optype') @runtime_checkable class CanGetattr(Protocol[_StrT_contra, _AnyT_co]): - def __getattr__(self, name: _StrT_contra, /) -> _AnyT_co: ... + def __getattr__(self, name: _StrT_contra, /) -> _AnyT_co: ... # type: ignore[misc] @set_module('optype') @@ -318,7 +320,7 @@ def __getattr__(self, name: _StrT_contra, /) -> _AnyT_co: ... class CanGetattribute(Protocol[_StrT_contra, _AnyT_co]): """Note that `isinstance(x, CanGetattribute)` is always `True`.""" @override - def __getattribute__(self, name: _StrT_contra, /) -> _AnyT_co: ... + def __getattribute__(self, name: _StrT_contra, /) -> _AnyT_co: ... # type: ignore[misc] @set_module('optype') @@ -326,7 +328,7 @@ def __getattribute__(self, name: _StrT_contra, /) -> _AnyT_co: ... class CanSetattr(Protocol[_StrT_contra, _AnyT_contra]): """Note that `isinstance(x, CanSetattr)` is always true.""" @override - def __setattr__( + def __setattr__( # type: ignore[misc] self, name: _StrT_contra, value: _AnyT_contra, @@ -539,12 +541,7 @@ def __pow__(self, exp: _T_contra, mod: None = ..., /) -> _T_co: ... @set_module('optype') @runtime_checkable class CanPow3(Protocol[_T_contra, _V_contra, _AnyIntT_co]): - def __pow__( - self, - exp: _T_contra, - mod: _V_contra, - /, - ) -> _AnyIntT_co: ... + def __pow__(self, exp: _T_contra, mod: _V_contra, /) -> _AnyIntT_co: ... @set_module('optype') @@ -559,12 +556,7 @@ def __pow__(self, exp: _T_contra, /) -> _T_co: ... @overload def __pow__(self, exp: _T_contra, mod: None = ..., /) -> _T_co: ... @overload - def __pow__( - self, - exp: _T_contra, - mod: _AnyIntT_contra, - /, - ) -> _AnyIntT_co: ... + def __pow__(self, exp: _T_contra, mod: _V_contra, /) -> _AnyIntT_co: ... @set_module('optype') @@ -1143,6 +1135,6 @@ class CanAwait(Protocol[_T_co]): # impossible to type. In practice, typecheckers work around that, by # accepting the lie called `collections.abc.Generator`... @overload - def __await__(self: CanAwait[None], /) -> CanNext[_FutureOrNone]: ... - @overload def __await__(self: CanAwait[_T_co], /) -> _AsyncGen[_T_co]: ... + @overload + def __await__(self: CanAwait[None], /) -> CanNext[_FutureOrNone]: ... diff --git a/optype/_do.py b/optype/_do.py index 3296825..8894963 100644 --- a/optype/_do.py +++ b/optype/_do.py @@ -1,3 +1,5 @@ +# mypy: disable-error-code="assignment" + from __future__ import annotations import math as _math @@ -57,7 +59,7 @@ # callables if _sys.version_info >= (3, 11): - do_call: _d.DoesCall = _o.call + do_call: _d.DoesCall = _o.call # type: ignore[attr-defined] else: _Pss_call = ParamSpec('_Pss_call') _R_call = TypeVar('_R_call') diff --git a/optype/_does.py b/optype/_does.py index dfd9c4f..8f03b21 100644 --- a/optype/_does.py +++ b/optype/_does.py @@ -5,9 +5,9 @@ if sys.version_info >= (3, 13): - from typing import ParamSpec, Protocol, TypeVar, final, overload + from typing import ParamSpec, Protocol, TypeVar, overload else: - from typing_extensions import ParamSpec, Protocol, TypeVar, final, overload + from typing_extensions import ParamSpec, Protocol, TypeVar, overload if TYPE_CHECKING: from collections.abc import Callable @@ -54,7 +54,6 @@ @set_module('optype') -@final class DoesNext(Protocol): @overload def __call__(self, iterator: _c.CanNext[_ValueT], /) -> _ValueT: ... @@ -68,7 +67,6 @@ def __call__( @set_module('optype') -@final class DoesANext(Protocol): @overload def __call__(self, aiterator: _c.CanANext[_ValueT], /) -> _ValueT: ... @@ -82,7 +80,6 @@ async def __call__( @set_module('optype') -@final class DoesIter(Protocol): @overload def __call__(self, iterable: _c.CanIter[_IteratorT], /) -> _IteratorT: ... @@ -109,7 +106,6 @@ def __call__( @set_module('optype') -@final class DoesAIter(Protocol): def __call__( self, @@ -121,25 +117,21 @@ def __call__( # type conversion @set_module('optype') -@final class DoesComplex(Protocol): def __call__(self, obj: _c.CanComplex, /) -> complex: ... @set_module('optype') -@final class DoesFloat(Protocol): def __call__(self, obj: _c.CanFloat, /) -> float: ... @set_module('optype') -@final class DoesInt(Protocol): def __call__(self, obj: _c.CanInt[_IntT], /) -> _IntT: ... @set_module('optype') -@final class DoesBool(Protocol): @overload def __call__(self, obj: _c.CanBool[_BoolT], /) -> _BoolT: ... @@ -152,13 +144,11 @@ def __call__(self, obj: object, /) -> bool: ... @set_module('optype') -@final class DoesStr(Protocol): def __call__(self, obj: _c.CanStr[_StrT], /) -> _StrT: ... @set_module('optype') -@final class DoesBytes(Protocol): def __call__(self, obj: _c.CanBytes[_BytesT], /) -> _BytesT: ... @@ -167,13 +157,11 @@ def __call__(self, obj: _c.CanBytes[_BytesT], /) -> _BytesT: ... @set_module('optype') -@final class DoesRepr(Protocol): def __call__(self, obj: _c.CanRepr[_StrT], /) -> _StrT: ... @set_module('optype') -@final class DoesFormat(Protocol): def __call__( self, @@ -187,7 +175,6 @@ def __call__( @set_module('optype') -@final class DoesLt(Protocol): @overload def __call__( @@ -206,7 +193,6 @@ def __call__( @set_module('optype') -@final class DoesLe(Protocol): @overload def __call__( @@ -225,7 +211,6 @@ def __call__( @set_module('optype') -@final class DoesEq(Protocol): @overload def __call__( @@ -235,7 +220,7 @@ def __call__( /, ) -> _OutT: ... @overload - def __call__( # pyright: ignore[reportOverlappingOverload] + def __call__( # type: ignore[misc] # pyright: ignore[reportOverlappingOverload] self, lhs: _LeftT, rhs: _c.CanEq[_LeftT, _OutT], @@ -244,7 +229,6 @@ def __call__( # pyright: ignore[reportOverlappingOverload] @set_module('optype') -@final class DoesNe(Protocol): @overload def __call__( @@ -254,7 +238,7 @@ def __call__( /, ) -> _OutT: ... @overload - def __call__( # pyright: ignore[reportOverlappingOverload] + def __call__( # type: ignore[misc] # pyright: ignore[reportOverlappingOverload] self, lhs: _LeftT, rhs: _c.CanNe[_LeftT, _OutT], @@ -263,7 +247,6 @@ def __call__( # pyright: ignore[reportOverlappingOverload] @set_module('optype') -@final class DoesGt(Protocol): @overload def __call__( @@ -282,7 +265,6 @@ def __call__( @set_module('optype') -@final class DoesGe(Protocol): @overload def __call__( @@ -304,7 +286,6 @@ def __call__( @set_module('optype') -@final class DoesGetattr(Protocol): @overload def __call__( @@ -339,7 +320,6 @@ def __call__( @set_module('optype') -@final class DoesSetattr(Protocol): def __call__( self, @@ -351,13 +331,11 @@ def __call__( @set_module('optype') -@final class DoesDelattr(Protocol): def __call__(self, obj: _c.CanDelattr[_StrT], name: _StrT, /) -> None: ... @set_module('optype') -@final class DoesDir(Protocol): @overload def __call__(self, /) -> list[str]: ... @@ -369,7 +347,6 @@ def __call__(self, obj: _c.CanDir[_IterT], /) -> _IterT: ... @set_module('optype') -@final class DoesCall(Protocol): def __call__( self, @@ -384,19 +361,16 @@ def __call__( @set_module('optype') -@final class DoesLen(Protocol): def __call__(self, obj: _c.CanLen[_IntT], /) -> _IntT: ... @set_module('optype') -@final class DoesLengthHint(Protocol): def __call__(self, obj: _c.CanLengthHint[_IntT], /) -> _IntT: ... @set_module('optype') -@final class DoesGetitem(Protocol): def __call__( self, @@ -410,7 +384,6 @@ def __call__( @set_module('optype') -@final class DoesSetitem(Protocol): def __call__( self, @@ -422,13 +395,11 @@ def __call__( @set_module('optype') -@final class DoesDelitem(Protocol): def __call__(self, obj: _c.CanDelitem[_KeyT], key: _KeyT, /) -> None: ... @set_module('optype') -@final class DoesMissing(Protocol): def __call__( self, @@ -439,7 +410,6 @@ def __call__( @set_module('optype') -@final class DoesContains(Protocol): def __call__( self, @@ -450,7 +420,6 @@ def __call__( @set_module('optype') -@final class DoesReversed(Protocol): """ This is correct type of `builtins.reversed`. @@ -472,7 +441,6 @@ def __call__( @set_module('optype') -@final class DoesAdd(Protocol): @overload def __call__( @@ -491,7 +459,6 @@ def __call__( @set_module('optype') -@final class DoesSub(Protocol): @overload def __call__( @@ -510,7 +477,6 @@ def __call__( @set_module('optype') -@final class DoesMul(Protocol): @overload def __call__( @@ -529,7 +495,6 @@ def __call__( @set_module('optype') -@final class DoesMatmul(Protocol): @overload def __call__( @@ -548,7 +513,6 @@ def __call__( @set_module('optype') -@final class DoesTruediv(Protocol): @overload def __call__( @@ -567,7 +531,6 @@ def __call__( @set_module('optype') -@final class DoesFloordiv(Protocol): @overload def __call__( @@ -586,7 +549,6 @@ def __call__( @set_module('optype') -@final class DoesMod(Protocol): @overload def __call__( @@ -605,7 +567,6 @@ def __call__( @set_module('optype') -@final class DoesDivmod(Protocol): @overload def __call__( @@ -624,7 +585,6 @@ def __call__( @set_module('optype') -@final class DoesPow(Protocol): @overload def __call__( @@ -651,7 +611,6 @@ def __call__( @set_module('optype') -@final class DoesLshift(Protocol): @overload def __call__( @@ -670,7 +629,6 @@ def __call__( @set_module('optype') -@final class DoesRshift(Protocol): @overload def __call__( @@ -689,7 +647,6 @@ def __call__( @set_module('optype') -@final class DoesAnd(Protocol): @overload def __call__( @@ -708,7 +665,6 @@ def __call__( @set_module('optype') -@final class DoesXor(Protocol): @overload def __call__( @@ -727,7 +683,6 @@ def __call__( @set_module('optype') -@final class DoesOr(Protocol): @overload def __call__( @@ -749,7 +704,6 @@ def __call__( @set_module('optype') -@final class DoesRAdd(Protocol): def __call__( self, @@ -760,7 +714,6 @@ def __call__( @set_module('optype') -@final class DoesRSub(Protocol): def __call__( self, @@ -771,7 +724,6 @@ def __call__( @set_module('optype') -@final class DoesRMul(Protocol): def __call__( self, @@ -782,7 +734,6 @@ def __call__( @set_module('optype') -@final class DoesRMatmul(Protocol): def __call__( self, @@ -793,7 +744,6 @@ def __call__( @set_module('optype') -@final class DoesRTruediv(Protocol): def __call__( self, @@ -804,7 +754,6 @@ def __call__( @set_module('optype') -@final class DoesRFloordiv(Protocol): def __call__( self, @@ -815,7 +764,6 @@ def __call__( @set_module('optype') -@final class DoesRMod(Protocol): def __call__( self, @@ -835,7 +783,6 @@ def __call__( @set_module('optype') -@final class DoesRPow(Protocol): def __call__( self, @@ -846,7 +793,6 @@ def __call__( @set_module('optype') -@final class DoesRLshift(Protocol): def __call__( self, @@ -857,7 +803,6 @@ def __call__( @set_module('optype') -@final class DoesRRshift(Protocol): def __call__( self, @@ -868,7 +813,6 @@ def __call__( @set_module('optype') -@final class DoesRAnd(Protocol): def __call__( self, @@ -879,7 +823,6 @@ def __call__( @set_module('optype') -@final class DoesRXor(Protocol): def __call__( self, @@ -890,7 +833,6 @@ def __call__( @set_module('optype') -@final class DoesROr(Protocol): def __call__( self, @@ -904,7 +846,6 @@ def __call__( @set_module('optype') -@final class DoesIAdd(Protocol): @overload def __call__( @@ -930,7 +871,6 @@ def __call__( @set_module('optype') -@final class DoesISub(Protocol): @overload def __call__( @@ -956,7 +896,6 @@ def __call__( @set_module('optype') -@final class DoesIMul(Protocol): @overload def __call__( @@ -982,7 +921,6 @@ def __call__( @set_module('optype') -@final class DoesIMatmul(Protocol): @overload def __call__( @@ -1008,7 +946,6 @@ def __call__( @set_module('optype') -@final class DoesITruediv(Protocol): @overload def __call__( @@ -1034,7 +971,6 @@ def __call__( @set_module('optype') -@final class DoesIFloordiv(Protocol): @overload def __call__( @@ -1060,7 +996,6 @@ def __call__( @set_module('optype') -@final class DoesIMod(Protocol): @overload def __call__( @@ -1086,7 +1021,6 @@ def __call__( @set_module('optype') -@final class DoesIPow(Protocol): @overload def __call__(self, @@ -1109,7 +1043,6 @@ def __call__(self, @set_module('optype') -@final class DoesILshift(Protocol): @overload def __call__( @@ -1135,7 +1068,6 @@ def __call__( @set_module('optype') -@final class DoesIRshift(Protocol): @overload def __call__( @@ -1161,7 +1093,6 @@ def __call__( @set_module('optype') -@final class DoesIAnd(Protocol): @overload def __call__( @@ -1187,7 +1118,6 @@ def __call__( @set_module('optype') -@final class DoesIXor(Protocol): @overload def __call__( @@ -1213,7 +1143,6 @@ def __call__( @set_module('optype') -@final class DoesIOr(Protocol): @overload def __call__( @@ -1242,25 +1171,21 @@ def __call__( @set_module('optype') -@final class DoesNeg(Protocol): def __call__(self, obj: _c.CanNeg[_OutT], /) -> _OutT: ... @set_module('optype') -@final class DoesPos(Protocol): def __call__(self, obj: _c.CanPos[_OutT], /) -> _OutT: ... @set_module('optype') -@final class DoesAbs(Protocol): def __call__(self, obj: _c.CanAbs[_OutT], /) -> _OutT: ... @set_module('optype') -@final class DoesInvert(Protocol): def __call__(self, obj: _c.CanInvert[_OutT], /) -> _OutT: ... @@ -1269,13 +1194,11 @@ def __call__(self, obj: _c.CanInvert[_OutT], /) -> _OutT: ... @set_module('optype') -@final class DoesIndex(Protocol): def __call__(self, obj: _c.CanIndex[_IntT], /) -> _IntT: ... @set_module('optype') -@final class DoesHash(Protocol): def __call__(self, obj: _c.CanHash, /) -> int: ... @@ -1284,7 +1207,6 @@ def __call__(self, obj: _c.CanHash, /) -> int: ... @set_module('optype') -@final class DoesRound(Protocol): @overload def __call__(self, obj: _c.CanRound1[_OutT], /) -> _OutT: ... @@ -1305,18 +1227,15 @@ def __call__( @set_module('optype') -@final class DoesTrunc(Protocol): def __call__(self, obj: _c.CanTrunc[_OutT], /) -> _OutT: ... @set_module('optype') -@final class DoesFloor(Protocol): def __call__(self, obj: _c.CanFloor[_OutT], /) -> _OutT: ... @set_module('optype') -@final class DoesCeil(Protocol): def __call__(self, obj: _c.CanCeil[_OutT], /) -> _OutT: ... diff --git a/optype/_has.py b/optype/_has.py index 85268da..ec4bba6 100644 --- a/optype/_has.py +++ b/optype/_has.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from typing import TYPE_CHECKING, Any, ClassVar, Final, TypeAlias +from typing import TYPE_CHECKING, Any, ClassVar, TypeAlias if sys.version_info >= (3, 13): @@ -52,7 +52,7 @@ class HasMatchArgs(Protocol): @set_module('optype') @runtime_checkable class HasSlots(Protocol): - __slots__: ClassVar[LiteralString | CanIter[CanNext[LiteralString]]] + __slots__: ClassVar[LiteralString | CanIter[CanNext[LiteralString]]] # type: ignore[assignment] _DictT = TypeVar('_DictT', bound='Mapping[str, Any]', default=dict[str, Any]) @@ -60,9 +60,9 @@ class HasSlots(Protocol): @set_module('optype') @runtime_checkable -class HasDict(Protocol[_DictT]): +class HasDict(Protocol[_DictT]): # type: ignore[misc] # the typeshed annotations for `builtins.object.__dict__` too narrow - __dict__: _DictT # pyright: ignore[reportIncompatibleVariableOverride] + __dict__: _DictT # type: ignore[assignment] # pyright: ignore[reportIncompatibleVariableOverride] @set_module('optype') @@ -85,35 +85,30 @@ class HasModule(Protocol[_ModuleT_co]): __module__: _ModuleT_co -_NameT_co = TypeVar('_NameT_co', covariant=True, bound=str, default=str) +_NameT = TypeVar('_NameT', bound=str, default=str) @set_module('optype') @runtime_checkable -class HasName(Protocol[_NameT_co]): - __name__: Final[_NameT_co] +class HasName(Protocol[_NameT]): + __name__: _NameT -_QualnameT_co = TypeVar( - '_QualnameT_co', - covariant=True, - bound=str, - default=str, -) +_QualnameT = TypeVar('_QualnameT', bound=str, default=str) @set_module('optype') @runtime_checkable -class HasQualname(Protocol[_QualnameT_co]): - __qualname__: _QualnameT_co +class HasQualname(Protocol[_QualnameT]): # pyright: ignore[reportInvalidTypeVarUse] + __qualname__: _QualnameT @set_module('optype') @runtime_checkable -class HasNames( - HasName[_NameT_co], - HasQualname[_QualnameT_co], - Protocol[_NameT_co, _QualnameT_co], +class HasNames( # pyright: ignore[reportInvalidTypeVarUse] + HasName[_NameT], + HasQualname[_QualnameT], + Protocol[_NameT, _QualnameT], ): ... @@ -132,7 +127,7 @@ class HasDoc(Protocol[_DocT_co]): _AnnotationsT_co = TypeVar( '_AnnotationsT_co', covariant=True, - bound='Mapping[str, Any]', + bound=dict[str, Any], default=dict[str, Any], ) @@ -140,7 +135,9 @@ class HasDoc(Protocol[_DocT_co]): @set_module('optype') @runtime_checkable class HasAnnotations(Protocol[_AnnotationsT_co]): - __annotations__: Final[_AnnotationsT_co] # pyright: ignore[reportIncompatibleVariableOverride] + @property + @override + def __annotations__(self) -> _AnnotationsT_co: ... # type: ignore[override] # pyright: ignore[reportIncompatibleVariableOverride] # should be one of `(TypeVar, TypeVarTuple, ParamSpec)` diff --git a/optype/_utils.py b/optype/_utils.py index 8cc01ab..8dd6b4a 100644 --- a/optype/_utils.py +++ b/optype/_utils.py @@ -9,13 +9,12 @@ if sys.version_info >= (3, 13): - from typing import LiteralString, Protocol, TypeVar, final, is_protocol + from typing import LiteralString, Protocol, TypeVar, is_protocol else: from typing_extensions import ( LiteralString, Protocol, TypeVar, - final, is_protocol, ) @@ -52,7 +51,6 @@ class _HasModule(Protocol): _HasModuleT = TypeVar('_HasModuleT', bound=_HasModule) -@final class _DoesSetModule(Protocol): def __call__(self, obj: _HasModuleT, /) -> _HasModuleT: ... diff --git a/optype/inspect.py b/optype/inspect.py index 51774ec..d7130fb 100644 --- a/optype/inspect.py +++ b/optype/inspect.py @@ -22,10 +22,10 @@ else: from typing_extensions import TypeAliasType, is_protocol, overload try: - from typing_extensions import TypeIs + from typing_extensions import TypeIs # type: ignore[attr-defined] except ImportError: # fallback for `typing_extensions<4.10` - from typing import TypeGuard as TypeIs + from typing import TypeGuard as TypeIs # type: ignore[assignment] if TYPE_CHECKING: from collections.abc import Callable as CanCall @@ -307,7 +307,7 @@ def get_protocols( elif hasattr(module, '__all__'): members = module.__all__ else: - members = (k for k in dir(module) if not k.startswith('_')) + members = [k for k in dir(module) if not k.startswith('_')] return frozenset({ cls for name in members diff --git a/optype/numpy/_any_array.py b/optype/numpy/_any_array.py index 77260a9..2420c08 100644 --- a/optype/numpy/_any_array.py +++ b/optype/numpy/_any_array.py @@ -1,7 +1,7 @@ from __future__ import annotations import sys -from typing import Final, TypeAlias as _Type, final +from typing import Final, TypeAlias as _Type import numpy as np @@ -76,7 +76,6 @@ T_co = TypeVar('T_co', covariant=True, bound=object) -@final @runtime_checkable class _PyArray(Protocol[T_co]): def __len__(self, /) -> int: ... diff --git a/optype/numpy/_any_dtype.py b/optype/numpy/_any_dtype.py index d548bec..bac1a57 100644 --- a/optype/numpy/_any_dtype.py +++ b/optype/numpy/_any_dtype.py @@ -1,4 +1,3 @@ - """ The allowed `np.dtype` arguments for specific scalar types. The names are analogous to those in `numpy.dtypes`. diff --git a/optype/numpy/_scalar.py b/optype/numpy/_scalar.py index 67f698c..72b9c31 100644 --- a/optype/numpy/_scalar.py +++ b/optype/numpy/_scalar.py @@ -27,9 +27,9 @@ ) # `CapsuleType` requires `typing_extensions>=4.12` try: - from typing_extensions import CapsuleType + from typing_extensions import CapsuleType # type: ignore[attr-defined] except ImportError: - from typing import Any as CapsuleType + from typing import Any as CapsuleType # type: ignore[assignment] if TYPE_CHECKING: from numpy._core.multiarray import flagsobj @@ -66,7 +66,7 @@ def base(self, /) -> None: ... @property def data(self, /) -> memoryview: ... @property - def dtype(self, /) -> np.dtype[Self]: ... # pyright: ignore[reportInvalidTypeArguments] + def dtype(self, /) -> np.dtype[Self]: ... # type: ignore[type-var] # pyright: ignore[reportInvalidTypeArguments] @property def flags(self, /) -> flagsobj: ... @property @@ -90,9 +90,9 @@ def __array_struct__(self, /) -> CapsuleType: ... @override def __hash__(self, /) -> int: ... @override - def __eq__(self, other: object, /) -> np.bool_: ... # pyright: ignore[reportIncompatibleMethodOverride] + def __eq__(self, other: object, /) -> np.bool_: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] @override - def __ne__(self, other: object, /) -> np.bool_: ... # pyright: ignore[reportIncompatibleMethodOverride] + def __ne__(self, other: object, /) -> np.bool_: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] def __bool__(self, /) -> bool: ... # Unlike `numpy/__init__.pyi` suggests, there exists no `__bytes__` method @@ -105,6 +105,6 @@ def __copy__(self, /) -> Self: ... def __deepcopy__(self, memo: dict[int, Any] | None, /) -> Self: ... @overload - def __array__(self, /) -> _Array0D[np.dtype[Self]]: ... # pyright: ignore[reportInvalidTypeArguments] + def __array__(self, /) -> _Array0D[np.dtype[Self]]: ... # type: ignore[type-var] # pyright: ignore[reportInvalidTypeArguments] @overload def __array__(self, dtype: _DT, /) -> _Array0D[_DT]: ... diff --git a/optype/pickle.py b/optype/pickle.py index 4eadd03..529a212 100644 --- a/optype/pickle.py +++ b/optype/pickle.py @@ -93,7 +93,7 @@ class CanReduceEx(Protocol[_ReduceT_co]): @override def __reduce_ex__( # pyright: ignore[reportIncompatibleMethodOverride] self, - protocol: CanIndex[_ProtocolVersion], + protocol: CanIndex[_ProtocolVersion], # type: ignore[override] /, ) -> _ReduceT_co: ... diff --git a/poetry.lock b/poetry.lock index cefad3e..c7c9183 100644 --- a/poetry.lock +++ b/poetry.lock @@ -208,6 +208,64 @@ files = [ {file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"}, ] +[[package]] +name = "mypy" +version = "1.11.1" +description = "Optional static typing for Python" +optional = false +python-versions = ">=3.8" +files = [ + {file = "mypy-1.11.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:a32fc80b63de4b5b3e65f4be82b4cfa362a46702672aa6a0f443b4689af7008c"}, + {file = "mypy-1.11.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:c1952f5ea8a5a959b05ed5f16452fddadbaae48b5d39235ab4c3fc444d5fd411"}, + {file = "mypy-1.11.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e1e30dc3bfa4e157e53c1d17a0dad20f89dc433393e7702b813c10e200843b03"}, + {file = "mypy-1.11.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:2c63350af88f43a66d3dfeeeb8d77af34a4f07d760b9eb3a8697f0386c7590b4"}, + {file = "mypy-1.11.1-cp310-cp310-win_amd64.whl", hash = "sha256:a831671bad47186603872a3abc19634f3011d7f83b083762c942442d51c58d58"}, + {file = "mypy-1.11.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:7b6343d338390bb946d449677726edf60102a1c96079b4f002dedff375953fc5"}, + {file = "mypy-1.11.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:e4fe9f4e5e521b458d8feb52547f4bade7ef8c93238dfb5bbc790d9ff2d770ca"}, + {file = "mypy-1.11.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:886c9dbecc87b9516eff294541bf7f3655722bf22bb898ee06985cd7269898de"}, + {file = "mypy-1.11.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:fca4a60e1dd9fd0193ae0067eaeeb962f2d79e0d9f0f66223a0682f26ffcc809"}, + {file = "mypy-1.11.1-cp311-cp311-win_amd64.whl", hash = "sha256:0bd53faf56de9643336aeea1c925012837432b5faf1701ccca7fde70166ccf72"}, + {file = "mypy-1.11.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:f39918a50f74dc5969807dcfaecafa804fa7f90c9d60506835036cc1bc891dc8"}, + {file = "mypy-1.11.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:0bc71d1fb27a428139dd78621953effe0d208aed9857cb08d002280b0422003a"}, + {file = "mypy-1.11.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b868d3bcff720dd7217c383474008ddabaf048fad8d78ed948bb4b624870a417"}, + {file = "mypy-1.11.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:a707ec1527ffcdd1c784d0924bf5cb15cd7f22683b919668a04d2b9c34549d2e"}, + {file = "mypy-1.11.1-cp312-cp312-win_amd64.whl", hash = "sha256:64f4a90e3ea07f590c5bcf9029035cf0efeae5ba8be511a8caada1a4893f5525"}, + {file = "mypy-1.11.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:749fd3213916f1751fff995fccf20c6195cae941dc968f3aaadf9bb4e430e5a2"}, + {file = "mypy-1.11.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:b639dce63a0b19085213ec5fdd8cffd1d81988f47a2dec7100e93564f3e8fb3b"}, + {file = "mypy-1.11.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4c956b49c5d865394d62941b109728c5c596a415e9c5b2be663dd26a1ff07bc0"}, + {file = "mypy-1.11.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:45df906e8b6804ef4b666af29a87ad9f5921aad091c79cc38e12198e220beabd"}, + {file = "mypy-1.11.1-cp38-cp38-win_amd64.whl", hash = "sha256:d44be7551689d9d47b7abc27c71257adfdb53f03880841a5db15ddb22dc63edb"}, + {file = "mypy-1.11.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:2684d3f693073ab89d76da8e3921883019ea8a3ec20fa5d8ecca6a2db4c54bbe"}, + {file = "mypy-1.11.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:79c07eb282cb457473add5052b63925e5cc97dfab9812ee65a7c7ab5e3cb551c"}, + {file = "mypy-1.11.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:11965c2f571ded6239977b14deebd3f4c3abd9a92398712d6da3a772974fad69"}, + {file = "mypy-1.11.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:a2b43895a0f8154df6519706d9bca8280cda52d3d9d1514b2d9c3e26792a0b74"}, + {file = "mypy-1.11.1-cp39-cp39-win_amd64.whl", hash = "sha256:1a81cf05975fd61aec5ae16501a091cfb9f605dc3e3c878c0da32f250b74760b"}, + {file = "mypy-1.11.1-py3-none-any.whl", hash = "sha256:0624bdb940255d2dd24e829d99a13cfeb72e4e9031f9492148f410ed30bcab54"}, + {file = "mypy-1.11.1.tar.gz", hash = "sha256:f404a0b069709f18bbdb702eb3dcfe51910602995de00bd39cea3050b5772d08"}, +] + +[package.dependencies] +mypy-extensions = ">=1.0.0" +tomli = {version = ">=1.1.0", markers = "python_version < \"3.11\""} +typing-extensions = ">=4.6.0" + +[package.extras] +dmypy = ["psutil (>=4.0)"] +install-types = ["pip"] +mypyc = ["setuptools (>=50)"] +reports = ["lxml"] + +[[package]] +name = "mypy-extensions" +version = "1.0.0" +description = "Type system extensions for programs checked with the mypy type checker." +optional = false +python-versions = ">=3.5" +files = [ + {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"}, + {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, +] + [[package]] name = "nodeenv" version = "1.9.1" @@ -669,4 +727,4 @@ numpy = ["numpy"] [metadata] lock-version = "2.0" python-versions = "^3.10.1" -content-hash = "d20d17e2a14553773d4f0d23c77a390c3e1302863069b5e60191eb2a259c5eeb" +content-hash = "a796c4eee6638defb1c8a339cc07d4f1ff59a7f597fa5b394f9dcc9c4cd08f62" diff --git a/pyproject.toml b/pyproject.toml index f8cc34f..941953e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ tox = "^4.16.0" [tool.poetry.group.lint.dependencies] basedpyright = "^1.15.1" codespell = "^2.3.0" +mypy = "^1.11.1" ruff = "^0.5.6" sp-repo-review = {version = "^2024.4.23", extras = ["cli"]} typing-extensions = "*" @@ -86,6 +87,14 @@ reportUnusedImport = false # dupe of F401 reportUnusedVariable = false # dupe of F841 +[tool.mypy] +python_version = "3.10" +always_true = "_NP_V2" +modules = ["optype"] +exclude = ["^.venv/.*", "^examples/.*", "^tests/.*"] +allow_redefinition = true + + [tool.pytest.ini_options] addopts = [ "-ra", From 7bc669a503c1810f95adfcbae4cd95bc4f49dc05 Mon Sep 17 00:00:00 2001 From: jorenham Date: Mon, 5 Aug 2024 23:17:23 +0200 Subject: [PATCH 2/2] fix the tests --- optype/_has.py | 6 ++---- tests/test_inspect.py | 3 --- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/optype/_has.py b/optype/_has.py index ec4bba6..4aeea7e 100644 --- a/optype/_has.py +++ b/optype/_has.py @@ -134,10 +134,8 @@ class HasDoc(Protocol[_DocT_co]): @set_module('optype') @runtime_checkable -class HasAnnotations(Protocol[_AnnotationsT_co]): - @property - @override - def __annotations__(self) -> _AnnotationsT_co: ... # type: ignore[override] # pyright: ignore[reportIncompatibleVariableOverride] +class HasAnnotations(Protocol[_AnnotationsT_co]): # pyright: ignore[reportInvalidTypeVarUse] + __annotations__: _AnnotationsT_co # type: ignore[override] # pyright: ignore[reportIncompatibleVariableOverride] # should be one of `(TypeVar, TypeVarTuple, ParamSpec)` diff --git a/tests/test_inspect.py b/tests/test_inspect.py index b23f84f..0d14746 100644 --- a/tests/test_inspect.py +++ b/tests/test_inspect.py @@ -191,9 +191,6 @@ def test_get_protocols(): def test_type_is_final(): - assert not opt.inspect.is_final(opt.CanAdd) - assert opt.inspect.is_final(opt.DoesAdd) - assert not opt.inspect.is_final(Proto) assert not opt.inspect.is_final(ProtoX) assert not opt.inspect.is_final(ProtoRuntime)