`dataclasses`: Synthetic `__init__` method on dataclass has broken `ClassVar` annotation under some conditions.
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
When using automatically-stringified annotations (from __future__ import annotations), typing_extensions.ClassVar is not treated the same as typing.ClassVar even though those two objects are identical. Specifically, the dataclasses.dataclass-produced __init__ method does not exclude typing_extensions.ClassVar-wrapped variables from its annotations, but does exclude typing.ClassVar-wrapped variables from its annotations. Among other problems, this causes typing.get_type_hints to choke.
Reproducer below:
from __future__ import annotations
import dataclasses as dcls
import inspect
import typing
import typing_extensions as typx
assert typing.ClassVar is typx.ClassVar
@dcls.dataclass
class Foo:
y: int
x: typing.ClassVar[ int ] = 1
print( "\n=== typing.ClassVar (ok) ===" )
print( 'anno', Foo.__init__.__annotations__ )
print( 'inspect', inspect.get_annotations( Foo.__init__, eval_str = True ) )
print( 'typing', typing.get_type_hints( Foo.__init__ ) )
print( 'typx', typx.get_type_hints( Foo.__init__ ) )
@dcls.dataclass
class Bar:
y: int
x: typx.ClassVar[ int ] = 1
print( "\n=== typing_extensions.ClassVar (breaks) ===" )
print( 'anno', Bar.__init__.__annotations__ )
print( 'inspect', inspect.get_annotations( Bar.__init__, eval_str = True ) )
print( 'typing', typing.get_type_hints( Bar.__init__ ) )
print( 'typx', typx.get_type_hints( Bar.__init__ ) )
This results in the following output:
=== typing.ClassVar (ok) ===
anno {'y': 'int', 'return': None}
inspect {'y': <class 'int'>, 'return': None}
typing {'y': <class 'int'>, 'return': <class 'NoneType'>}
typx {'y': <class 'int'>, 'return': <class 'NoneType'>}
=== typing_extensions.ClassVar (breaks) ===
anno {'y': 'int', 'x': 'typx.ClassVar[int]', 'return': None}
inspect {'y': <class 'int'>, 'x': typing.ClassVar[int], 'return': None}
Traceback (most recent call last):
File "/home/me/src/somepkg/bugs/dcls-classvar-hints.py", line 38, in <module>
print( 'typing', typing.get_type_hints( Bar.__init__ ) )
File "/usr/lib/python3.10/typing.py", line 1871, in get_type_hints
value = _eval_type(value, globalns, localns)
File "/usr/lib/python3.10/typing.py", line 327, in _eval_type
return t._evaluate(globalns, localns, recursive_guard)
File "/usr/lib/python3.10/typing.py", line 693, in _evaluate
type_ = _type_check(
File "/usr/lib/python3.10/typing.py", line 167, in _type_check
raise TypeError(f"{arg} is not valid as type argument")
TypeError: typing.ClassVar[int] is not valid as type argument
(Credit to @Daraan for helping me pinpoint the problem.)
This is reproducible on both Python 3.10 and 3.13.
Possibly related to https://github.com/python/cpython/issues/89687.
CPython versions tested on:
3.10, 3.13
Operating systems tested on:
No response
Linked PRs
- gh-134073
- gh-140541
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 reproducer 开始,跟踪 dataclasses.dataclass 如何为 ClassVar 注解构建 init.annotations,并比较 typing.ClassVar 与 typing_extensions.ClassVar。通过检查生成的注解是否排除 ClassVar 字段,以及 typing.get_type_hints 是否能够正常工作且不会引发异常,确认 fix;issue 列出了相关 PR gh-134073 和 gh-140541。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100