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

Overload with Dict only checks overlap on keys #9451

Closed
rhshadrach opened this issue Sep 17, 2020 · 3 comments · Fixed by #9452
Closed

Overload with Dict only checks overlap on keys #9451

rhshadrach opened this issue Sep 17, 2020 · 3 comments · Fixed by #9452
Labels
bug mypy got something wrong

Comments

@rhshadrach
Copy link

Using mypy 0.782, it looks like overlap is only checked on dictionary keys.

from typing import Dict, overload

class A: pass
class B: pass

@overload
def foo(x: Dict[A, A]) -> A: ...

@overload
def foo(x: Dict[A, B]) -> B: ...

def foo(x):
    for k, v in x: return v
    raise ValueError

gives error: Overloaded function signatures 1 and 2 overlap with incompatible return types. I get the same error when using Mapping instead of `Dict.

@gvanrossum
Copy link
Member

gvanrossum commented Sep 17, 2020

It's not specific to Dict or Mapping, it seems to happen with any generic class over two type variables.

I do not actually have a good theory as to why it happens, but I suspect there is a case where a type can be either a Dict[A, A] or a Dict[A, B]. Perhaps the possibility of multiple inheritance is key:

class C(A, B): ...
a: Dict[A, C] {}
foo(a)

@rhshadrach
Copy link
Author

Thanks for the response. If I change the 2nd overload to Dict[B, B], then mypy doesn't give any errors. I think the ambiguity with multiple inheritance still exists in this case (e.g. Dict[C, C]), yes?

@JelleZijlstra
Copy link
Member

I suspect it's this code: https://github.com/python/mypy/blob/master/mypy/meet.py#L342. is_overlapping_types() returns True if any of the type arguments are overlapping, but presumably it should only do that if they all overlap.

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

Successfully merging a pull request may close this issue.

3 participants