python / python/mypy

Generic method in generic class impacting type signature of unrelated method

Aperta
#20,644 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione fornita di Store[ReadWrite] con mypy 1.19.1 e i flag di strictness elencati, quindi traccia il modo in cui il metodo generico Store.copy_from influisce sul tipo self di Store.read. Il lavoro è completato quando la riproduzione non segnala più un argomento self non valido, il comportamento generico rimane corretto ed è presente una copertura di regressione per questo caso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.