Dataclasses: `No overload variant of "asdict" matches argument type "Self" [call-overload]` when deciding decorator conditionally
还没有人认领这个 Issue。
评估
调研方向
使用 mypy 1.10.0 和 --show-error-context --strict 运行提供的 Python 3.12 示例,以复现 dataclasses.asdict 和 dataclasses.replace 的错误。首先跟踪条件 dataclass 装饰器、Self 以及已报告的重载和类型变量检查的处理。完成的标准是示例在保留运行时行为的同时通过类型检查,且不再出现这些误报。
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 18 小时
- 30 天内合并 PR
- 54
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/mypy 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
-
documentation
难度 2/5 1-3 小时 新手友好度 72/100
-
bug topic-configuration topic-error-reporting
难度 2/5 1-3 小时 新手友好度 68/100
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#759 ·