Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1593.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unimported `typing.ClassVar` annotations created as forward references are now detected as class variables.
1 change: 1 addition & 0 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ def _is_class_var(annot):
annotations which would put attrs-based classes at a performance
disadvantage compared to plain old classes.
"""
annot = getattr(annot, "__forward_arg__", annot)
annot = str(annot)

# Annotation can be quoted.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,23 @@ class C:

assert_init_annotations(C)

@pytest.mark.skipif(
sys.version_info[:2] < (3, 14),
reason="Python 3.14 changed annotation evaluation behavior.",
)
def test_missing_classvar_import(self, slots):
"""
Unimported ClassVars are recognized as ForwardRefs.
"""

@attr.s(auto_attribs=True, slots=slots)
class C:
cls_var: ClassVar[str] # noqa: F821
value: int = 1

assert "cls_var" not in attr.fields_dict(C)
assert 1 == C().value

def test_keyword_only_auto_attribs(self):
"""
`kw_only` propagates to attributes defined via `auto_attribs`.
Expand Down Expand Up @@ -691,6 +708,7 @@ class A:
"typing.ClassVar",
"'typing.ClassVar[dict]'",
"t.ClassVar[int]",
typing.ForwardRef("ClassVar[str]"),
],
)
def test_is_class_var(annot):
Expand Down