8000 [mypyc] Group related tests together in separate test files by JukkaL · Pull Request #9130 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
/ mypy Public

[mypyc] Group related tests together in separate test files #9130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move some dict run tests to a separate file
  • Loading branch information
JukkaL committed Jul 11, 2020
commit 2c54f480da5c9bc41cbc5b9b1b89a2bd44fe6861
194 changes: 194 additions & 0 deletions mypyc/test-data/run-dicts.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# Dict test cases (compile and run)

[case testDictStuff]
from typing import Dict, Any, List, Set, Tuple
from defaultdictwrap import make_dict

def f(x: int) -> int:
dict1 = {} # type: Dict[int, int]
dict1[1] = 1
dict2 = {} # type: Dict[int, int]
dict2[x] = 2
dict1.update(dict2)

l = [(5, 2)] # type: Any
dict1.update(l)
d2 = {6: 4} # type: Any
dict1.update(d2)

return dict1[1]

def g() -> int:
d = make_dict()
d['a'] = 10
d['a'] += 10
d['b'] += 10
l = [('c', 2)] # type: Any
d.update(l)
d2 = {'d': 4} # type: Any
d.update(d2)
return d['a'] + d['b']

def h() -> None:
d = {} # type: Dict[Any, Any]
d[{}]

def update_dict(x: Dict[Any, Any], y: Any):
x.update(y)

def make_dict1(x: Any) -> Dict[Any, Any]:
return dict(x)

def make_dict2(x: Dict[Any, Any]) -> Dict[Any, Any]:
return dict(x)

def u(x: int) -> int:
d = {} # type: Dict[str, int]
d.update(x=x)
return d['x']

def get_content(d: Dict[int, int]) -> Tuple[List[int], List[int], List[Tuple[int, int]]]:
return list(d.keys()), list(d.values()), list(d.items())

def get_content_set(d: Dict[int, int]) -> Tuple[Set[int], Set[int], Set[Tuple[int, int]]]:
return set(d.keys()), set(d.values()), set(d.items())
[file defaultdictwrap.py]
from typing import Dict
from collections import defaultdict # type: ignore
def make_dict() -> Dict[str, int]:
return defaultdict(int)

[file driver.py]
from collections import OrderedDict
from native import (
f, g, h, u, make_dict1, make_dict2, update_dict, get_content, get_content_set
)
assert f(1) == 2
assert f(2) == 1
assert g() == 30
# Make sure we get a TypeError from indexing with unhashable and not KeyError
try:
h()
except TypeError:
pass
else:
assert False
d = {'a': 1, 'b': 2}
assert make_dict1(d) == d
assert make_dict1(d.items()) == d
assert make_dict2(d) == d
# object.__dict__ is a "mappingproxy" and not a dict
assert make_dict1(object.__dict__) == dict(object.__dict__)
d = {}
update_dict(d, object.__dict__)
assert d == dict(object.__dict__)

assert u(10) == 10
assert get_content({1: 2}) == ([1], [2], [(1, 2)])
od = OrderedDict([(1, 2), (3, 4)])
assert get_content(od) == ([1, 3], [2, 4], [(1, 2), (3, 4)])
od.move_to_end(1)
assert get_content(od) == ([3, 1], [4, 2], [(3, 4), (1, 2)])
assert get_content_set({1: 2}) == ({1}, {2}, {(1, 2)})
assert get_content_set(od) == ({1, 3}, {2, 4}, {(1, 2), (3, 4)})
[typing fixtures/typing-full.pyi]

[case testDictIterationMethodsRun]
from typing import Dict
def print_dict_methods(d1: Dict[int, int],
d2: Dict[int, int],
d3: Dict[int, int]) -> None:
for k in d1.keys():
print(k)
for k, v in d2.items():
print(k)
print(v)
for v in d3.values():
print(v)

def clear_during_iter(d: Dict[int, int]) -> None:
for k in d:
d.clear()

class Custom(Dict[int, int]): pass
[file driver.py]
from native import print_dict_methods, Custom, clear_during_iter
from collections import OrderedDict
print_dict_methods({}, {}, {})
print_dict_methods({1: 2}, {3: 4, 5: 6}, {7: 8})
print('==')
c = Custom({0: 1})
print_dict_methods(c, c, c)
print('==')
d = OrderedDict([(1, 2), (3, 4)])
print_dict_methods(d, d, d)
print('==')
d.move_to_end(1)
print_dict_methods(d, d, d)
clear_during_iter({}) # OK
try:
clear_during_iter({1: 2, 3: 4})
except RuntimeError as e:
assert str(e) == "dictionary changed size during iteration"
else:
assert False
try:
clear_during_iter(d)
except RuntimeError as e:
assert str(e) == "OrderedDict changed size during iteration"
else:
assert False

class CustomMad(dict):
def __iter__(self):
return self
def __next__(self):
raise ValueError
m = CustomMad()
try:
clear_during_iter(m)
except ValueError:
pass
else:
assert False

class CustomBad(dict):
def items(self):
return [(1, 2, 3)] # Oops
b = CustomBad()
try:
print_dict_methods(b, b, b)
except TypeError as e:
assert str(e) == "a tuple of length 2 expected"
else:
assert False
[out]
1
3
4
5
6
8
==
0
0
1
1
==
1
3
1
2
3
4
2
4
==
3
1
3
4
1
2
4
2
193 changes: 0 additions & 193 deletions mypyc/test-data/run.test
Original file line number Diff line number Diff line change
Expand Up @@ -707,199 +707,6 @@ s = {1, 2, 3}
update(s, [5, 4, 3])
assert s == {1, 2, 3, 4, 5}

