Skip to content

False positive with contravariant type variables in overloads implemented on Protocols #20108

@bzoracler

Description

@bzoracler

Bug Report, To Reproduce, & Actual Behaviour

See mypy Playground:

from typing import TypeVar, Protocol, overload

_T1_contra = TypeVar("_T1_contra", contravariant=True)
_T2_contra = TypeVar("_T2_contra", contravariant=True)

class A(Protocol[_T1_contra, _T2_contra]):  # E: Contravariant type variable "_T2_contra" used in protocol where covariant one is expected  [misc]
    @overload
    def method(self, a: _T1_contra) -> None: ...
    @overload
    def method(self, a: _T2_contra) -> None: ...
    def method(self, a: object) -> None: ...

No errors are emitted if type parameter syntax is used (which should automatically imply contravariance), or if Protocol is replaced by Generic.

Expected Behavior

No errors.

Your Environment

  • Mypy version used: 1.18.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions