-
Notifications
You must be signed in to change notification settings - Fork 225
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
Add DefaultDict to typing.py #179
Comments
If people are already using this, and the solution is simple, then this is a good idea. I added a PR #197 for this |
I like this, but we should also add this to mypy. Can you submit a PR there too? |
I am not sure about this. I am not familiar with all the internals of mypy. Do I need to simply make changes to lib-typing/3.2 and lib-typing/2.7 or something else also? If only lib-typing should be modified, then no problem. |
Actually lib-typing is the least important -- it's an copy of the real
typing.py that's installed for mypy's own benefit (and AFAIK only the 3.2
version ever gets used).
But there's some special-casing to make typing.Dict an alias of
`__builtins__.dict` in nodes.py and you should add DefaultDict there as an
alias for collections.defaultdict. Also add a test case; tips for
contributing and running tests are in README.md and also on the github wiki.
|
Oh, forgot. There's also two versions of typing.pyi in typeshed that
need to be updated.
|
@gvanrossum I am now working on adding this to mypy. I tried to add While I was adding tests for this I found out that python/mypy#815 has not been fixed completely. Here are few examples: from collections import defaultdict
# this is now fixed
defaultdict(list)
# this expectedly fails but the error message shows that the type of first argument
# incorrectly found to be List[_T], while it should be type, or something similar:
# Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], str]
d = defaultdict(list) # type: defaultdict[int, str]
class MyDDict(defaultdict):
pass
# and here we see the old bug again
# Argument 1 to "MyDDict" has incompatible type List[_T]; expected Callable[[], _VT]
MyDDict(list) |
Hm... If you just alias typing.DefaultDict to collections.defaultdict, you won't be able to index it in a position where the code is executable, e.g. in a Python 3 style annotation Can you please separately file the bug with MDDict(list)? It appears mypy may not be handling an overloaded |
(Sorry, I missed some context. I blame the lack of coffee. :-) I think just adding typing.DefaultDict as an alias for collections.defaultdict in typeshed is the way to go, since collections.defaultdict is fully generic (and we can't change this easily because there's already too much code depending on it). |
@gvanrossum , OK I filed python/mypy#1358 and added a PR to typeshed python/typeshed#139 |
I've seen some PY2 code that uses
defaultdict[t1, t2]
in type annotations. Those are inside comments so it works. But if you try to do the same using inline PY3 annotations it doesn't work:This gives an error:
We should probably just add typing.DefaultDict (similar to the way we added NamedTuple).
The text was updated successfully, but these errors were encountered: