-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Improve documentation for decisions around certain list
, dict
, Mapping
, and Sequence
methods
#7015
Comments
It's true that these functions will take any argument at runtime and return a result, but I'm not convinced that that's what we should be doing in the stub. |
These kinds of methods are typeshed top hits. E.g., just last week: #6922 |
This diverges what is valid according to typing and what is valid at runtime in core libraries, which makes annotating existing code bases that might utilize this behavior correctly to be considered incorrect. I didn't know it was in typing's scope to decide that certain core Python behavior isn't valid, are you sure that isn't overreaching? |
typeshed's purpose is to help developers to find mistakes in their code. As @JelleZijlstra pointed out, these methods are typed to find likely problems, not necessarily to accept everything that doesn't crash at runtime. |
Ideally we would have a way to say "this method accepts any type that overlaps with this type", so you could do |
All of the examples I could come up with (see one below) would be serviced by a This is just an stupid example of a pattern I have used and seen others use before. You handle some cases using a dict and def stringify(value: object) -> str:
# handle a few of the values first
a = ({None: "None", True: "True", False: "False", ...: "..."}).get(value)
if a is not None:
return a
# rest are handled by arbitrary logic
... |
list
, dict
, Mapping
, and Sequence
methods are unnecessarily strictlist
, dict
, Mapping
, and Sequence
methods
list.index
andlist.count
currently only accepts values of the element type andSequence.index
andSequence.count
useAny
for the type of the argument. In my opinion all of these functions could safely takeobject
.dict.get
andMapping.get
currently only accepts values of the key type. This could be safely extended to be any Hashable object. Non-Hashable values will raise TypeError, but runtime safely accepts values of any other type. I ran into someone on the gitter who needed this a few weeks ago, but he never opened an issue.I would be willing to make a PR for this.
The text was updated successfully, but these errors were encountered: