Skip to content

Commit

Permalink
Give up any hope of using __slots__ with Protocols for now
Browse files Browse the repository at this point in the history
python/mypy#11013 is wreaking havoc with them.
  • Loading branch information
posita committed May 3, 2022
1 parent 8cdf2c4 commit 97c7a38
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 85 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions docs/whytho.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
72 changes: 0 additions & 72 deletions numerary/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class SupportsAbs(
r"""
A derivative of ``beartype.typing.SupportsAbs`` with an override-able cache.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsAbs)
Expand All @@ -91,8 +89,6 @@ class SupportsComplex(
r"""
A derivative of ``beartype.typing.SupportsComplex`` with an override-able cache.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(Decimal, Fraction, target_t=SupportsComplex)
Expand All @@ -107,8 +103,6 @@ class SupportsFloat(
r"""
A derivative of ``beartype.typing.SupportsFloat`` with an override-able cache.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsFloat)
Expand All @@ -123,8 +117,6 @@ class SupportsInt(
r"""
A derivative of ``beartype.typing.SupportsInt`` with an override-able cache.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, float, bool, target_t=SupportsInt)
Expand All @@ -139,8 +131,6 @@ class SupportsIndex(
r"""
A derivative of ``beartype.typing.SupportsIndex`` with an override-able cache.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, bool, target_t=SupportsIndex)
Expand All @@ -156,8 +146,6 @@ class SupportsRound(
r"""
A derivative of ``beartype.typing.SupportsRound`` with an override-able cache.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsRound)
Expand All @@ -169,8 +157,6 @@ class _SupportsConjugate(_Protocol):
The non-caching version of
[``SupportsConjugate``][numerary.types.SupportsConjugate].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def conjugate(self) -> Any:
Expand Down Expand Up @@ -220,8 +206,6 @@ class SupportsConjugate(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(
Expand All @@ -234,8 +218,6 @@ class _SupportsRealImag(_Protocol):
r"""
The non-caching version of [``SupportsRealImag``][numerary.types.SupportsRealImag].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@property
def real(self) -> Any:
Expand Down Expand Up @@ -285,8 +267,6 @@ class SupportsRealImag(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsRealImag)
Expand All @@ -298,8 +278,6 @@ class _SupportsRealImagAsMethod(_Protocol):
The non-caching version of
[``SupportsRealImagAsMethod``][numerary.types.SupportsRealImagAsMethod].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def as_real_imag(self) -> Tuple[Any, Any]:
Expand Down Expand Up @@ -343,8 +321,6 @@ class SupportsRealImagAsMethod(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


# See <https://github.com/mkdocstrings/mkdocstrings/issues/333>
Expand All @@ -370,8 +346,6 @@ class _SupportsTrunc(_Protocol):
r"""
The non-caching version of [``SupportsTrunc``][numerary.types.SupportsTrunc].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __trunc__(self) -> int:
Expand Down Expand Up @@ -423,8 +397,6 @@ class SupportsTrunc(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, bool, float, Decimal, Fraction, target_t=SupportsTrunc)
Expand All @@ -436,8 +408,6 @@ class _SupportsFloorCeil(_Protocol):
The non-caching version of
[``SupportsFloorCeil``][numerary.types.SupportsFloorCeil].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __floor__(self) -> int:
Expand Down Expand Up @@ -504,8 +474,6 @@ class SupportsFloorCeil(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


# Prior to Python 3.9, floats didn't have an explicit __floor__ method; it was
Expand All @@ -525,8 +493,6 @@ class _SupportsDivmod(
r"""
The non-caching version of [``SupportsDivmod``][numerary.types.SupportsDivmod].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __divmod__(self, other: Any) -> Tuple[_T_co, _T_co]:
Expand Down Expand Up @@ -584,8 +550,6 @@ class SupportsDivmod(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


# complex defines these methods, but only to raise exceptions
Expand All @@ -599,8 +563,6 @@ class _SupportsNumeratorDenominator(_Protocol):
The non-caching version of
[``SupportsNumeratorDenominator``][numerary.types.SupportsNumeratorDenominator].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@property
def numerator(self) -> int:
Expand Down Expand Up @@ -657,8 +619,6 @@ class SupportsNumeratorDenominator(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, bool, Fraction, target_t=SupportsNumeratorDenominator)
Expand All @@ -670,8 +630,6 @@ class _SupportsNumeratorDenominatorMethods(_Protocol):
The non-caching version of
[``SupportsNumeratorDenominatorMethods``][numerary.types.SupportsNumeratorDenominatorMethods].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def numerator(self) -> SupportsInt:
Expand All @@ -698,8 +656,6 @@ class SupportsNumeratorDenominatorMethods(
See also the [``numerator``][numerary.types.numerator] and
[``denominator``][numerary.types.denominator] helper functions.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


# See <https://github.com/mkdocstrings/mkdocstrings/issues/333>
Expand Down Expand Up @@ -729,8 +685,6 @@ class _SupportsComplexOps(
The non-caching version of
[``SupportsComplexOps``][numerary.types.SupportsComplexOps].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __add__(self, other: Any) -> _T_co:
Expand Down Expand Up @@ -818,8 +772,6 @@ class SupportsComplexOps(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsComplexOps)
Expand All @@ -831,8 +783,6 @@ class _SupportsComplexPow(_Protocol):
The non-caching version of
[``SupportsComplexPow``][numerary.types.SupportsComplexPow].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __pow__(self, exponent: Any) -> Any:
Expand Down Expand Up @@ -887,8 +837,6 @@ class SupportsComplexPow(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsComplexPow)
Expand All @@ -902,8 +850,6 @@ class _SupportsRealOps(
r"""
The non-caching version of [``SupportsRealOps``][numerary.types.SupportsRealOps].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __lt__(self, other: Any) -> bool:
Expand Down Expand Up @@ -983,8 +929,6 @@ class SupportsRealOps(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


# complex defines these methods, but only to raise exceptions
Expand All @@ -1001,8 +945,6 @@ class _SupportsIntegralOps(
The non-caching version of
[``SupportsIntegralOps``][numerary.types.SupportsIntegralOps].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __lshift__(self, other: Any) -> _T_co:
Expand Down Expand Up @@ -1090,8 +1032,6 @@ class SupportsIntegralOps(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, bool, target_t=SupportsIntegralOps)
Expand All @@ -1103,8 +1043,6 @@ class _SupportsIntegralPow(_Protocol):
The non-caching version of
[``SupportsIntegralPow``][numerary.types.SupportsIntegralPow].
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

@abstractmethod
def __pow__(self, exponent: Any, modulus: Optional[Any] = None) -> Any:
Expand Down Expand Up @@ -1156,8 +1094,6 @@ class SupportsIntegralPow(
```
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()


_assert_isinstance(int, bool, target_t=SupportsIntegralPow)
Expand Down Expand Up @@ -1215,8 +1151,6 @@ class RealLike(
* [``SupportsFloorCeil``][numerary.types.SupportsFloorCeil]
* [``SupportsDivmod``][numerary.types.SupportsDivmod]
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

# Must be able to instantiate it
@abstractmethod
Expand Down Expand Up @@ -1286,8 +1220,6 @@ class RationalLike(
* [``SupportsFloorCeil``][numerary.types.SupportsFloorCeil]
* [``SupportsDivmod``][numerary.types.SupportsDivmod]
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

# Must be able to instantiate it
@abstractmethod
Expand Down Expand Up @@ -1350,8 +1282,6 @@ class RationalLikeMethods(
See also the [``numerator``][numerary.types.numerator] and
[``denominator``][numerary.types.denominator] helper functions.
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

# Must be able to instantiate it
@abstractmethod
Expand Down Expand Up @@ -1445,8 +1375,6 @@ class IntegralLike(
* [``SupportsDivmod``][numerary.types.SupportsDivmod]
* [``SupportsNumeratorDenominator``][numerary.types.SupportsNumeratorDenominator]
"""
# See <https://github.com/python/mypy/issues/11013>
__slots__: Any = ()

# Must be able to instantiate it
@abstractmethod
Expand Down
8 changes: 4 additions & 4 deletions tests/numberwang.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
Loading

0 comments on commit 97c7a38

Please sign in to comment.