From 97c7a38180923e8c8775962a92d83f2964aba89d Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Tue, 3 May 2022 16:53:42 -0500 Subject: [PATCH] Give up any hope of using __slots__ with Protocols for now python/mypy#11013 is wreaking havoc with them. --- README.md | 2 -- docs/whytho.md | 5 +--- numerary/types.py | 72 --------------------------------------------- tests/numberwang.py | 8 ++--- tests/test_types.py | 4 +-- 5 files changed, 6 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index 1a60c66..5ffb17f 100644 --- a/README.md +++ b/README.md @@ -186,12 +186,10 @@ False ``` python >>> from abc import abstractmethod ->>> from beartype.typing import Any >>> from numerary.types import CachingProtocolMeta, Protocol, runtime_checkable >>> @runtime_checkable ... class MySupportsOne(Protocol, metaclass=CachingProtocolMeta): -... __slots__: Any = () ... @abstractmethod ... def one(self) -> int: ... pass diff --git a/docs/whytho.md b/docs/whytho.md index fbc0f1f..c315434 100644 --- a/docs/whytho.md +++ b/docs/whytho.md @@ -175,11 +175,9 @@ Can we imitate those? ... from typing import Protocol, runtime_checkable ... except ImportError: ... from typing_extensions import Protocol, runtime_checkable # type: ignore [misc] ->>> from typing import Any >>> @runtime_checkable ... class SupportsNumeratorDenominator(Protocol): -... __slots__: Any = () ... @property ... def numerator(self) -> int: ... pass @@ -284,11 +282,10 @@ That seems as good a place as any to tackle next. ... from typing_extensions import Protocol, runtime_checkable # type: ignore [misc] >>> from abc import abstractmethod ->>> from typing import Any, Iterable, Union +>>> from typing import Any >>> @runtime_checkable ... class SupportsRealComparisons(Protocol): -... __slots__: Any = () ... @abstractmethod ... def __lt__(self, other: Any) -> bool: ... pass diff --git a/numerary/types.py b/numerary/types.py index d6b342c..9d11259 100644 --- a/numerary/types.py +++ b/numerary/types.py @@ -75,8 +75,6 @@ class SupportsAbs( r""" A derivative of ``beartype.typing.SupportsAbs`` with an override-able cache. """ - # See - __slots__: Any = () _assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsAbs) @@ -91,8 +89,6 @@ class SupportsComplex( r""" A derivative of ``beartype.typing.SupportsComplex`` with an override-able cache. """ - # See - __slots__: Any = () _assert_isinstance(Decimal, Fraction, target_t=SupportsComplex) @@ -107,8 +103,6 @@ class SupportsFloat( r""" A derivative of ``beartype.typing.SupportsFloat`` with an override-able cache. """ - # See - __slots__: Any = () _assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsFloat) @@ -123,8 +117,6 @@ class SupportsInt( r""" A derivative of ``beartype.typing.SupportsInt`` with an override-able cache. """ - # See - __slots__: Any = () _assert_isinstance(int, float, bool, target_t=SupportsInt) @@ -139,8 +131,6 @@ class SupportsIndex( r""" A derivative of ``beartype.typing.SupportsIndex`` with an override-able cache. """ - # See - __slots__: Any = () _assert_isinstance(int, bool, target_t=SupportsIndex) @@ -156,8 +146,6 @@ class SupportsRound( r""" A derivative of ``beartype.typing.SupportsRound`` with an override-able cache. """ - # See - __slots__: Any = () _assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsRound) @@ -169,8 +157,6 @@ class _SupportsConjugate(_Protocol): The non-caching version of [``SupportsConjugate``][numerary.types.SupportsConjugate]. """ - # See - __slots__: Any = () @abstractmethod def conjugate(self) -> Any: @@ -220,8 +206,6 @@ class SupportsConjugate( ``` """ - # See - __slots__: Any = () _assert_isinstance( @@ -234,8 +218,6 @@ class _SupportsRealImag(_Protocol): r""" The non-caching version of [``SupportsRealImag``][numerary.types.SupportsRealImag]. """ - # See - __slots__: Any = () @property def real(self) -> Any: @@ -285,8 +267,6 @@ class SupportsRealImag( ``` """ - # See - __slots__: Any = () _assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsRealImag) @@ -298,8 +278,6 @@ class _SupportsRealImagAsMethod(_Protocol): The non-caching version of [``SupportsRealImagAsMethod``][numerary.types.SupportsRealImagAsMethod]. """ - # See - __slots__: Any = () @abstractmethod def as_real_imag(self) -> Tuple[Any, Any]: @@ -343,8 +321,6 @@ class SupportsRealImagAsMethod( ``` """ - # See - __slots__: Any = () # See @@ -370,8 +346,6 @@ class _SupportsTrunc(_Protocol): r""" The non-caching version of [``SupportsTrunc``][numerary.types.SupportsTrunc]. """ - # See - __slots__: Any = () @abstractmethod def __trunc__(self) -> int: @@ -423,8 +397,6 @@ class SupportsTrunc( ``` """ - # See - __slots__: Any = () _assert_isinstance(int, bool, float, Decimal, Fraction, target_t=SupportsTrunc) @@ -436,8 +408,6 @@ class _SupportsFloorCeil(_Protocol): The non-caching version of [``SupportsFloorCeil``][numerary.types.SupportsFloorCeil]. """ - # See - __slots__: Any = () @abstractmethod def __floor__(self) -> int: @@ -504,8 +474,6 @@ class SupportsFloorCeil( ``` """ - # See - __slots__: Any = () # Prior to Python 3.9, floats didn't have an explicit __floor__ method; it was @@ -525,8 +493,6 @@ class _SupportsDivmod( r""" The non-caching version of [``SupportsDivmod``][numerary.types.SupportsDivmod]. """ - # See - __slots__: Any = () @abstractmethod def __divmod__(self, other: Any) -> Tuple[_T_co, _T_co]: @@ -584,8 +550,6 @@ class SupportsDivmod( ``` """ - # See - __slots__: Any = () # complex defines these methods, but only to raise exceptions @@ -599,8 +563,6 @@ class _SupportsNumeratorDenominator(_Protocol): The non-caching version of [``SupportsNumeratorDenominator``][numerary.types.SupportsNumeratorDenominator]. """ - # See - __slots__: Any = () @property def numerator(self) -> int: @@ -657,8 +619,6 @@ class SupportsNumeratorDenominator( ``` """ - # See - __slots__: Any = () _assert_isinstance(int, bool, Fraction, target_t=SupportsNumeratorDenominator) @@ -670,8 +630,6 @@ class _SupportsNumeratorDenominatorMethods(_Protocol): The non-caching version of [``SupportsNumeratorDenominatorMethods``][numerary.types.SupportsNumeratorDenominatorMethods]. """ - # See - __slots__: Any = () @abstractmethod def numerator(self) -> SupportsInt: @@ -698,8 +656,6 @@ class SupportsNumeratorDenominatorMethods( See also the [``numerator``][numerary.types.numerator] and [``denominator``][numerary.types.denominator] helper functions. """ - # See - __slots__: Any = () # See @@ -729,8 +685,6 @@ class _SupportsComplexOps( The non-caching version of [``SupportsComplexOps``][numerary.types.SupportsComplexOps]. """ - # See - __slots__: Any = () @abstractmethod def __add__(self, other: Any) -> _T_co: @@ -818,8 +772,6 @@ class SupportsComplexOps( ``` """ - # See - __slots__: Any = () _assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsComplexOps) @@ -831,8 +783,6 @@ class _SupportsComplexPow(_Protocol): The non-caching version of [``SupportsComplexPow``][numerary.types.SupportsComplexPow]. """ - # See - __slots__: Any = () @abstractmethod def __pow__(self, exponent: Any) -> Any: @@ -887,8 +837,6 @@ class SupportsComplexPow( ``` """ - # See - __slots__: Any = () _assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsComplexPow) @@ -902,8 +850,6 @@ class _SupportsRealOps( r""" The non-caching version of [``SupportsRealOps``][numerary.types.SupportsRealOps]. """ - # See - __slots__: Any = () @abstractmethod def __lt__(self, other: Any) -> bool: @@ -983,8 +929,6 @@ class SupportsRealOps( ``` """ - # See - __slots__: Any = () # complex defines these methods, but only to raise exceptions @@ -1001,8 +945,6 @@ class _SupportsIntegralOps( The non-caching version of [``SupportsIntegralOps``][numerary.types.SupportsIntegralOps]. """ - # See - __slots__: Any = () @abstractmethod def __lshift__(self, other: Any) -> _T_co: @@ -1090,8 +1032,6 @@ class SupportsIntegralOps( ``` """ - # See - __slots__: Any = () _assert_isinstance(int, bool, target_t=SupportsIntegralOps) @@ -1103,8 +1043,6 @@ class _SupportsIntegralPow(_Protocol): The non-caching version of [``SupportsIntegralPow``][numerary.types.SupportsIntegralPow]. """ - # See - __slots__: Any = () @abstractmethod def __pow__(self, exponent: Any, modulus: Optional[Any] = None) -> Any: @@ -1156,8 +1094,6 @@ class SupportsIntegralPow( ``` """ - # See - __slots__: Any = () _assert_isinstance(int, bool, target_t=SupportsIntegralPow) @@ -1215,8 +1151,6 @@ class RealLike( * [``SupportsFloorCeil``][numerary.types.SupportsFloorCeil] * [``SupportsDivmod``][numerary.types.SupportsDivmod] """ - # See - __slots__: Any = () # Must be able to instantiate it @abstractmethod @@ -1286,8 +1220,6 @@ class RationalLike( * [``SupportsFloorCeil``][numerary.types.SupportsFloorCeil] * [``SupportsDivmod``][numerary.types.SupportsDivmod] """ - # See - __slots__: Any = () # Must be able to instantiate it @abstractmethod @@ -1350,8 +1282,6 @@ class RationalLikeMethods( See also the [``numerator``][numerary.types.numerator] and [``denominator``][numerary.types.denominator] helper functions. """ - # See - __slots__: Any = () # Must be able to instantiate it @abstractmethod @@ -1445,8 +1375,6 @@ class IntegralLike( * [``SupportsDivmod``][numerary.types.SupportsDivmod] * [``SupportsNumeratorDenominator``][numerary.types.SupportsNumeratorDenominator] """ - # See - __slots__: Any = () # Must be able to instantiate it @abstractmethod diff --git a/tests/numberwang.py b/tests/numberwang.py index 3af1fe5..22f81f1 100644 --- a/tests/numberwang.py +++ b/tests/numberwang.py @@ -36,9 +36,9 @@ __truediv__, __xor__, ) -from typing import Any, overload +from typing import overload -from beartype.typing import Iterable, Optional, Union +from beartype.typing import Any, Optional, Union from numerary.bt import beartype from numerary.types import SupportsInt @@ -82,7 +82,7 @@ class TestIntFlag(IntFlag): class NumberwangBase: - __slots__: Union[str, Iterable[str]] = ("val",) + __slots__: Any = ("val",) @beartype def __init__(self, arg: SupportsInt = 0): @@ -688,7 +688,7 @@ class NumberwangDerived(NumberwangBase, Integral): # type: ignore [misc] class Wangernumb: - __slots__: Union[str, Iterable[str]] = ("val",) + __slots__: Any = ("val",) @beartype def __init__(self, arg: _RealT = 0): diff --git a/tests/test_types.py b/tests/test_types.py index 2299f56..c04d594 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -13,7 +13,7 @@ import pytest from beartype import beartype from beartype.roar import BeartypeException -from beartype.typing import Any, Tuple +from beartype.typing import Tuple from numerary import IntegralLike, RealLike from numerary.types import CachingProtocolMeta, Protocol, runtime_checkable @@ -29,8 +29,6 @@ class SupportsOne( Protocol, metaclass=CachingProtocolMeta, ): - __slots__: Any = () - @abstractmethod def one(self) -> int: pass