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

Conversation

chadrik
Copy link
Contributor

@chadrik chadrik commented Feb 6, 2018

Solves issue #1642. A previous attempt at this, #1656, added __copy__ but omitted copy, and it did not properly use self-type.

One question regarding the previous PR: should the __copy__ method be in the stubs at all? As far as I can tell, OrderedDict does not have a __copy__ method. Was adding this a mistake or is it necessary for structural type checking of some sort?

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks! Here are some comments.

@@ -93,10 +93,13 @@ 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 __reversed__(self) -> Iterator[_KT]: ...
def __copy__(self) -> OrderedDict[_KT, _VT]: ...
def __copy__(self: _OrderedDictT) -> _OrderedDictT: ...
Copy link
Member

Choose a reason for hiding this comment

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

There is actually no Copyable protocol in typing, so I would say it is safe to remove it, if it is not defined at runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do.

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

Choose a reason for hiding this comment

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

Note that there are some bugs with self-types in generic types (in mypy). But hopefully most of them will be fixed soon (as soon as I will have time to finish a patch I am working on).

@chadrik
Copy link
Contributor Author

chadrik commented Feb 7, 2018

Ok, I removed __copy__.

I also added defaultdict.copy() as a bonus. Do you think it's necessary to have a separate PR or can we just do a twofer?

@chadrik
Copy link
Contributor Author

chadrik commented Feb 7, 2018

I guess I should add Counter too, but I'll wait for your go-ahead.

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Feb 7, 2018

You can add other copy methods in this PR using the current local TypeVar convention, but then please open a separate issue/PR on top of this to align this file with PEP 8 (this also appears in some other files, so this requires a wider discussion/exposure).

@chadrik
Copy link
Contributor Author

chadrik commented Feb 7, 2018

You can add other copy methods in this PR using the current local TypeVar convention,

Will do.

but then please open a separate issue/PR on top of this to align this file with PEP 8 (this also appears in some other files, so this requires a wider discussion/exposure).

I'm probably the wrong guy to head up that PR because I personally find short TypeVar names to be too cryptic, and I independently adopted the same convention used in collections.pyi (especially for self-types). I've always been taught to favor clarity over brevity.

@JelleZijlstra
Copy link
Member

For what it's worth, I think slightly longer TypeVar names are fine here.

Counter also inherits from dict and so needs its own copy with self-type.
@chadrik chadrik changed the title OrderedDict.copy() should return self-type Collections that inherit from dict should should override copy() Feb 7, 2018
@chadrik
Copy link
Contributor Author

chadrik commented Feb 7, 2018

This PR now includes copy() methods for OrderedDict, defaultdict, and Counter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants