Mypy fails to type check function signature after plugin pass
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
I wrote a plugin to replace types, but Mypy fails to typecheck after my changes. I start off trying to fix the type error in this signature with a plugin:
def almost_id(a: str) -> str:
return int(a)
almost_id("6")
with my plugin:
def replace_str(self, ctx: FunctionSigContext) -> FunctionLike:
api = ctx.api
new_signature = ctx.default_signature.copy_modified(
ret_type=api.named_type('builtins.int')
)
un_anal = new_signature.definition.unanalyzed_type.copy_modified(
ret_type = api.named_type('builtins.int')
)
new_signature.definition.unanalyzed_type = un_anal
typ = new_signature.definition.type.copy_modified(
ret_type = api.named_type('builtins.int')
)
new_signature.definition.type = typ
return new_signature
I have tried going through the attrs of the functionsigcontext, api and the expr_checker but can't seem to find some other functionality to either:
- force mypy to proceed with type checking
- modify the existing type analysis or map
- delay type checking until the plugin mods have been completed, something like defer() in the semantic analyzer.
To Reproduce
gist: https://gist.github.com/dibrinsofor/bddcbd2eedbb233b131bb1b8d562369a
mypy tests/str_int.py
Expected Behavior
I expect mypy to pass
Actual Behavior
tests/str_int.py:2: error: Incompatible return value type (got "int", expected "str") [return-value]
even though printing the new signature returns:
def (a: builtins.str) -> builtins.int
Your Environment
- Mypy version used:
1.14.0+dev.8c7db13f71098a6facea66ab165b6bc3ef688bdf - Mypy command-line flags:
--show-traceback - Python version used:
Python 3.11.5
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
Führe mypy tests/str_int.py anhand der Gist-Reproduktion aus und vergleiche den gemeldeten Fehler mit der nach replace_str ausgegebenen Signatur. Verfolge, wie das Ergebnis von FunctionSigContext während der semantischen Analyse und der Ausdrucksprüfung verwendet wird, und nutze dabei die im Bericht erwähnten Pfade expr_checker und defer(). Erledigt ist die Aufgabe, wenn der vom Plugin geänderte Rückgabetyp berücksichtigt wird und die Reproduktion erfolgreich ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100