Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions stdlib/2/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Container(Protocol[_T_co]):
@abstractmethod
def __contains__(self, x: object) -> bool: ...

class Sequence(Iterable[_T_co], Container[_T_co], Sized, Reversible[_T_co], Generic[_T_co]):
class Sequence(Iterable[_T_co], Container[_T_co], Reversible[_T_co], Generic[_T_co]):
@overload
@abstractmethod
def __getitem__(self, i: int) -> _T_co: ...
Expand All @@ -157,6 +157,9 @@ class Sequence(Iterable[_T_co], Container[_T_co], Sized, Reversible[_T_co], Gene
def __contains__(self, x: object) -> bool: ...
def __iter__(self) -> Iterator[_T_co]: ...
def __reversed__(self) -> Iterator[_T_co]: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

class MutableSequence(Sequence[_T], Generic[_T]):
@abstractmethod
Expand Down Expand Up @@ -187,7 +190,7 @@ class MutableSequence(Sequence[_T], Generic[_T]):
def remove(self, object: _T) -> None: ...
def __iadd__(self, x: Iterable[_T]) -> MutableSequence[_T]: ...

class AbstractSet(Sized, Iterable[_T_co], Container[_T_co], Generic[_T_co]):
class AbstractSet(Iterable[_T_co], Container[_T_co], Generic[_T_co]):
@abstractmethod
def __contains__(self, x: object) -> bool: ...
# Mixin methods
Expand All @@ -201,6 +204,10 @@ class AbstractSet(Sized, Iterable[_T_co], Container[_T_co], Generic[_T_co]):
def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
# TODO: argument can be any container?
def isdisjoint(self, s: AbstractSet[Any]) -> bool: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...


class MutableSet(AbstractSet[_T], Generic[_T]):
@abstractmethod
Expand All @@ -216,7 +223,10 @@ class MutableSet(AbstractSet[_T], Generic[_T]):
def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...

class MappingView(Sized):
class MappingView:
def __len__(self) -> int: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

class ItemsView(AbstractSet[Tuple[_KT_co, _VT_co]], MappingView, Generic[_KT_co, _VT_co]):
Expand All @@ -238,7 +248,7 @@ class ContextManager(Protocol[_T_co]):
exc_value: Optional[BaseException],
traceback: Optional[TracebackType]) -> Optional[bool]: ...

class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
class Mapping(Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
# TODO: We wish the key type could also be covariant, but that doesn't work,
# see discussion in https: //github.com/python/typing/pull/273.
@abstractmethod
Expand All @@ -256,6 +266,9 @@ class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
def itervalues(self) -> Iterator[_VT_co]: ...
def iteritems(self) -> Iterator[Tuple[_KT, _VT_co]]: ...
def __contains__(self, o: object) -> bool: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
@abstractmethod
Expand Down
24 changes: 20 additions & 4 deletions stdlib/3/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,20 @@ class Container(Protocol[_T_co]):

if sys.version_info >= (3, 6):
@runtime
class Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

_Collection = Collection
else:
@runtime
class _Collection(Sized, Iterable[_T_co], Container[_T_co], Protocol[_T_co]): ...
class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

class Sequence(_Collection[_T_co], Reversible[_T_co], Generic[_T_co]):
class Sequence(Iterable[_T_co], Container[_T_co], Reversible[_T_co], Generic[_T_co]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe preserve _Collection here, as otherwise mypy could infer a less precise type for some type joins? Then you could leave out __len__ as it would be inherited.

@overload
@abstractmethod
def __getitem__(self, i: int) -> _T_co: ...
Expand All @@ -257,6 +264,9 @@ class Sequence(_Collection[_T_co], Reversible[_T_co], Generic[_T_co]):
def __contains__(self, x: object) -> bool: ...
def __iter__(self) -> Iterator[_T_co]: ...
def __reversed__(self) -> Iterator[_T_co]: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

class MutableSequence(Sequence[_T], Generic[_T]):
@abstractmethod
Expand Down Expand Up @@ -302,6 +312,9 @@ class AbstractSet(_Collection[_T_co], Generic[_T_co]):
def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ...
# TODO: Argument can be a more general ABC?
def isdisjoint(self, s: AbstractSet[Any]) -> bool: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

class MutableSet(AbstractSet[_T], Generic[_T]):
@abstractmethod
Expand All @@ -317,7 +330,10 @@ class MutableSet(AbstractSet[_T], Generic[_T]):
def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...

class MappingView(Sized):
class MappingView:
def __len__(self) -> int: ...
# Implement Sized (but don't have it as a base class).
@abstractmethod
def __len__(self) -> int: ...

class ItemsView(AbstractSet[Tuple[_KT_co, _VT_co]], MappingView, Generic[_KT_co, _VT_co]):
Expand Down