Skip to content
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

Mypy incorrectly determines type #1358

Closed
ilevkivskyi opened this issue Apr 9, 2016 · 6 comments
Closed

Mypy incorrectly determines type #1358

ilevkivskyi opened this issue Apr 9, 2016 · 6 comments
Assignees
Labels
bug mypy got something wrong

Comments

@ilevkivskyi
Copy link
Member

It looks like #815 has not been fixed completely, here is an example

from collections import defaultdict

# this is now fixed
defaultdict(list)

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)
@ilevkivskyi
Copy link
Member Author

Here is another example that shows that something is wrong even without subclassing

d = defaultdict(list) # type: defaultdict[int, str]

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 Callable[..., List[_T]] or something similar:

Argument 1 to "defaultdict" has incompatible type List[_T]; expected Callable[[], str]

@gvanrossum gvanrossum added the bug mypy got something wrong label Apr 9, 2016
@gvanrossum
Copy link
Member

Actually, in your second example, List[_T] is the value of the argument, just normalized a bit differently to show that list is written as List, and it's generic in one parameter. This is done (after a bunch of convolutions) here: https://github.com/python/mypy/blob/master/mypy/messages.py#L196 . You can argue that the message formatting code should be more careful; maybe it should say "has incompatible value List[_T]" or maybe "has incompatible type Type[List[_T]]" (since we're probably going to introduce Type[...] anyways, see python/typing#107) but the type checking is not at fault.

However your first issue still stands, and I'll look into why the properties of defaultdict aren't correctly conferred on its subclass. (I have a vague feeling I ran into a similar issue myself recently but can't find anything I might have put in the tracker about it.)

@gvanrossum
Copy link
Member

For the first issue, I'm going to venture a guess that this is one of our numerous issues with generic types, and I'm assigning it to Reid Barton who's looking to become the expert on this topic.

@ilevkivskyi
Copy link
Member Author

The error message is indeed a bit misleading, because in another case

d = defaultdict(42) # type: defaultdict[int, str]

it mentions the type and not the value

Argument 1 to "defaultdict" has incompatible type "int"; expected Callable[[], str]

@rwbarton
Copy link
Contributor

I think class MyDDict(defaultdict): means MyDDict is a subtype of something like defaultdict[Any,Any]. Assuming that is what you wanted, then this seems to be the same issue as #1246.

If you wanted MyDDict to be a generic class like defaultdict, then you have to write (see #302)

K = TypeVar('K')
V = TypeVar('V')
class MyDDict(defaultdict[K,V], Generic[K,V]): ...

With this change, mypy already accepts the call MyDDict(list).

@gvanrossum
Copy link
Member

OK, let's close this as a dupe of #1246.

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

3 participants