[case testDictStuff]
from typing import Dict, Any, List, Set, Tuple
from defaultdictwrap import make_dict

def f(x: int) -> int:
dict1 = {} # type: Dict[int, int]
dict1[1] = 1
dict2 = {} # type: Dict[int, int]
dict2[x] = 2
dict1.update(dict2)

l = [(5, 2)] # type: Any
dict1.update(l)
d2 = {6: 4} # type: Any
dict1.update(d2)

return dict1[1]

def g() -> int:
d = make_dict()
d['a'] = 10
d['a'] += 10
d['b'] += 10
l = [('c', 2)] # type: Any
d.update(l)
d2 = {'d': 4} # type: Any
d.update(d2)
return d['a'] + d['b']

def h() -> None:
d = {} # type: Dict[Any, Any]
d[{}]

def update_dict(x: Dict[Any, Any], y: Any):
x.update(y)

def make_dict1(x: Any) -> Dict[Any, Any]:
return dict(x)

def make_dict2(x: Dict[Any, Any]) -> Dict[Any, Any]:
return dict(x)

def u(x: int) -> int:
d = {} # type: Dict[str, int]
d.update(x=x)
return d['x']

def get_content(d: Dict[int, int]) -> Tuple[List[int], List[int], List[Tuple[int, int]]]:
return list(d.keys()), list(d.values()), list(d.items())

def get_content_set(d: Dict[int, int]) -> Tuple[Set[int], Set[int], Set[Tuple[int, int]]]:
return set(d.keys()), set(d.values()), set(d.items())
[file defaultdictwrap.py]
from typing import Dict
from collections import defaultdict # type: ignore
def make_dict() -> Dict[str, int]:
return defaultdict(int)

[file driver.py]
from collections import OrderedDict
from native import (
f, g, h, u, make_dict1, make_dict2, update_dict, get_content, get_content_set
)
assert f(1) == 2
assert f(2) == 1
assert g() == 30
# Make sure we get a TypeError from indexing with unhashable and not KeyError
try:
h()
except TypeError:
pass
else:
assert False
d = {'a': 1, 'b': 2}
assert make_dict1(d) == d
assert make_dict1(d.items()) == d
assert make_dict2(d) == d
# object.__ 628C dict__ is a "mappingproxy" and not a dict
assert make_dict1(object.__dict__) == dict(object.__dict__)
d = {}
update_dict(d, object.__dict__)
assert d == dict(object.__dict__)

assert u(10) == 10
assert get_content({1: 2}) == ([1], [2], [(1, 2)])
od = OrderedDict([(1, 2), (3, 4)])
assert get_content(od) == ([1, 3], [2, 4], [(1, 2), (3, 4)])
od.move_to_end(1)
assert get_content(od) == ([3, 1], [4, 2], [(3, 4), (1, 2)])
assert get_content_set({1: 2}) == ({1}, {2}, {(1, 2)})
assert get_content_set(od) == ({1, 3}, {2, 4}, {(1, 2), (3, 4)})
[typing fixtures/typing-full.pyi]

[case testDictIterationMethodsRun]
from typing import Dict
def print_dict_methods(d1: Dict[int, int],
d2: Dict[int, int],
d3: Dict[int, int]) -> None:
for k in d1.keys():
print(k)
for k, v in d2.items():
print(k)
print(v)
for v in d3.values():
print(v)

def clear_during_iter(d: Dict[int, int]) -> None:
for k in d:
d.clear()

class Custom(Dict[int, int]): pass
[file driver.py]
from native import print_dict_methods, Custom, clear_during_iter
from collections import OrderedDict
print_dict_methods({}, {}, {})
print_dict_methods({1: 2}, {3: 4, 5: 6}, {7: 8})
print('==')
c = Custom({0: 1})
print_dict_methods(c, c, c)
print('==')
d = OrderedDict([(1, 2), (3, 4)])
print_dict_methods(d, d, d)
print('==')
d.move_to_end(1)
print_dict_methods(d, d, d)
clear_during_iter({}) # OK
try:
clear_during_iter({1: 2, 3: 4})
except RuntimeError as e:
assert str(e) == "dictionary changed size during iteration"
else:
assert False
try:
clear_during_iter(d)
except RuntimeError as e:
assert str(e) == "OrderedDict changed size during iteration"
else:
assert False

class CustomMad(dict):
def __iter__(self):
return self
def __next__(self):
raise ValueError
m = CustomMad()
try:
clear_during_iter(m)
except ValueError:
pass
else:
assert False

class CustomBad(dict):
def items(self):
return [(1, 2, 3)] # Oops
b = CustomBad()
try:
print_dict_methods(b, b, b)
except TypeError as e:
assert str(e) == "a tuple of length 2 expected"
else:
assert False
[out]
1
3
4
5
6
8
==
0
0
1
1
==
1
3
1
2
3
4
2
4
==
3
1
3
4
1
2
4
2

[case testPyMethodCall]
from typing import List
def f(x: List[int]) -> int:
Expand Down
Loading
0