Skip to content

Collections that inherit from dict should should override copy() #1856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion stdlib/2/collections.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ class deque(Sized, Iterable[_T], Reversible[_T], Generic[_T]):
def __contains__(self, o: _T) -> bool: ...
def __reversed__(self) -> Iterator[_T]: ...

_CounterT = TypeVar('_CounterT', bound=Counter)

class Counter(Dict[_T, int], Generic[_T]):
@overload
def __init__(self, **kwargs: int) -> None: ...
@overload
def __init__(self, mapping: Mapping[_T, int]) -> None: ...
@overload
def __init__(self, iterable: Iterable[_T]) -> None: ...
def copy(self: _CounterT) -> _CounterT: ...
def elements(self) -> Iterator[_T]: ...
def most_common(self, n: Optional[int] = ...) -> List[Tuple[_T, int]]: ...
@overload
Expand Down Expand Up @@ -93,10 +96,14 @@ class Counter(Dict[_T, int], Generic[_T]):
def __iand__(self, other: Counter[_T]) -> Counter[_T]: ...
def __ior__(self, other: Counter[_T]) -> Counter[_T]: ...

_OrderedDictT = TypeVar('_OrderedDictT', bound=OrderedDict)
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I prefer shorter names for type variables (also this is what PEP 8 currently recommends).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed the convention already established in the file by _UserDictT, _UserListT, and _UserStringT. I think it makes sense to keep it consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you really hate it as is I can do _OdictT. your call.


class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
def popitem(self, last: bool = ...) -> Tuple[_KT, _VT]: ...
def copy(self: _OrderedDictT) -> _OrderedDictT: ...
def __reversed__(self) -> Iterator[_KT]: ...
def __copy__(self) -> OrderedDict[_KT, _VT]: ...

_DefaultDictT = TypeVar('_DefaultDictT', bound=defaultdict)

class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]):
default_factory = ... # type: Callable[[], _VT]
Expand All @@ -111,3 +118,4 @@ class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]):
def __init__(self, default_factory: Optional[Callable[[], _VT]],
iterable: Iterable[Tuple[_KT, _VT]]) -> None: ...
def __missing__(self, key: _KT) -> _VT: ...
def copy(self: _DefaultDictT) -> _DefaultDictT: ...
11 changes: 8 additions & 3 deletions stdlib/3/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class deque(MutableSequence[_T], Generic[_T]):
def __mul__(self, other: int) -> deque[_T]: ...
def __imul__(self, other: int) -> None: ...

_CounterT = TypeVar('_CounterT', bound=Counter)

class Counter(Dict[_T, int], Generic[_T]):
@overload
Expand All @@ -244,7 +245,7 @@ class Counter(Dict[_T, int], Generic[_T]):
def __init__(self, mapping: Mapping[_T, int]) -> None: ...
@overload
def __init__(self, iterable: Iterable[_T]) -> None: ...

def copy(self: _CounterT) -> _CounterT: ...
def elements(self) -> Iterator[_T]: ...

def most_common(self, n: Optional[int] = ...) -> List[Tuple[_T, int]]: ...
Expand Down Expand Up @@ -277,11 +278,15 @@ class Counter(Dict[_T, int], Generic[_T]):
def __iand__(self, other: Counter[_T]) -> Counter[_T]: ...
def __ior__(self, other: Counter[_T]) -> Counter[_T]: ...

_OrderedDictT = TypeVar('_OrderedDictT', bound=OrderedDict)

class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
def popitem(self, last: bool = ...) -> Tuple[_KT, _VT]: ...
def move_to_end(self, key: _KT, last: bool = ...) -> None: ...
def copy(self: _OrderedDictT) -> _OrderedDictT: ...
def __reversed__(self) -> Iterator[_KT]: ...
def __copy__(self) -> OrderedDict[_KT, _VT]: ...

_DefaultDictT = TypeVar('_DefaultDictT', bound=defaultdict)

class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]):
default_factory = ... # type: Callable[[], _VT]
Expand All @@ -296,9 +301,9 @@ class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]):
@overload
def __init__(self, default_factory: Optional[Callable[[], _VT]],
iterable: Iterable[Tuple[_KT, _VT]]) -> None: ...

def __missing__(self, key: _KT) -> _VT: ...
# TODO __reversed__
def copy(self: _DefaultDictT) -> _DefaultDictT: ...

if sys.version_info >= (3, 3):
class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
Expand Down