Dataclasses: `No overload variant of "asdict" matches argument type "Self" [call-overload]` when deciding decorator conditionally
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 42/100
Rechercherichtung
Führe das bereitgestellte Python-3.12-Beispiel mit mypy 1.10.0 und --show-error-context --strict aus, um die Fehler für dataclasses.asdict und dataclasses.replace zu reproduzieren. Beginne damit, die Verarbeitung des bedingten dataclass-Dekorators, von Self sowie der gemeldeten Überladungs- und Typvariablenprüfungen nachzuverfolgen. Als erledigt gilt die Aufgabe, wenn das Beispiel ohne diese Fehlalarme eine Typprüfung besteht und dabei das Laufzeitverhalten erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Bug Report
Mypy raises a false positive for dataclass.asdict (No overload variant of "asdict" matches argument type "Self" [call-overload]) on the very specific circumstances:
- The specific decorator for
dataclassis decided based on a condition (e.g. Python version) - The
datalcasses.asdictmethod is called from a method that uses PEP 673's Self Type.
I also noted a similar problem occurs for dataclasses.replace: Value of type variable "_DataclassT" of "replace" cannot be "Self" [type-var].
To Reproduce
import sys
import dataclasses
import json
from typing import Self
if sys.version_info >= (3, 10):
dataclass = dataclasses.dataclass(kw_only=True)
else:
dataclass = dataclasses.dataclass
@dataclass
class HelloWorld:
a: int = 1
b: int = 2
c: int = 3
def double(self) -> Self:
fields = dataclasses.asdict(self)
return self.__class__(**{k: v*2 for k, v in fields.items()})
def json_overwrite(self, text: str) -> Self:
new = json.loads(text)
return dataclasses.replace(self, **new)
x = HelloWorld().double()
y = HelloWorld().json_overwrite('{"c": 42}')
assert x.b == 4
assert y.c == 42
print(f"{x=}")
print(f"{y=}")
Gist URL: https://gist.github.com/mypy-play/70b801fbe15391f0750be8f6c403fdf4
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&flags=show-error-context%2Cstrict&gist=70b801fbe15391f0750be8f6c403fdf4
Expected Behavior
There should be no false negative.
The program works fine in runtime:
$ python3.12 main.py
x=HelloWorld(a=2, b=4, c=6)
y=HelloWorld(a=1, b=2, c=42)
Actual Behavior
main.py: note: In member "double" of class "HelloWorld":
main.py:19: error: No overload variant of "asdict" matches argument type "Self" [call-overload]
main.py:19: note: Possible overload variants:
main.py:19: note: def asdict(obj: DataclassInstance) -> dict[str, Any]
main.py:19: note: def [_T] asdict(obj: DataclassInstance, *, dict_factory: Callable[[list[tuple[str, Any]]], _T]) -> _T
main.py: note: In member "json_overwrite" of class "HelloWorld":
main.py:24: error: Value of type variable "_DataclassT" of "replace" cannot be "Self" [type-var]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.10.0
- Mypy command-line flags:
--show-error-context,--strict - Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.12
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/mypy
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
bug topic-configuration topic-error-reporting
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
bancolombia/sentinel#23 ·
-
test md OffenCI
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100