Mypy should allow for Self in parametrized parent class definition — issues with contextlib.AbstractContextManager
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
AbstractContextManager defines a __enter__ that returns self
- https://github.com/python/typeshed/blob/6a8d653a671925b0a3af61729ff8cf3f90c9c662/stdlib/contextlib.pyi#L42
- https://github.com/python/cpython/blob/5436d8b9c397c48d9b0d5f9d4ad5e1d5a5d500f6/Lib/contextlib.py#L25
But it's impossible to just inherit from contextlib.AbstractContextManager and have the type of __enter__ be correctly assumed to be Self, without a bit of boilerplate.
To Reproduce
class A(contextlib.AbstractContextManager):
pass
reveal_type(A().__enter__()) # should be Self but assumed to be Any
and mypy disallows writing
class B(contextlib.AbstractContextManager[Self]): # error: Self type is only allowed in annotations within class definition [misc]
pass
and doesn't issue a warning for this runtime error:
class C(contextlib.AbstractContextManager[C]):
pass
Full example here
https://mypy-play.net/?mypy=latest&python=3.11&gist=7e76e56be6c702005a7ffbb7846b69c2
Expected Behavior
Either Self is allowed in the AbstractContextManager parameter list, or the type vars work by default.
Actual Behavior
Errors. See the playground.
(Although pyright doesn't error when using Self in the parameter list, but it still wrongly infers Any as the return of __enter__)
Workaround
Repeat the class name, and repeat the __enter__ impelmentation
class E(contextlib.AbstractContextManager["E"]):
def __enter__(self) -> Self:
return self
Your Environment
- Mypy version used: 1.11.2
- Mypy command-line flags: n/a
- Python version used: 3.11
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo gli esempi nel link mypy-play, in particolare i casi AbstractContextManager e Self, e confronta i tipi rivelati e gli errori segnalati con il comportamento previsto. Il lavoro è completato quando un test di regressione mirato dimostra il comportamento scelto di Self o della variabile di tipo senza richiedere il workaround, preservando al contempo la semantica rilevante di Python 3.11.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100