Skip to content

Commit ca889d1

Browse files
committed
Merge pull request python#153 from o11c/typing-slots
Use `__slots__` in `typing` module.
2 parents 10da00d + 764588c commit ca889d1

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

‎prototyping/typing.py‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def __repr__(self):
128128
class Final:
129129
"""Mix-in class to prevent instantiation."""
130130

131+
__slots__ = ()
132+
131133
def __new__(self, *args, **kwds):
132134
raise TypeError("Cannot instantiate %r" % self.__class__)
133135

@@ -204,6 +206,8 @@ class _TypeAlias:
204206
False.
205207
"""
206208

209+
__slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
210+
207211
def __new__(cls, *args, **kwds):
208212
"""Constructor.
209213
@@ -341,6 +345,8 @@ class Any(Final, metaclass=AnyMeta, _root=True):
341345
- As a special case, Any and object are subclasses of each other.
342346
"""
343347

348+
__slots__ = ()
349+
344350

345351
class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
346352
"""Type variable.
@@ -635,6 +641,8 @@ class Optional(Final, metaclass=OptionalMeta, _root=True):
635641
Optional[X] is equivalent to Union[X, type(None)].
636642
"""
637643

644+
__slots__ = ()
645+
638646

639647
class TupleMeta(TypingMeta):
640648
"""Metaclass for Tuple."""
@@ -734,6 +742,8 @@ class Tuple(Final, metaclass=TupleMeta, _root=True):
734742
To specify a variable-length tuple of homogeneous type, use Sequence[T].
735743
"""
736744

745+
__slots__ = ()
746+
737747

738748
class CallableMeta(TypingMeta):
739749
"""Metaclass for Callable."""
@@ -840,6 +850,8 @@ class Callable(Final, metaclass=CallableMeta, _root=True):
840850
such function types are rarely used as callback types.
841851
"""
842852

853+
__slots__ = ()
854+
843855

844856
def _gorg(a):
845857
"""Return the farthest origin of a generic class."""
@@ -1042,6 +1054,8 @@ def lookup_name(mapping: Mapping[X, Y], key: X, default: Y) -> Y:
10421054
# Same body as above.
10431055
"""
10441056

1057+
__slots__ = ()
1058+
10451059
def __new__(cls, *args, **kwds):
10461060
next_in_mro = object
10471061
# Look for the last occurrence of Generic or Generic[...].
@@ -1208,6 +1222,7 @@ def _get_protocol_attrs(self):
12081222
attr != '__abstractmethods__' and
12091223
attr != '_is_protocol' and
12101224
attr != '__dict__' and
1225+
attr != '__slots__' and
12111226
attr != '_get_protocol_attrs' and
12121227
attr != '__parameters__' and
12131228
attr != '__origin__' and
@@ -1225,6 +1240,8 @@ class _Protocol(metaclass=_ProtocolMeta):
12251240
such as Hashable).
12261241
"""
12271242

1243+
__slots__ = ()
1244+
12281245
_is_protocol = True
12291246

12301247

@@ -1235,56 +1252,63 @@ class _Protocol(metaclass=_ProtocolMeta):
12351252

12361253

12371254
class Iterable(Generic[T_co], extra=collections_abc.Iterable):
1238-
pass
1255+
__slots__ = ()
12391256

12401257

12411258
class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
1242-
pass
1259+
__slots__ = ()
12431260

12441261

12451262
class SupportsInt(_Protocol):
1263+
__slots__ = ()
12461264

12471265
@abstractmethod
12481266
def __int__(self) -> int:
12491267
pass
12501268

12511269

12521270
class SupportsFloat(_Protocol):
1271+
__slots__ = ()
12531272

12541273
@abstractmethod
12551274
def __float__(self) -> float:
12561275
pass
12571276

12581277

12591278
class SupportsComplex(_Protocol):
1279+
__slots__ = ()
12601280

12611281
@abstractmethod
12621282
def __complex__(self) -> complex:
12631283
pass
12641284

12651285

12661286
class SupportsBytes(_Protocol):
1287+
__slots__ = ()
12671288

12681289
@abstractmethod
12691290
def __bytes__(self) -> bytes:
12701291
pass
12711292

12721293

12731294
class SupportsAbs(_Protocol[T]):
1295+
__slots__ = ()
12741296

12751297
@abstractmethod
12761298
def __abs__(self) -> T:
12771299
pass
12781300

12791301

12801302
class SupportsRound(_Protocol[T]):
1303+
__slots__ = ()
12811304

12821305
@abstractmethod
12831306
def __round__(self, ndigits: int = 0) -> T:
12841307
pass
12851308

12861309

12871310
class Reversible(_Protocol[T]):
1311+
__slots__ = ()
12881312

12891313
@abstractmethod
12901314
def __reversed__(self) -> 'Iterator[T]':
@@ -1295,7 +1319,7 @@ def __reversed__(self) -> 'Iterator[T]':
12951319

12961320

12971321
class Container(Generic[T_co], extra=collections_abc.Container):
1298-
pass
1322+
__slots__ = ()
12991323

13001324

13011325
# Callable was defined earlier.
@@ -1369,6 +1393,7 @@ def __subclasscheck__(self, cls):
13691393

13701394

13711395
class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
1396+
__slots__ = ()
13721397

13731398
def __new__(cls, *args, **kwds):
13741399
if _geqv(cls, FrozenSet):
@@ -1416,6 +1441,7 @@ def __new__(cls, *args, **kwds):
14161441

14171442
class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
14181443
extra=_G_base):
1444+
__slots__ = ()
14191445

14201446
def __new__(cls, *args, **kwds):
14211447
if _geqv(cls, Generator):
@@ -1459,6 +1485,8 @@ class IO(Generic[AnyStr]):
14591485
way to track the other distinctions in the type system.
14601486
"""
14611487

1488+
__slots__ = ()
1489+
14621490
@abstractproperty
14631491
def mode(self) -> str:
14641492
pass
@@ -1543,6 +1571,8 @@ def __exit__(self, type, value, traceback) -> None:
15431571
class BinaryIO(IO[bytes]):
15441572
"""Typed version of the return of open() in binary mode."""
15451573

1574+
__slots__ = ()
1575+
15461576
@abstractmethod
15471577
def write(self, s: Union[bytes, bytearray]) -> int:
15481578
pass
@@ -1555,6 +1585,8 @@ def __enter__(self) -> 'BinaryIO':
15551585
class TextIO(IO[str]):
15561586
"""Typed version of the return of open() in text mode."""
15571587

1588+
__slots__ = ()
1589+
15581590
@abstractproperty
15591591
def buffer(self) -> BinaryIO:
15601592
pass

0 commit comments

Comments
 (0)