mirror of
https://github.com/python/cpython.git
synced 2025-11-26 13:22:51 +00:00
gh-102721: Improve coverage of _collections_abc._CallableGenericAlias (#102722)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
b0ec6253c9
commit
fbe82fdd77
2 changed files with 32 additions and 14 deletions
|
|
@ -481,15 +481,8 @@ class _CallableGenericAlias(GenericAlias):
|
||||||
# rather than the default types.GenericAlias object. Most of the
|
# rather than the default types.GenericAlias object. Most of the
|
||||||
# code is copied from typing's _GenericAlias and the builtin
|
# code is copied from typing's _GenericAlias and the builtin
|
||||||
# types.GenericAlias.
|
# types.GenericAlias.
|
||||||
|
|
||||||
if not isinstance(item, tuple):
|
if not isinstance(item, tuple):
|
||||||
item = (item,)
|
item = (item,)
|
||||||
# A special case in PEP 612 where if X = Callable[P, int],
|
|
||||||
# then X[int, str] == X[[int, str]].
|
|
||||||
if (len(self.__parameters__) == 1
|
|
||||||
and _is_param_expr(self.__parameters__[0])
|
|
||||||
and item and not _is_param_expr(item[0])):
|
|
||||||
item = (item,)
|
|
||||||
|
|
||||||
new_args = super().__getitem__(item).__args__
|
new_args = super().__getitem__(item).__args__
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1921,14 +1921,29 @@ class BaseCallableTests:
|
||||||
self.assertEqual(weakref.ref(alias)(), alias)
|
self.assertEqual(weakref.ref(alias)(), alias)
|
||||||
|
|
||||||
def test_pickle(self):
|
def test_pickle(self):
|
||||||
|
global T_pickle, P_pickle, TS_pickle # needed for pickling
|
||||||
Callable = self.Callable
|
Callable = self.Callable
|
||||||
alias = Callable[[int, str], float]
|
T_pickle = TypeVar('T_pickle')
|
||||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
P_pickle = ParamSpec('P_pickle')
|
||||||
s = pickle.dumps(alias, proto)
|
TS_pickle = TypeVarTuple('TS_pickle')
|
||||||
loaded = pickle.loads(s)
|
|
||||||
self.assertEqual(alias.__origin__, loaded.__origin__)
|
samples = [
|
||||||
self.assertEqual(alias.__args__, loaded.__args__)
|
Callable[[int, str], float],
|
||||||
self.assertEqual(alias.__parameters__, loaded.__parameters__)
|
Callable[P_pickle, int],
|
||||||
|
Callable[P_pickle, T_pickle],
|
||||||
|
Callable[Concatenate[int, P_pickle], int],
|
||||||
|
Callable[Concatenate[*TS_pickle, P_pickle], int],
|
||||||
|
]
|
||||||
|
for alias in samples:
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
with self.subTest(alias=alias, proto=proto):
|
||||||
|
s = pickle.dumps(alias, proto)
|
||||||
|
loaded = pickle.loads(s)
|
||||||
|
self.assertEqual(alias.__origin__, loaded.__origin__)
|
||||||
|
self.assertEqual(alias.__args__, loaded.__args__)
|
||||||
|
self.assertEqual(alias.__parameters__, loaded.__parameters__)
|
||||||
|
|
||||||
|
del T_pickle, P_pickle, TS_pickle # cleaning up global state
|
||||||
|
|
||||||
def test_var_substitution(self):
|
def test_var_substitution(self):
|
||||||
Callable = self.Callable
|
Callable = self.Callable
|
||||||
|
|
@ -1954,6 +1969,16 @@ class BaseCallableTests:
|
||||||
self.assertEqual(C5[int, str, float],
|
self.assertEqual(C5[int, str, float],
|
||||||
Callable[[typing.List[int], tuple[str, int], float], int])
|
Callable[[typing.List[int], tuple[str, int], float], int])
|
||||||
|
|
||||||
|
def test_type_subst_error(self):
|
||||||
|
Callable = self.Callable
|
||||||
|
P = ParamSpec('P')
|
||||||
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
pat = "Expected a list of types, an ellipsis, ParamSpec, or Concatenate."
|
||||||
|
|
||||||
|
with self.assertRaisesRegex(TypeError, pat):
|
||||||
|
Callable[P, T][0, int]
|
||||||
|
|
||||||
def test_type_erasure(self):
|
def test_type_erasure(self):
|
||||||
Callable = self.Callable
|
Callable = self.Callable
|
||||||
class C1(Callable):
|
class C1(Callable):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue