constrained TypeVar does not allow default of one of the constraint choices
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
I suspect this is related to https://github.com/python/mypy/issues/3737 but seems to be a specific sub-case that should be unambigious (and pyright accepts the code unchanged)
Bug Report
a constrained TypeVar T: (U, V) "T must be either U or V" does not accept either U or V as a default value
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=b7cc5486d300e5869277d59fa33a2b03
class U: pass
class V: pass
class C[T: (U, V)]:
def __init__(self, tp: type[T] = U) -> None:
self.x: dict[int, T] = {1: tp()}
(I kinda understand that mypy wouldn't know well enough that C() would actually be C[U] -- I've omitted overloads that make that work with mypy -- but the error on the default parameter still remains)
Expected Behavior
(no errors)
Actual Behavior
main.py:5: error: Incompatible default for argument "tp" (default has type "type[U]", argument has type "type[V]") [assignment]
Found 1 error in 1 file (checked 1 source file)
I'm guessing that for a constrained typevar mypy is just checking each possible constraint against the default value?
Your Environment
- Mypy version used: 1.12.1
- Mypy command-line flags: n/a
- Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: 3.12.x
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 das eingeschränkte TypeVar-Beispiel von der verlinkten mypy-play-Seite mit mypy 1.12.1 und verfolge anschließend den Typprüfpfad für das Standardargument. Füge einen Regressionstest hinzu, der Standardwerte für beide Einschränkungsoptionen abdeckt, und überprüfe, dass das Beispiel keine Fehler meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100