Skip to content

Commit

Permalink
Merge pull request python#153 from o11c/typing-slots
Browse files Browse the repository at this point in the history
Use `__slots__` in `typing` module.
  • Loading branch information
gvanrossum committed Aug 4, 2015
2 parents 10da00d + 764588c commit ca889d1
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions prototyping/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ def __repr__(self):
class Final:
"""Mix-in class to prevent instantiation."""

__slots__ = ()

def __new__(self, *args, **kwds):
raise TypeError("Cannot instantiate %r" % self.__class__)

Expand Down Expand Up @@ -204,6 +206,8 @@ class _TypeAlias:
False.
"""

__slots__ = ('name', 'type_var', 'impl_type', 'type_checker')

def __new__(cls, *args, **kwds):
"""Constructor.
Expand Down Expand Up @@ -341,6 +345,8 @@ class Any(Final, metaclass=AnyMeta, _root=True):
- As a special case, Any and object are subclasses of each other.
"""

__slots__ = ()


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

__slots__ = ()


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

__slots__ = ()


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

__slots__ = ()


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

__slots__ = ()

def __new__(cls, *args, **kwds):
next_in_mro = object
# Look for the last occurrence of Generic or Generic[...].
Expand Down Expand Up @@ -1208,6 +1222,7 @@ def _get_protocol_attrs(self):
attr != '__abstractmethods__' and
attr != '_is_protocol' and
attr != '__dict__' and
attr != '__slots__' and
attr != '_get_protocol_attrs' and
attr != '__parameters__' and
attr != '__origin__' and
Expand All @@ -1225,6 +1240,8 @@ class _Protocol(metaclass=_ProtocolMeta):
such as Hashable).
"""

__slots__ = ()

_is_protocol = True


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


class Iterable(Generic[T_co], extra=collections_abc.Iterable):
pass
__slots__ = ()


class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
pass
__slots__ = ()


class SupportsInt(_Protocol):
__slots__ = ()

@abstractmethod
def __int__(self) -> int:
pass


class SupportsFloat(_Protocol):
__slots__ = ()

@abstractmethod
def __float__(self) -> float:
pass


class SupportsComplex(_Protocol):
__slots__ = ()

@abstractmethod
def __complex__(self) -> complex:
pass


class SupportsBytes(_Protocol):
__slots__ = ()

@abstractmethod
def __bytes__(self) -> bytes:
pass


class SupportsAbs(_Protocol[T]):
__slots__ = ()

@abstractmethod
def __abs__(self) -> T:
pass


class SupportsRound(_Protocol[T]):
__slots__ = ()

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


class Reversible(_Protocol[T]):
__slots__ = ()

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


class Container(Generic[T_co], extra=collections_abc.Container):
pass
__slots__ = ()


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


class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
__slots__ = ()

def __new__(cls, *args, **kwds):
if _geqv(cls, FrozenSet):
Expand Down Expand Up @@ -1416,6 +1441,7 @@ def __new__(cls, *args, **kwds):

class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
extra=_G_base):
__slots__ = ()

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

__slots__ = ()

@abstractproperty
def mode(self) -> str:
pass
Expand Down Expand Up @@ -1543,6 +1571,8 @@ def __exit__(self, type, value, traceback) -> None:
class BinaryIO(IO[bytes]):
"""Typed version of the return of open() in binary mode."""

__slots__ = ()

@abstractmethod
def write(self, s: Union[bytes, bytearray]) -> int:
pass
Expand All @@ -1555,6 +1585,8 @@ def __enter__(self) -> 'BinaryIO':
class TextIO(IO[str]):
"""Typed version of the return of open() in text mode."""

__slots__ = ()

@abstractproperty
def buffer(self) -> BinaryIO:
pass
Expand Down

0 comments on commit ca889d1

Please sign in to comment.