From 300e9a9d5c834e1307ed130378a2530d5bb6a968 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Sat, 20 Mar 2021 10:06:56 +0100 Subject: [PATCH 1/2] Detect ClassVars coming from typing_extensions --- src/attr/_make.py | 7 ++++++- tests/test_annotations.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/attr/_make.py b/src/attr/_make.py index 3b02bbb15..d0a251112 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -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) diff --git a/tests/test_annotations.py b/tests/test_annotations.py index 203b8ddee..2df6311b2 100644 --- a/tests/test_annotations.py +++ b/tests/test_annotations.py @@ -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`. From 627e35ea80f9ca1b652b26392b859758c412c835 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Sat, 20 Mar 2021 10:10:18 +0100 Subject: [PATCH 2/2] Add newsfragment --- changelog.d/782.change.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/782.change.rst diff --git a/changelog.d/782.change.rst b/changelog.d/782.change.rst new file mode 100644 index 000000000..9314ccf29 --- /dev/null +++ b/changelog.d/782.change.rst @@ -0,0 +1 @@ +``ClassVar``\ s are now also detected if they come from `typing-extensions `_.