Skip to content

mypy incorrectly reports incompatible return types for overloaded methods #12733

Open
@Prometheus3375

Description

@Prometheus3375

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions