8000 bpo-44975: [typing] Support issubclass for ClassVar data members by Fidget-Spinner · Pull Request #27883 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44975: [typing] Support issubclass for ClassVar data members #27883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Prev Previous commit
Next Next commit
Apply Lukasz' suggestions
  • Loading branch information
Fidget-Spinner committed Aug 24, 2021
commit 58c69e1ca7e0e00977f8d43cff37feee87b2b9db
2 changes: 1 addition & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ class D:
self.assertNotIsSubclass(C, P)
self.assertIsSubclass(D, P)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also test these classes:

class G:
   x: ClassVar[int] = 1
class H:
   x: 'ClassVar[int]' = 1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't testing more code paths, the code doesn't look at the annotations of the first class argument in issubclass.

# String / PEP 563 annotations.
# String annotations (forward references).
@runtime_checkable
class P(Protocol):
x: "ClassVar[int]" = 1
Expand Down
11 changes: 4 additions & 7 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ def _get_protocol_attrs(cls):
attrs.add(attr)
return attrs

_classvar_prefixes = ("typing.ClassVar[", "t.ClassVar[", "ClassVar[")

def _is_callable_or_classvar_members_only(cls):
attr_names = _get_protocol_attrs(cls)
Expand All @@ -1406,13 +1407,9 @@ def _is_callable_or_classvar_members_only(cls):
annotation = annotations.get(attr_name)
if getattr(annotation, '__name__', None) == 'ClassVar':
continue
# String / PEP 563 annotations
# Note: If PEP 649 is accepted, we can probably drop this.
if isinstance(annotation, str):
if (annotation.startswith('ClassVar[')
or annotation.startswith('typing.ClassVar[')
or annotation.startswith('typing_extensions.ClassVar[')):
continue
# String annotations (forward references).
if isinstance(annotation, str) and annotation.startswith(_classvar_prefixes):
continue
return False
return True

Expand Down
0