Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81b80ba

Browse files
ddfishergvanrossum
authored andcommitted
Annotate yet more test code (#2671)
1 parent ae7959d commit 81b80ba

6 files changed

Lines changed: 67 additions & 68 deletions

File tree

mypy.ini

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,3 @@ disallow_untyped_defs = False
2525

2626
[mypy-mypy.test.testpythoneval]
2727
disallow_untyped_defs = False
28-
29-
[mypy-mypy.test.testparse]
30-
disallow_untyped_defs = False
31-
32-
[mypy-mypy.test.testinfer]
33-
disallow_untyped_defs = False
34-
35-
[mypy-mypy.test.testtypegen]
36-
disallow_untyped_defs = False
37-
38-
[mypy-mypy.test.testtransform]
39-
disallow_untyped_defs = False

mypy/test/testinfer.py

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
"""Test cases for type inference helper functions."""
22

3-
import typing
3+
from typing import List, Optional, Tuple, Union
44

55
from mypy.myunit import Suite, assert_equal, assert_true
66
from mypy.checkexpr import map_actuals_to_formals
77
from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, ARG_NAMED
8-
from mypy.types import AnyType, TupleType
8+
from mypy.types import AnyType, TupleType, Type
99

1010

1111
class MapActualsToFormalsSuite(Suite):
1212
"""Test cases for checkexpr.map_actuals_to_formals."""
1313

14-
def test_basic(self):
14+
def test_basic(self) -> None:
1515
self.assert_map([], [], [])
1616

17-
def test_positional_only(self):
17+
def test_positional_only(self) -> None:
1818
self.assert_map([ARG_POS],
1919
[ARG_POS],
2020
[[0]])
2121
self.assert_map([ARG_POS, ARG_POS],
2222
[ARG_POS, ARG_POS],
2323
[[0], [1]])
2424

25-
def test_optional(self):
25+
def test_optional(self) -> None:
2626
self.assert_map([],
2727
[ARG_OPT],
2828
[[]])
@@ -33,7 +33,7 @@ def test_optional(self):
3333
[ARG_OPT, ARG_OPT],
3434
[[0], []])
3535

36-
def test_callee_star(self):
36+
def test_callee_star(self) -> None:
3737
self.assert_map([],
3838
[ARG_STAR],
3939
[[]])
@@ -44,7 +44,7 @@ def test_callee_star(self):
4444
[ARG_STAR],
4545
[[0, 1]])
4646

47-
def test_caller_star(self):
47+
def test_caller_star(self) -> None:
4848
self.assert_map([ARG_STAR],
4949
[ARG_STAR],
5050
[[0]])
@@ -58,7 +58,7 @@ def test_caller_star(self):
5858
[ARG_OPT, ARG_STAR],
5959
[[0], [0]])
6060

61-
def test_too_many_caller_args(self):
61+
def test_too_many_caller_args(self) -> None:
6262
self.assert_map([ARG_POS],
6363
[],
6464
[])
@@ -69,7 +69,7 @@ def test_too_many_caller_args(self):
6969
[ARG_POS],
7070
[[0]])
7171

72-
def test_tuple_star(self):
72+
def test_tuple_star(self) -> None:
7373
self.assert_vararg_map(
7474
[ARG_STAR],
7575
[ARG_POS],
@@ -86,10 +86,10 @@ def test_tuple_star(self):
8686
[[0], [0], []],
8787
self.tuple(AnyType(), AnyType()))
8888

89-
def tuple(self, *args):
90-
return TupleType(args, None)
89+
def tuple(self, *args: Type) -> TupleType:
90+
return TupleType(list(args), None)
9191

92-
def test_named_args(self):
92+
def test_named_args(self) -> None:
9393
self.assert_map(
9494
['x'],
9595
[(ARG_POS, 'x')],
@@ -99,25 +99,25 @@ def test_named_args(self):
9999
[(ARG_POS, 'x'), (ARG_POS, 'y')],
100100
[[1], [0]])
101101

102-
def test_some_named_args(self):
102+
def test_some_named_args(self) -> None:
103103
self.assert_map(
104104
['y'],
105105
[(ARG_OPT, 'x'), (ARG_OPT, 'y'), (ARG_OPT, 'z')],
106106
[[], [0], []])
107107

108-
def test_missing_named_arg(self):
108+
def test_missing_named_arg(self) -> None:
109109
self.assert_map(
110110
['y'],
111111
[(ARG_OPT, 'x')],
112112
[[]])
113113

114-
def test_duplicate_named_arg(self):
114+
def test_duplicate_named_arg(self) -> None:
115115
self.assert_map(
116116
['x', 'x'],
117117
[(ARG_OPT, 'x')],
118118
[[0, 1]])
119119

120-
def test_varargs_and_bare_asterisk(self):
120+
def test_varargs_and_bare_asterisk(self) -> None:
121121
self.assert_map(
122122
[ARG_STAR],
123123
[ARG_STAR, (ARG_NAMED, 'x')],
@@ -127,7 +127,7 @@ def test_varargs_and_bare_asterisk(self):
127127
[ARG_STAR, (ARG_NAMED, 'x')],
128128
[[0], [1]])
129129

