Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Dict constructor with unknown base class #1363

Closed
drewhaven opened this issue Apr 11, 2016 · 3 comments
Closed

Dict constructor with unknown base class #1363

drewhaven opened this issue Apr 11, 2016 · 3 comments
Labels
bug mypy got something wrong

Comments

@drewhaven
Copy link

Consider the following:

from typing import Any, Dict
from base import Base

class Derived(Base):
    def to_dict(self) -> Dict[str, Any]:
        return dict(self)

When we run mypy with --slient-imports on only this file, we get the following error:

note: In member "to_dict" of class "Derived":
error: No overload variant of "dict" matches argument types [test_mypy_dict.Derived]
@gvanrossum gvanrossum added the bug mypy got something wrong label Apr 11, 2016
@gvanrossum gvanrossum added this to the 0.3.2 milestone Apr 11, 2016
@gvanrossum
Copy link
Member

Looks like this was caused by 1423a54. I think the fix will be easy.

@gvanrossum
Copy link
Member

OK, it's not as easy as I thought. :-( It is not finding any overloads because the arg (self) has a type that is derived from Any, but is not exactly Any. So the allowance for Any made in overload_arg_similarity() doesn't trigger. If I make the allowance more flexible by using is_subtype(actual, AnyType()) then it arbitrarily picks the first overload with the right number of parameters, and then we get

Argument 1 to "dict" has incompatible type "Derived"; expected Mapping[str, Any]

Maybe the right solution is just to make self's type equal to Any? @JukkaL any thoughts?

@JukkaL
Copy link
Collaborator

JukkaL commented Apr 12, 2016

Making self's type Any wouldn't help in general, because the same issue could happen for non-self values as well.

We could make anything derived from Any be a subtype of every type, since there could be any number of base classes that we don't see. The above problem would likely go away if we did this. Of course, this would hide some real errors, but the behavior would be more correct.

For example, this would be okay:

from typing import Any

a = int  # type: Any
class B(a): pass

def f(x: int) -> None: pass
def g(x: str) -> None: pass

f(B())  # currently error, but should perhaps be fine
g(B())  # same as above, perhaps should not be an error

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

No branches or pull requests

3 participants