Skip to content

Let MutableMapping.pop take a different default type. #1044

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 1 commit into from
Mar 20, 2017

Conversation

euresti
Copy link
Contributor

@euresti euresti commented Mar 20, 2017

This is particularly useful for the idiom d.pop(k, None) to remove an item if it exists.

Fixes #278

This is particularly useful for the idiom `d.pop(k, None)` to remove an item if it exists.

Fixes python#278
@@ -610,7 +610,6 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __init__(self, iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
def clear(self) -> None: ...
def copy(self) -> Dict[_KT, _VT]: ...
def pop(self, k: _KT, default: _VT = None) -> _VT: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems a bit arbitrary to remove pop but to leave clear(), etc. We should prune more methods in a follow-up diff.

@ambv ambv merged commit 2fdcd2e into python:master Mar 20, 2017
@ambv
Copy link
Contributor

ambv commented Mar 20, 2017

This is implemented paralleling what get() does.

@JelleZijlstra
Copy link
Member

This makes .pop() return a Union, shouldn't we use an overload instead to parallel .get()? With a Union users will have to isinstance check every usage.

@euresti
Copy link
Contributor Author

euresti commented Mar 20, 2017

I think Union[_T, _T] automatically resolves to _T At least that's what reveal_type show.

from typing import TypeVar, Union

_T = TypeVar('_T')

def pop(var: Union[int, _T]) -> Union[int, _T]:
    return var

reveal_type(pop(7))  # Revealed type is 'builtins.int'
reveal_type(pop('foo')) # Revealed type is 'Union[builtins.int, builtins.str*]'
reveal_type(pop(None)) # Revealed type is 'Union[builtins.int, builtins.None]'

@euresti
Copy link
Contributor Author

euresti commented Mar 20, 2017

Oh wait. pop has a default value. Let me look.

@euresti euresti deleted the map_pop branch March 20, 2017 21:18
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