Skip to content

mypy incorrectly reports incompatible return types for overloaded methods #12733

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

Open
Prometheus3375 opened this issue May 5, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@Prometheus3375
Copy link

Prometheus3375 commented May 5, 2022

Bug Report

@overload
def foo(cls: type[dict], mapping: SupportsKeysAndGetItem[K, V], /) -> dict[K, V]: ...
@overload
def foo(cls: type[dict], mapping: SupportsKeysAndGetItem[str, V], /, **kwargs: V) -> dict[str, V]: ...

Return types are compatible.

class FrozendictBase(Generic[K, V]):
    @overload
    def __new__(
            cls,
            mapping: SupportsKeysAndGetItem[K, V],
            /,
            ) -> 'FrozendictBase[K, V]': ...

    @overload
    def __new__(
            cls,
            mapping: SupportsKeysAndGetItem[str, V],
            /,
            **kwargs: V,
            ) -> 'FrozendictBase[str, V]': ...

Return types are also compatible, but mypy reports an error.

To Reproduce

  1. Download file.txt and save as sandbox.py.
  2. Run mypy sandbox.py.

Expected Behavior

sandbox.py:8: error: Invariant type variable "V" used in protocol where covariant one is expected

This is due to #5775.

Actual Behavior

sandbox.py:8: error: Invariant type variable "V" used in protocol where covariant one is expected
sandbox.py:39: error: Overloaded function signatures 3 and 4 overlap with incompatible return types
sandbox.py:54: error: Overloaded function signatures 5 and 6 overlap with incompatible return types

Your Environment

  • Mypy version used: mypy 0.950
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.10.4
  • Operating system and version: Windows 10
@Prometheus3375 Prometheus3375 added the bug mypy got something wrong label May 5, 2022
@hauntsaninja
Copy link
Collaborator

I thiiiiink mypy is correct here, although it's certainly not intuitive.

The tricky thing is dict is invariant in its key type. So if you pass SupportsKeysAndGetItem[StrSubclass, int] and no extra kwargs, both overloads would match, but overload 1 returns you a dict[StrSubclass, int] and overload 2 returns you a dict[str, int] and those are incompatible.

@Prometheus3375
Copy link
Author

Prometheus3375 commented May 7, 2022

@hauntsaninja

  1. mypy reports errors only in FrozendictBase methods.
  2. SupportsKeysAndGetItem is also invariant in both key and values in this example, so SupportsKeysAndGetItem[StrSubclass, int] is not subclass of SupportsKeysAndGetItem[str, int]; thus, overload 2 is not macthed.

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

2 participants