Skip to content

Generic method in generic class impacting type signature of unrelated method #20644

@lijok

Description

@lijok

Bug Report

MyPy's type resolution appears to get impacted by an unrelated generic method when evaluating a method.

To Reproduce

"""Mypy bug: method-level type parameter breaks unrelated method's self type.

mypy --strict mypy-bug-repro.py
mypy 1.19.1, Python 3.13.9
"""

from typing import Protocol, Self


class Read(Protocol):
    def read(self: Self) -> None: ...


class Write(Protocol):
    def write(self: Self) -> None: ...


class CopyFrom(Protocol):
    def copy_from(self: CopyFrom, src: CopyFrom) -> None: ...


class ReadWrite(Read, Write, Protocol):
    pass


class Store[B: Read | Write | CopyFrom]:
    def read(self: Store[Read]) -> None: ...
    def copy_from[T: CopyFrom](self: Store[T], src: Store[T]) -> None: ...


def test(store: Store[ReadWrite]) -> None:
    store.read()  # Error: Invalid self argument

Expected Behavior

Expected mypy to be happy

Actual Behavior

mypy --strict --warn-unreachable --extra-checks returns;

error: Invalid self argument "Store[ReadWrite]" to attribute function "read" with type "Callable[[Store[Read]], None]"  [misc]

Removing Store.copy_from fixes this.
Replacing test def with def test(store: Store[Read]) -> None: fixes this
Replacing Store.copy_from with the following fixes it as well;

type T = CopyFrom
class Store[B: Read | Write | CopyFrom]:
    def read(self: Store[Read]) -> None: ...
    def copy_from(self: Store[T], src: Store[T]) -> None: ...

Your Environment

  • Mypy version used: mypy 1.19.1 (compiled: yes)
  • Mypy command-line flags: --strict --warn-unreachable --extra-checks
  • Mypy configuration options from mypy.ini (and other config files):
[tool.mypy]
plugins = ["pydantic.mypy"]
exclude = ["build-resources"]
  • Python version used: Python 3.13.9

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