Skip to content

Commit 67059fd

Browse files
committed
Rework tests of nested strings
1 parent 8cae202 commit 67059fd

File tree

1 file changed

+38
-72
lines changed

1 file changed

+38
-72
lines changed

src/test_typing_extensions.py

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8649,77 +8649,6 @@ def unroll_cases(cases):
86498649
Format.FORWARDREF: Optional[str],
86508650
Format.STRING: str(Union[str, None, "str"]),
86518651
},
8652-
# Classes and members
8653-
"X": X,
8654-
"Y[X]": {
8655-
Format.VALUE: Y[X],
8656-
Format.FORWARDREF: Y[X],
8657-
Format.STRING: "Y[X]",
8658-
},
8659-
"Y[T_nonlocal]": [
8660-
{
8661-
"type_params": (T,), # check with wrong TypeVar
8662-
Format.VALUE: NameError,
8663-
Format.FORWARDREF: typing.ForwardRef("Y[T_nonlocal]"),
8664-
Format.STRING: "Y[T_nonlocal]",
8665-
},
8666-
{
8667-
"type_params": (T_local, ),
8668-
Format.VALUE: Y[T_local],
8669-
Format.FORWARDREF: Y[T_local],
8670-
Format.STRING: "Y[T_nonlocal]",
8671-
},
8672-
],
8673-
"Y[Y['T']]": {
8674-
Format.VALUE: Y[Y[T]],
8675-
Format.FORWARDREF: Y[Y[T]],
8676-
Format.STRING: "Y[Y['T']]",
8677-
},
8678-
"""Y["Y['T']"]""": [
8679-
{
8680-
Format.VALUE: (
8681-
Y[Y[T]]
8682-
if sys.version_info[:2] > (3, 8)
8683-
else "Skip: nested string not supported"
8684-
),
8685-
Format.FORWARDREF: (
8686-
Y[Y[T]]
8687-
if sys.version_info[:2] > (3, 8)
8688-
else "Skip: nested string not supported"
8689-
),
8690-
Format.STRING: """Y["Y['T']"]""",
8691-
},
8692-
],
8693-
"Y[Y[T_nonlocal]]": [
8694-
{
8695-
"type_params": (T_local, ),
8696-
Format.VALUE: Y[Y[T_local]],
8697-
Format.FORWARDREF: Y[Y[T_local]],
8698-
Format.STRING: "Y[Y[T_nonlocal]]",
8699-
},
8700-
{
8701-
"type_params": None,
8702-
Format.VALUE: NameError,
8703-
Format.FORWARDREF: typing.ForwardRef("Y[Y[T_nonlocal]]"),
8704-
Format.STRING: "Y[Y[T_nonlocal]]",
8705-
},
8706-
],
8707-
"""Y['Z["StrAlias"]']""": [
8708-
{
8709-
# This will be Y[Z["StrAlias"]] in 3.8 and 3.10
8710-
Format.VALUE: (
8711-
Y[Z[str]]
8712-
if sys.version_info[:2] > (3, 10)
8713-
else "Skip: nested ForwardRef not fully resolved"
8714-
),
8715-
Format.FORWARDREF: (
8716-
Y[Z[str]]
8717-
if sys.version_info[:2] > (3, 10)
8718-
else "Skip: nested ForwardRef not fully resolved"
8719-
),
8720-
Format.STRING: """Y['Z["StrAlias"]']""",
8721-
},
8722-
],
87238652
"Y.a": [
87248653
{
87258654
"skip_if": {"localns": minimal_localns, "format": Format.FORWARDREF},
@@ -8930,7 +8859,6 @@ class Gen[Tx]:
89308859
self.assertIs(evaluate_forward_ref(typing.ForwardRef("Tx", is_class=True), owner=Gen, globals={"Tx": str}), str)
89318860
self.assertIs(evaluate_forward_ref(typing.ForwardRef("Tx", is_class=True), owner=Gen, type_params=(not_Tx,), globals={"Tx": str}), str)
89328861

8933-
89348862
with self.assertRaises(NameError):
89358863
evaluate_forward_ref(typing.ForwardRef("alias"), type_params=Gen.__type_params__)
89368864
self.assertIs(evaluate_forward_ref(typing.ForwardRef("alias"), owner=Gen), int)
@@ -9005,6 +8933,44 @@ def test_name_lookup_without_eval(self):
90058933
with self.assertRaises(NameError):
90068934
evaluate_forward_ref(typing.ForwardRef("doesntexist"))
90078935

8936+
def test_nested_strings(self):
8937+
# This variable must have a different name TypeVar
8938+
Tx = TypeVar("Tx")
8939+
8940+
class Y(Generic[Tx]):
8941+
a = "X"
8942+
bT = "Y[T_nonlocal]"
8943+
8944+
Z = TypeAliasType("Z", Y[Tx], type_params=(Tx,))
8945+
8946+
evaluated_ref1a = evaluate_forward_ref(typing.ForwardRef("Y[Y['Tx']]"), locals={"Y": Y, "Tx": Tx})
8947+
self.assertEqual(get_origin(evaluated_ref1a), Y)
8948+
self.assertEqual(get_args(evaluated_ref1a), (Y[Tx],))
8949+
8950+
with self.subTest("nested string with type_params"):
8951+
if not TYPING_3_12_0:
8952+
self.skipTest("# TODO find reason why this one fails before 3.12.?")
8953+
evaluated_ref1b = evaluate_forward_ref(
8954+
typing.ForwardRef("Y[Y['Tx']]"), locals={"Y": Y}, type_params=(Tx,)
8955+
)
8956+
self.assertEqual(get_origin(evaluated_ref1b), Y)
8957+
self.assertEqual(get_args(evaluated_ref1b), (Y[Tx],))
8958+
8959+
with self.subTest("nested string of TypeVar"):
8960+
evaluated_ref2 = evaluate_forward_ref(typing.ForwardRef("""Y["Y['Tx']"]"""), locals={"Y": Y})
8961+
self.assertEqual(get_origin(evaluated_ref2), Y)
8962+
if not TYPING_3_9_0:
8963+
self.skipTest("Nested string 'Tx' stays ForwardRef in 3.8")
8964+
self.assertEqual(get_args(evaluated_ref2), (Y[Tx],))
8965+
8966+
with self.subTest("nested string of TypeAliasType and alias"):
8967+
# NOTE: Using Y here works for 3.10
8968+
evaluated_ref3 = evaluate_forward_ref(typing.ForwardRef("""Y['Z["StrAlias"]']"""), locals={"Y": Y, "Z": Z, "StrAlias": str})
8969+
self.assertEqual(get_origin(evaluated_ref3), Y)
8970+
if sys.version_info[:2] in ((3,8), (3, 10)):
8971+
self.skipTest("Nested string 'StrAlias' is not resolved in 3.8 and 3.10")
8972+
self.assertEqual(get_args(evaluated_ref3), (Z[str],))
8973+
90088974

90098975
if __name__ == '__main__':
90108976
main()

0 commit comments

Comments
 (0)