Skip to content

Commit 3e8f2b6

Browse files
committed
Add runtime test
1 parent b670e60 commit 3e8f2b6

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

mypyc/test-data/run-generics.test

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
fn_mapping({})
31+
print("=====")
32+
fn_mapping({"a": 1, "b": 2})
33+
print("=====")
34+
35+
fn_union({"a": 1, "b": 2})
36+
print("=====")
37+
fn_union({"a": "1", "b": "2"})
38+
print("=====")
39+
40+
orig: Union[Dict[str, int], Dict[str, str]] = {"a": 1, "b": 2}
41+
fn_union(orig)
42+
print("=====")
43+
44+
td: TD = {"foo": 1}
45+
fn_typeddict(td)
46+
[typing fixtures/typing-full.pyi]
47+
[out]
48+
\[]
49+
\[]
50+
\[]
51+
{}
52+
=====
53+
\['a', 'b']
54+
\[1, 2]
55+
\['a', 'b']
56+
{'a': 1, 'b': 2}
57+
=====
58+
\['a', 'b']
59+
\[1, 2]
60+
\['a', 'b']
61+
{'a': 1, 'b': 2}
62+
=====
63+
\['a', 'b']
64+
\['1', '2']
65+
\['a', 'b']
66+
{'a': '1', 'b': '2'}
67+
=====
68+
\['a', 'b']
69+
\[1, 2]
70+
\['a', 'b']
71+
{'a': 1, 'b': 2}
72+
=====
73+
\['foo']
74+
\[1]
75+
\['foo']
76+
{'foo': 1}
77+
78+
[case testParamSpecComponentsAreUsable]
79+
from typing import Callable, ParamSpec
80+
81+
P = ParamSpec("P")
82+
83+
def deco(func: Callable[P, int]) -> Callable[P, int]:
84+
def inner(*args: P.args, **kwargs: P.kwargs) -> int:
85+
print([x for x in args])
86+
print({k: v for k, v in kwargs.items()})
87+
print(list(kwargs))
88+
print(list(kwargs.keys()))
89+
print(list(kwargs.values()))
90+
return func(*args, **kwargs)
91+
92+
return inner
93+
94+
@deco
95+
def f(x: int, y: str) -> int:
96+
return x
97+
98+
assert f(1, 'a') == 1
99+
assert f(2, y='b') == 2
100+
[out]
101+
[1, 'a']
102+
{}
103+
[]
104+
[]
105+
[]
106+
[2]
107+
{'y': 'b'}
108+
['y']
109+
['y']
110+
['b']

mypyc/test/test_run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"run-classes.test",
6262
"run-traits.test",
6363
"run-generators.test",
64+
"run-generics.test",
6465
"run-multimodule.test",
6566
"run-bench.test",
6667
"run-mypy-sim.test",

0 commit comments

Comments
 (0)