-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix for dict_keys being in _collections.abc #6888
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
Conversation
This comment has been minimized.
This comment has been minimized.
Unable to work out how to fix Python 3.10 as this requires the generic |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
It's a tricky problem to solve ☹
Mypy previously didn't object to you annotating your code with Arguably, since CPython caused this problem, CPython should fix it: either by properly exposing the |
I'm happy to file a BPO issue tomorrow about this, FWIW. It seems like a pretty serious problem. |
Hi @AlexWaygood, thanks for the reply and explanation. I looked around a fair bit in the CPython code for the "true" return type of a dict but couldn't find it. I saw how Thanks for offering to raise a BPO that would be great. |
Three alternative solutions that wouldn't require changes in CPython would be:
I like the idea of a CPython fix much better than any of those three, but I think those are our options 🙂 |
This isn't exactly true. What broke OP's code was changing the return types of This would have broken OP's code regardless of whether the Getting slightly off topic, there is a sort of general problem that can happen with Liskov's substitution principle:
Here, LSP often causes tension for typeshed. We often make changes that allow type checkers to infer more specific types (which is good), but users who inherit from those types get broken. When users don't care about exact types and especially when those exact types don't exist or aren't obvious at runtime, usability suffers. I recently walked someone through LSP issues involving a custom IO type + Anyway, a possible better solution for the code OP has in their issue report could be to inherit their custom class from |
Hey, at least I got this part right 😉 |
It is tricky! Thanks for helping talk through it. I led OP astray with my unworkable suggestion in #6837 (comment) , which you very kindly explain the problems with in point 2 of #6888 (comment) |
BPO issue here: https://bugs.python.org/issue46399 |
FYI, I think my proposed solution (to expose |
How about adding
|
Ooh, I like this idea. Let's wait until adding them to |
As I mentioned on the bug, I don't think exposing dict_keys and friends at runtime actually solves that much, since you can't inherit from or instantiate them. How would a user write type-clean code if we exposed those types? |
Yeah. It's still not ideal. Here's yet another idea for fixing this issue: Define a custom protocol in |
I wonder if a protocol can inherit from a concrete class. |
No, it can't. And But tbh, probably doesn't need to be? Just a generic ABC that's subclassable might work? |
As discussed, this change isn't correct, and there isn't much we can do in typeshed. Users may just need to |
This is my attempt at fixing the issue I opened (#6837) that requires the user to import
_collections_abc.dict_keys
as the return type of{}.keys()
- I believe that it still satisfies the requirements of #6039 whilst maintaining backward compatibility to versions of mypy <v0.930