Skip to content

mypy confused by defaultdict(list) #815

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
agentydragon opened this issue Aug 18, 2015 · 9 comments
Closed

mypy confused by defaultdict(list) #815

agentydragon opened this issue Aug 18, 2015 · 9 comments
Labels
bug mypy got something wrong

Comments

@agentydragon
Copy link
Contributor

Source:

from collections import defaultdict
x = defaultdict(list)

Error:

test.py:2: error: Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], List[_T]]
test.py:2: error: Need type annotation for variable
@o11c
Copy link
Contributor

o11c commented Aug 18, 2015

What happens if you add # type: defaultdict[List[int]] or whatever the actual type of the list is?

@agentydragon
Copy link
Contributor Author

x = defaultdict(list) # type: defaultdict[str, List[str]] ==> test.py:2: error: Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], List[str]]

@agentydragon
Copy link
Contributor Author

Also, this is an issue on the hack branch -- master might or might not have this issue.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 23, 2015

Also happens on master. Here is a self-contained repro suitable for a unit test:

from typing import Generic, Callable, TypeVar

T = TypeVar('T')

class C(Generic[T]):
    def __init__(self, x: Callable[[], T]) -> None: ...

class D(Generic[T]): pass

a = C(D)
b = C(D) # type: C[D[str]]

@JukkaL JukkaL added bug mypy got something wrong priority and removed hack labels Aug 23, 2015
@gvanrossum
Copy link
Member

How hard would this be? The majority of uses of defaultdict that I see have one of the builtins set or list as argument. (Also the error message seems wrong -- the argument is not a value of type List[_T], it's a class representing that type.)

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 14, 2015

I haven't look into this yet but my guess would be that I could fix this pretty quickly.

@gvanrossum
Copy link
Member

One quick hack would be to just add another overload to collections.pyi:

    @overload
    def __init__(self, default_factory: type) -> None: ...

But this feels wrong -- it should just know that these types are callables of 0 arguments.

@gvanrossum
Copy link
Member

If you decide to fix this in typeshed, also note that two of the overloads
of defaultdict.__init__ are wrong -- it does not take a map or iterable
first parameter (those must be the second). Also the default_factory
parameter should be Optional. Keyword args are treated like dict's
keyword args.

On Sun, Dec 13, 2015 at 8:49 PM, Jukka Lehtosalo [email protected]
wrote:

I haven't look into this yet but my guess would be that I could fix this
pretty quickly.


Reply to this email directly or view it on GitHub
#815 (comment).

--Guido van Rossum (python.org/~guido)

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 14, 2015

I'll give this a try tomorrow or Tuesday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants