Skip to content

Why is collections.ChainMap.__missing__ defined in the stub? #7188

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

Closed
AlexWaygood opened this issue Feb 13, 2022 · 5 comments
Closed

Why is collections.ChainMap.__missing__ defined in the stub? #7188

AlexWaygood opened this issue Feb 13, 2022 · 5 comments

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Feb 13, 2022

collections.ChainMap.__missing__ is defined in typeshed here:

def __missing__(self, __key: _KT) -> _VT: ...

But, at runtime, it always raises KeyError (and always has done), so there's no situation in which it would be safe to call this method (it's really just an implementation detail to give ChainMap a nicer error message for when the key isn't present in any of the underlying mappings, as far as I can tell).

The method was added to typeshed in a standalone PR (#4784)... Am I __missing__ something? Is there a good reason for it to be there?

@hauntsaninja
Copy link
Collaborator

Possibly useful for subclasses? Clearly not the most important stub, but the default good reason of "it exists at runtime" applies. Maybe @natemcmaster remembers if they had a specific use case in mind when they contributed #4784 :-)

@natemcmaster
Copy link
Contributor

Yes, I added it for subclassing. I have a type in my project that overrides ChainMap.__getitem__ and calls __missing__

@srittau
Copy link
Collaborator

srittau commented Feb 15, 2022

It seems fine to have this method, although we could consider changing the return type to NoReturn.

@srittau srittau closed this as completed Feb 15, 2022
@AlexWaygood
Copy link
Member Author

Yes, I added it for subclassing. I have a type in my project that overrides ChainMap.__getitem__ and calls __missing__

Thanks! Makes sense :)

@natemcmaster
Copy link
Contributor

natemcmaster commented Feb 15, 2022

Changing to NoReturn would break my usecase since my subclass returns a default value when __missing__ is called, similar to defaultdict. https://docs.python.org/3/library/collections.html#collections.defaultdict.missing

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

No branches or pull requests

4 participants