Skip to content

Commit a195879

Browse files
committed
Better test cases for __type_params__ & owner
1 parent fb992e8 commit a195879

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/test_typing_extensions.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8125,30 +8125,21 @@ def test_evaluate_forward_refs(self):
81258125
T_local_type_params = (T_nonlocal,)
81268126
del T_nonlocal
81278127

8128-
class _EasyStr:
8129-
def __str__(self):
8130-
return self.__class__.__name__
8131-
def __repr__(self) -> str:
8132-
return str(self)
8133-
8134-
class X(_EasyStr):
8128+
class X:
81358129
T_in_Y = object()
81368130

81378131
T_in_Y = TypeVar("T_in_Y")
8138-
class Y(Generic[T_in_Y], _EasyStr):
8132+
class Y(Generic[T_in_Y]):
81398133
a = "X"
81408134
bT = "Y[T_nonlocal]"
8141-
alias = int
8142-
8143-
# workaround for type statement
8144-
__type_params__ = (T_in_Y,)
8145-
Y.T_in_Y = T_in_Y
8135+
# Construct which has __type_params__
8136+
Z = TypeAliasType("Z", Y[T_in_Y], type_params=(T_in_Y,))
8137+
Y_type_params = (T_in_Y,)
8138+
del T_in_Y
81468139

81478140
# Assure that these are not in globals
81488141
assert X.__name__ not in globals()
81498142
assert Y.__name__ not in globals()
8150-
self.assertEqual(Y.__type_params__, (T_in_Y,))
8151-
del T_in_Y
81528143

81538144
minimal_locals = {
81548145
#"Y": Y,
@@ -8165,6 +8156,9 @@ class Y(Generic[T_in_Y], _EasyStr):
81658156

81668157
cases = {
81678158
# values can be types, dicts or list of dicts.
8159+
# Combination of all "skip_if" subdicts should provide complete coverage,
8160+
# i.e. they should come in pairs.
8161+
# For not complete coverage set the value to "Skip: message".
81688162
None: {
81698163
Format.VALUE: NoneType,
81708164
Format.FORWARDREF: NoneType,
@@ -8279,7 +8273,8 @@ class Y(Generic[T_in_Y], _EasyStr):
82798273
Format.STRING: "Y.bT",
82808274
},
82818275
],
8282-
# Special cases for _type_check
8276+
# Special cases for _type_check. Note depending on is_class and is_argument
8277+
# Those will raise a TypeError, expected is converted.
82838278
ClassVar[None]: ClassVar[None],
82848279
Final[None]: Final[None],
82858280
Protocol[T]: Protocol[T],
@@ -8311,12 +8306,13 @@ class Y(Generic[T_in_Y], _EasyStr):
83118306
Format.FORWARDREF: TypeError,
83128307
Format.STRING: "Generic",
83138308
},
8309+
# Class members
83148310
"T_in_Y": [
83158311
{
83168312
"owner": None,
8317-
"type_params": Y.__type_params__,
8318-
Format.VALUE: Y.__type_params__[0],
8319-
Format.FORWARDREF: Y.__type_params__[0],
8313+
"type_params": Y_type_params,
8314+
Format.VALUE: Y_type_params[0],
8315+
Format.FORWARDREF: Y_type_params[0],
83208316
Format.STRING: "T_in_Y",
83218317
},
83228318
{
@@ -8329,15 +8325,22 @@ class Y(Generic[T_in_Y], _EasyStr):
83298325
{
83308326
"owner": Y,
83318327
"type_params": None,
8332-
Format.VALUE: Y.__type_params__[0],
8333-
Format.FORWARDREF: Y.__type_params__[0],
8328+
Format.VALUE: NameError,
8329+
Format.FORWARDREF: ForwardRef("T_in_Y"),
8330+
Format.STRING: "T_in_Y",
8331+
},
8332+
{
8333+
"owner": Z,
8334+
"type_params": None,
8335+
Format.VALUE: Y_type_params[0],
8336+
Format.FORWARDREF: Y_type_params[0],
83348337
Format.STRING: "T_in_Y",
83358338
},
83368339
{
83378340
"owner": Y,
8338-
"type_params": Y.__type_params__,
8339-
Format.VALUE: Y.__type_params__[0],
8340-
Format.FORWARDREF: Y.__type_params__[0],
8341+
"type_params": Y_type_params,
8342+
Format.VALUE: Y_type_params[0],
8343+
Format.FORWARDREF: Y_type_params[0],
83418344
Format.STRING: "T_in_Y",
83428345
},
83438346
{
@@ -8356,9 +8359,7 @@ def _unroll_subcase(
83568359
owner = expected.get("owner", None)
83578360
type_params = expected.get("type_params", None)
83588361
skip_if = expected.get("skip_if", False)
8359-
if format not in expected:
8360-
return
8361-
if skip_if:
8362+
if skip_if: # Should only be used in list of dicts
83628363
L = locals()
83638364
skip = all(
83648365
# Check skip_if; for localns check only truthy value
@@ -8379,6 +8380,7 @@ def _unroll_subcase(
83798380
owner,
83808381
)
83818382
def unroll_cases(cases, outer_locals):
8383+
"""Generator to yield all test cases"""
83828384
for (
83838385
annot,
83848386
expected,
@@ -8413,10 +8415,9 @@ def unroll_cases(cases, outer_locals):
84138415
globalns,
84148416
localns,
84158417
)
8416-
if case is not None:
8417-
yield case
8418+
yield case
84188419
# Multiple cases depending on other parameters
8419-
elif isinstance(expected, list):
8420+
elif type(expected) is list:
84208421
yield from filter(
84218422
None,
84228423
(
@@ -8482,6 +8483,7 @@ def unroll_cases(cases, outer_locals):
84828483
type_params=type_params,
84838484
globals=bool(globalns),
84848485
locals=bool(localns),
8486+
owner=owner,
84858487
):
84868488
if isinstance(expected, str) and expected.lower().startswith("skip"):
84878489
self.skipTest(expected)

0 commit comments

Comments
 (0)