Debian Codegraph @

mypyc/test-data/run-generics.test

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