Cryptic error message for two generic callables with different variable binding
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
If a @contextmanager yields a generic type alias, mypy 1.11 errors with Argument 1 to "contextmanager" has incompatible type "$TYPE"; expected "$TYPE" [arg-type], where the inferred and expected types are identical.
This does not happen if the generic type is inlined (no type alias), or if the type alias is not generic; see playground link below for examples.
This started happening in 1.11 (didn't happen in 1.10). This may be related to https://github.com/python/mypy/issues/8696, but I opened a new issue because it looks like a regression.
Real code that tripped this: link.
To Reproduce
Short version (more examples in playground links below):
GenericFn = Callable[[_T], _T]
@contextmanager
def make_generic() -> Iterator[GenericFn[_T]]:
def rv(obj: _T) -> _T:
return obj
yield rv
Playground:
Expected Behavior
Either fail with a useful error message (the inferred and expected types being exactly the same is confusing; I am not sure what would fix the error), or don't fail (like 1.10).
Actual Behavior
error: Argument 1 to "contextmanager" has incompatible type "Callable[[], Iterator[Callable[[_T], _T]]]"; expected "Callable[[], Iterator[Callable[[_T], _T]]]" [arg-type]
Your Environment
- Mypy version used: 1.11.0
- Mypy command-line flags: --strict
- Mypy configuration options from
mypy.ini(and other config files): n/a - Python version used: 3.12
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 minimale @contextmanager-Beispiel mit mypy 1.11.0 unter Python 3.12 und --strict und vergleiche es anschließend mit 1.10.1 und den verlinkten Playground-Fällen. Verfolge die Verarbeitung generischer Typ-Aliase rund um make_generic und contextmanager; abgeschlossen ist die Aufgabe, wenn die Regression vermieden wird oder die identischen abgeleiteten und erwarteten Typen durch einen hilfreichen Fehler ersetzt werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100