130-
def test_keyword_varargs(self):
130+
def test_keyword_varargs(self) -> None:
131131
self.assert_map(
132132
['x'],
133133
[ARG_STAR2],
@@ -145,13 +145,13 @@ def test_keyword_varargs(self):
145145
[(ARG_POS, 'x'), ARG_STAR2],
146146
[[0], [1]])
147147

148-
def test_both_kinds_of_varargs(self):
148+
def test_both_kinds_of_varargs(self) -> None:
149149
self.assert_map(
150150
[ARG_STAR, ARG_STAR2],
151151
[(ARG_POS, 'x'), (ARG_POS, 'y')],
152152
[[0, 1], [0, 1]])
153153

154-
def test_special_cases(self):
154+
def test_special_cases(self) -> None:
155155
self.assert_map([ARG_STAR],
156156
[ARG_STAR, ARG_STAR2],
157157
[[0], []])
@@ -165,9 +165,13 @@ def test_special_cases(self):
165165
[ARG_STAR2],
166166
[[0]])
167167

168-
def assert_map(self, caller_kinds, callee_kinds, expected):
169-
caller_kinds, caller_names = expand_caller_kinds(caller_kinds)
170-
callee_kinds, callee_names = expand_callee_kinds(callee_kinds)
168+
def assert_map(self,
169+
caller_kinds_: List[Union[int, str]],
170+
callee_kinds_: List[Union[int, Tuple[int, str]]],
171+
expected: List[List[int]],
172+
) -> None:
173+
caller_kinds, caller_names = expand_caller_kinds(caller_kinds_)
174+
callee_kinds, callee_names = expand_callee_kinds(callee_kinds_)
171175
result = map_actuals_to_formals(
172176
caller_kinds,
173177
caller_names,
@@ -176,8 +180,12 @@ def assert_map(self, caller_kinds, callee_kinds, expected):
176180
lambda i: AnyType())
177181
assert_equal(result, expected)
178182

