Skip to content
Merged
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/782.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``ClassVar``\ s are now also detected if they come from `typing-extensions <https://pypi.org/project/typing-extensions/>`_.
7 changes: 6 additions & 1 deletion src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
_tuple_property_pat = (
" {attr_name} = _attrs_property(_attrs_itemgetter({index}))"
)
_classvar_prefixes = ("typing.ClassVar", "t.ClassVar", "ClassVar")
_classvar_prefixes = (
"typing.ClassVar",
"t.ClassVar",
"ClassVar",
"typing_extensions.ClassVar",
)
# we don't use a double-underscore prefix because that triggers
# name mangling when trying to create a slot for the field
# (when slots=True)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ class C:
foo=typing.Optional[typing.Any],
)

@pytest.mark.parametrize("slots", [True, False])
def test_typing_extensions_classvar(self, slots):
"""
If ClassVar is coming from typing_extensions, it is recognized too.
"""

@attr.s(auto_attribs=True, slots=slots)
class C:
cls_var: "typing_extensions.ClassVar" = 23 # noqa

assert_init_annotations(C)

def test_keyword_only_auto_attribs(self):
"""
`kw_only` propagates to attributes defined via `auto_attribs`.
Expand Down