Mypy rejects `cls.__repr__(obj)` when `cls: type[Any]`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere die beiden __repr__-Aufrufe und den Fall cls: type[Any] in mypy-play anhand der Python 3.11-Beispiele aus dem Issue. Verfolge den Typprüfpfad für den Memberzugriff von type[Any] und bestätige, dass cls.__repr__(obj) akzeptiert wird, ohne das gültige Verhalten von type[object] zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100