179-
def assert_vararg_map(self, caller_kinds, callee_kinds, expected,
180-
vararg_type):
183+
def assert_vararg_map(self,
184+
caller_kinds: List[int],
185+
callee_kinds: List[int],
186+
expected: List[List[int]],
187+
vararg_type: Type,
188+
) -> None:
181189
result = map_actuals_to_formals(
182190
caller_kinds,
183191
[],
@@ -187,7 +195,8 @@ def assert_vararg_map(self, caller_kinds, callee_kinds, expected,
187195
assert_equal(result, expected)
188196

189197

190-
def expand_caller_kinds(kinds_or_names):
198+
def expand_caller_kinds(kinds_or_names: List[Union[int, str]]
199+
) -> Tuple[List[int], List[Optional[str]]]:
191200
kinds = []
192201
names = []
193202
for k in kinds_or_names:
@@ -200,7 +209,8 @@ def expand_caller_kinds(kinds_or_names):
200209
return kinds, names
201210

202211

203-
def expand_callee_kinds(kinds_and_names):
212+
def expand_callee_kinds(kinds_and_names: List[Union[int, Tuple[int, str]]]
213+
) -> Tuple[List[int], List[Optional[str]]]:
204214
kinds = []
205215
names = []
206216
for v in kinds_and_names:

mypy/test/testparse.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import os.path
44

5-
import typing
5+
from typing import List
66

77
from mypy import defaults
88
from mypy.myunit import Suite, AssertionFailure
99
from mypy.test.helpers import assert_string_arrays_equal
10-
from mypy.test.data import parse_test_cases
10+
from mypy.test.data import parse_test_cases, DataDrivenTestCase
1111
from mypy.test import config
1212
from mypy.parse import parse
1313
from mypy.errors import CompileError
@@ -18,16 +18,16 @@ class ParserSuite(Suite):
1818
parse_files = ['parse.test',
1919
'parse-python2.test']
2020

21-
def cases(self):
21+
def cases(self) -> List[DataDrivenTestCase]:
2222
# The test case descriptions are stored in data files.
23-
c = []
23+
c = [] # type: List[DataDrivenTestCase]
2424
for f in self.parse_files:
2525
c += parse_test_cases(
2626
os.path.join(config.test_data_prefix, f), test_parser)
2727
return c
2828

2929

30-
def test_parser(testcase):
30+
def test_parser(testcase: DataDrivenTestCase) -> None:
3131
"""Perform a single parser test case.
3232
3333
The argument contains the description of the test case.
@@ -58,14 +58,14 @@ def test_parser(testcase):
5858

5959

6060
class ParseErrorSuite(Suite):
61-
def cases(self):
61+
def cases(self) -> List[DataDrivenTestCase]:
6262
# Test case descriptions are in an external file.
6363
return parse_test_cases(os.path.join(config.test_data_prefix,
6464
'parse-errors.test'),
6565
test_parse_error)
6666

6767

68-
def test_parse_error(testcase):
68+
def test_parse_error(testcase: DataDrivenTestCase) -> None:
6969
try:
7070
# Compile temporary file. The test file contains non-ASCII characters.
7171
parse(bytes('\n'.join(testcase.input), 'utf-8'), INPUT_FILE_NAME, None, Options())

mypy/test/testtransform.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
from mypy.build import BuildSource
99
from mypy.myunit import Suite
1010
from mypy.test.helpers import assert_string_arrays_equal, testfile_pyversion
11-
from mypy.test.data import parse_test_cases
11+
from mypy.test.data import parse_test_cases, DataDrivenTestCase
1212
from mypy.test.config import test_data_prefix, test_temp_dir
1313
from mypy.errors import CompileError
1414
from mypy.nodes import TypeInfo
1515
from mypy.treetransform import TransformVisitor
16+
from mypy.types import Type
1617
from mypy.options import Options
1718

1819

@@ -27,8 +28,8 @@ class TransformSuite(Suite):
2728
'semanal-abstractclasses.test',
2829
'semanal-python2.test']
2930

30-
def cases(self):
31-
c = []
31+
def cases(self) -> List[DataDrivenTestCase]:
32+
c = [] # type: List[DataDrivenTestCase]
3233
for f in self.transform_files:
3334
c += parse_test_cases(os.path.join(test_data_prefix, f),
3435
test_transform,
@@ -37,7 +38,7 @@ def cases(self):
3738
return c
3839

3940

40-
def test_transform(testcase):
41+
def test_transform(testcase: DataDrivenTestCase) -> None:
4142
"""Perform an identity transform test case."""
4243

4344
try:
@@ -79,6 +80,6 @@ def test_transform(testcase):
7980

8081

8182
class TestTransformVisitor(TransformVisitor):
82-
def type(self, type):
83+
def type(self, type: Type) -> Type:
8384
assert type is not None
8485
return type

mypy/test/testtypegen.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
import os.path
44
import re
55

6-
import typing
6+
from typing import Set, List
77

88
from mypy import build
99
from mypy.build import BuildSource
1010
from mypy.myunit import Suite
1111
from mypy.test import config
12-
from mypy.test.data import parse_test_cases
12+
from mypy.test.data import parse_test_cases, DataDrivenTestCase
1313
from mypy.test.helpers import assert_string_arrays_equal
1414
from mypy.util import short_type
15-
from mypy.nodes import NameExpr, TypeVarExpr, CallExpr
15+
from mypy.nodes import (
16+
NameExpr, TypeVarExpr, CallExpr, Expression, MypyFile, AssignmentStmt, IntExpr
17+
)
1618
from mypy.traverser import TraverserVisitor
1719
from mypy.errors import CompileError
1820
from mypy.options import Options
@@ -22,14 +24,14 @@ class TypeExportSuite(Suite):
2224
# List of files that contain test case descriptions.
2325
files = ['typexport-basic.test']
2426

25-
def cases(self):
26-
c = []
27+
def cases(self) -> List[DataDrivenTestCase]:
28+
c = [] # type: List[DataDrivenTestCase]
2729
for f in self.files:
2830
c += parse_test_cases(os.path.join(config.test_data_prefix, f),
2931
self.run_test, config.test_temp_dir)
3032
return c
3133

32-
def run_test(self, testcase):
34+
def run_test(self, testcase: DataDrivenTestCase) -> None:
3335
try:
3436
line = testcase.input[0]
3537
mask = ''
@@ -81,32 +83,33 @@ def run_test(self, testcase):
8183

8284

8385
class SkippedNodeSearcher(TraverserVisitor):
84-
def __init__(self):
85-
self.nodes = set()
86+
def __init__(self) -> None:
87+
self.nodes = set() # type: Set[Expression]
88+
self.is_typing = False
8689

87-
def visit_mypy_file(self, f):
90+
def visit_mypy_file(self, f: MypyFile) -> None:
8891
self.is_typing = f.fullname() == 'typing'
8992
super().visit_mypy_file(f)
9093

91-
def visit_assignment_stmt(self, s):
94+
def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
9295
if s.type or ignore_node(s.rvalue):
9396
for lvalue in s.lvalues:
9497
if isinstance(lvalue, NameExpr):
9598
self.nodes.add(lvalue)
9699
super().visit_assignment_stmt(s)
97100

98-
def visit_name_expr(self, n):
101+
def visit_name_expr(self, n: NameExpr) -> None:
99102
self.skip_if_typing(n)
100103

101-
def visit_int_expr(self, n):
104+
def visit_int_expr(self, n: IntExpr) -> None:
102105
self.skip_if_typing(n)
103106

104-
def skip_if_typing(self, n):
107+
def skip_if_typing(self, n: Expression) -> None:
105108
if self.is_typing:
106109
self.nodes.add(n)
107110

108111

109-
def ignore_node(node):
112+
def ignore_node(node: Expression) -> bool:
110113
"""Return True if node is to be omitted from test case output."""
111114

112115
# We want to get rid of object() expressions in the typing module stub

mypy_strict_optional.ini

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
; This allows us to make mypy strict Optional compliant over time.
33
[mypy]
44
strict_optional = True
5-
show_none_errors = False
6-
7-
[mypy-mypy.test.*]
8-
show_none_errors = True
5+
ignore_errors = True

0 commit comments

Comments
 (0)