python / python/mypy

Generic method in generic class impacting type signature of unrelated method

Open
#20,644 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Store[ReadWrite] reproduction with mypy 1.19.1 and the listed strictness flags, then trace how the generic Store.copy_from method affects the self type of Store.read. Done means the reproduction no longer reports an invalid self argument while the generic behavior remains correct, with regression coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.