mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.10] bpo-46655: allow stringized TypeAlias with get_type_hints (GH-31156). (#31175)
(cherry picked from commit 77b025be4a)
Co-authored-by: Gregory Beauregard <greg@greg.red>
This commit is contained in:
parent
c1ff4cb98b
commit
e2eeffefed
3 changed files with 7 additions and 1 deletions
|
|
@ -4768,6 +4768,11 @@ def test_no_isinstance(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
isinstance(42, TypeAlias)
|
isinstance(42, TypeAlias)
|
||||||
|
|
||||||
|
def test_stringized_usage(self):
|
||||||
|
class A:
|
||||||
|
a: "TypeAlias"
|
||||||
|
self.assertEqual(get_type_hints(A), {'a': TypeAlias})
|
||||||
|
|
||||||
def test_no_issubclass(self):
|
def test_no_issubclass(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
issubclass(Employee, TypeAlias)
|
issubclass(Employee, TypeAlias)
|
||||||
|
|
|
||||||
|
|
@ -165,7 +165,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
|
||||||
if (isinstance(arg, _GenericAlias) and
|
if (isinstance(arg, _GenericAlias) and
|
||||||
arg.__origin__ in invalid_generic_forms):
|
arg.__origin__ in invalid_generic_forms):
|
||||||
raise TypeError(f"{arg} is not valid as type argument")
|
raise TypeError(f"{arg} is not valid as type argument")
|
||||||
if arg in (Any, NoReturn, Final):
|
if arg in (Any, NoReturn, Final, TypeAlias):
|
||||||
return arg
|
return arg
|
||||||
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
|
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
|
||||||
raise TypeError(f"Plain {arg} is not valid as type argument")
|
raise TypeError(f"Plain {arg} is not valid as type argument")
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
In :func:`typing.get_type_hints`, support evaluating bare stringified ``TypeAlias`` annotations. Patch by Gregory Beauregard.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue