Mypy rejects `cls.__repr__(obj)` when `cls: type[Any]`
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
Mypy accepts this:
object.__repr__(123) # type-checks
cls: type[object]
cls.__repr__(123) # also type-checks
But rejects this:
cls: type[Any]
cls.__repr__(123) # Too many arguments for "__repr__" of "object" [call-arg]
Expected Behavior
I expected mypy to accept cls.__repr__(obj) even when cls: type[Any]. In particular, I expected type[Any] to be more permissive than type[object], not less.
Your Environment
Tested with mypy master branch + Python 3.11 using mypy-play.
Context
I currently have this running in production:
def _robust_repr(obj: object) -> str:
cls: Type[object] # Had to add this to suppress the typecheck error
for cls in type(obj).__mro__:
with suppress(Exception):
return cls.__repr__(obj)
assert False # object.__repr__ can't fail
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
Riproduci le due chiamate a __repr__ e il caso cls: type[Any] in mypy-play usando gli esempi Python 3.11 dell’issue. Segui il percorso del controllo dei tipi per l’accesso ai membri di type[Any] e conferma che cls.__repr__(obj) venga accettato senza modificare il comportamento valido di type[object].
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 48/100