-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrun-generics.test
More file actions
113 lines (98 loc) · 2.12 KB
/
run-generics.test
File metadata and controls
113 lines (98 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
[case testTypeVarMappingBound]
# Dicts are special-cased for efficient iteration.
from typing import Dict, TypedDict, TypeVar, Union
class TD(TypedDict):
foo: int
M = TypeVar("M", bound=Dict[str, int])
U = TypeVar("U", bound=Union[Dict[str, int], Dict[str, str]])
T = TypeVar("T", bound=TD)
def fn_mapping(m: M) -> None:
print([x for x in m])
print([x for x in m.values()])
print([x for x in m.keys()])
print({k: v for k, v in m.items()})
def fn_union(m: U) -> None:
print([x for x in m])
print([x for x in m.values()])
print([x for x in m.keys()])
print({k: v for k, v in m.items()})
def fn_typeddict(t: T) -> None:
print([x for x in t])
print([x for x in t.values()])
print([x for x in t.keys()])
print({k: v for k, v in t.items()})
def test_mapping() -> None:
fn_mapping({})
print("=====")
fn_mapping({"a": 1, "b": 2})
print("=====")
fn_union({"a": 1, "b": 2})
print("=====")
fn_union({"a": "1", "b": "2"})
print("=====")
orig: Union[Dict[str, int], Dict[str, str]] = {"a": 1, "b": 2}
fn_union(orig)
print("=====")
td: TD = {"foo": 1}
fn_typeddict(td)
[typing fixtures/typing-full.pyi]
[out]
\[]
\[]
\[]
{}
=====
\['a', 'b']
\[1, 2]
\['a', 'b']
{'a': 1, 'b': 2}
=====
\['a', 'b']
\[1, 2]
\['a', 'b']
{'a': 1, 'b': 2}
=====
\['a', 'b']
\['1', '2']
\['a', 'b']
{'a': '1', 'b': '2'}
=====
\['a', 'b']
\[1, 2]
\['a', 'b']
{'a': 1, 'b': 2}
=====
\['foo']
\[1]
\['foo']
{'foo': 1}
[case testParamSpecComponentsAreUsable]
from typing import Callable
from typing_extensions import ParamSpec
P = ParamSpec("P")
def deco(func: Callable[P, int]) -> Callable[P, int]:
def inner(*args: P.args, **kwargs: P.kwargs) -> int:
print([x for x in args])
print({k: v for k, v in kwargs.items()})
print(list(kwargs))
print(list(kwargs.keys()))
print(list(kwargs.values()))
return func(*args, **kwargs)
return inner
@deco
def f(x: int, y: str) -> int:
return x
def test_usable() -> None:
assert f(1, 'a') == 1
assert f(2, y='b') == 2
[out]
\[1, 'a']
{}
\[]
\[]
\[]
\[2]
{'y': 'b'}
\['y']
\['y']
\['b']