mirror of
https://github.com/python/cpython.git
synced 2026-01-05 23:12:38 +00:00
bpo-46553: allow bare typing.ClassVar annotations (#30983)
These are used in the wild and covered by dataclasses unit tests. Several static type checkers support this pattern.
This commit is contained in:
parent
45faf151c6
commit
5445e173e7
3 changed files with 7 additions and 2 deletions
|
|
@ -2071,7 +2071,7 @@ def test_fail_with_bare_union(self):
|
|||
with self.assertRaises(TypeError):
|
||||
Tuple[Optional]
|
||||
with self.assertRaises(TypeError):
|
||||
ClassVar[ClassVar]
|
||||
ClassVar[ClassVar[int]]
|
||||
with self.assertRaises(TypeError):
|
||||
List[ClassVar[int]]
|
||||
|
||||
|
|
@ -2896,12 +2896,16 @@ def test_special_forms_forward(self):
|
|||
class C:
|
||||
a: Annotated['ClassVar[int]', (3, 5)] = 4
|
||||
b: Annotated['Final[int]', "const"] = 4
|
||||
x: 'ClassVar' = 4
|
||||
y: 'Final' = 4
|
||||
|
||||
class CF:
|
||||
b: List['Final[int]'] = 4
|
||||
|
||||
self.assertEqual(get_type_hints(C, globals())['a'], ClassVar[int])
|
||||
self.assertEqual(get_type_hints(C, globals())['b'], Final[int])
|
||||
self.assertEqual(get_type_hints(C, globals())['x'], ClassVar)
|
||||
self.assertEqual(get_type_hints(C, globals())['y'], Final)
|
||||
with self.assertRaises(TypeError):
|
||||
get_type_hints(CF, globals()),
|
||||
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
|
|||
if (isinstance(arg, _GenericAlias) and
|
||||
arg.__origin__ in invalid_generic_forms):
|
||||
raise TypeError(f"{arg} is not valid as type argument")
|
||||
if arg in (Any, NoReturn, Final):
|
||||
if arg in (Any, NoReturn, ClassVar, Final):
|
||||
return arg
|
||||
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
|
||||
raise TypeError(f"Plain {arg} is not valid as type argument")
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
In :func:`typing.get_type_hints`, support evaluating bare stringified ``ClassVar`` annotations. Patch by Gregory Beauregard.
|
||||
Loading…
Add table
Add a link
Reference in a new issue