FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix stringified bare ClassVar type hints by Sanjays2402 · Pull Request #782 · python/typing_extensions · GitHub

Repository navigation

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

Filter by extension

Filter by extension .md  (1) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

No user-facing changes since 4.16.0rc2.

- Fix `get_type_hints()` raising `TypeError` for a stringified bare `ClassVar`
annotation on Python 3.9 and 3.10.

# Release 4.16.0rc2 (June 25, 2026)

- Avoid a `DeprecationWarning` when `deprecated` is applied to a coroutine function on
Expand Down
12 changes: 12 additions & 0 deletions src/test_typing_extensions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5648,6 +5648,18 @@ def test_compatibility(self):


class GetTypeHintsTests(BaseTestCase):
def test_get_type_hints_lone_stringified_classvar(self):
class Parent:
parent: 'ClassVar' = 1

class Child(Parent):
child: 'typing.ClassVar' = 2

self.assertEqual(
get_type_hints(Child, globals()),
{'parent': ClassVar, 'child': typing.ClassVar},
)

def test_get_type_hints(self):
def foobar(x: List['X']): ...
X = Annotated[int, (1, 10)]
Expand Down
34 changes: 31 additions & 3 deletions src/typing_extensions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1602,9 +1602,37 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
- If two dict arguments are passed, they specify globals and
locals, respectively.
"""
hint = typing.get_type_hints(
obj, globalns=globalns, localns=localns, include_extras=True
)
if sys.version_info < (3, 11) and isinstance(obj, type):
hint = {}
for base in reversed(obj.__mro__):
base_globals = (
getattr(sys.modules.get(base.__module__), '__dict__', {})
if globalns is None else globalns
)
annotations = base.__dict__.get('__annotations__', {})
if isinstance(annotations, _types.GetSetDescriptorType):
annotations = {}
base_locals = dict(vars(base)) if localns is None else localns
if localns is None and globalns is None:
base_globals, base_locals = base_locals, base_globals
for name, value in annotations.items():
if value is None:
value = type(None)
elif isinstance(value, str):
evaluated = eval(value, base_globals, base_locals)
if evaluated is ClassVar:
value = evaluated
else:
value = ForwardRef(
value, is_argument=False, is_class=True
)
hint[name] = typing._eval_type(
value, base_globals, base_locals
)
else:
hint = typing.get_type_hints(
obj, globalns=globalns, localns=localns, include_extras=True
)
# Breakpoint: https://github.com/python/cpython/pull/30304
if sys.version_info < (3, 11):
_clean_optional(obj, hint, globalns, localns)
Expand Down
Loading

Back | FazBrowse Home | New Git URL