8000 Add mypy.ini. Fix strict none errors in the test subpackage. (#2200) · python/mypy@20aea69 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20aea69

Browse files
authored
Add mypy.ini. Fix strict none errors in the test subpackage. (#2200)
* Fix strict none errors in the test subpackage. This shows how to deal with #2199 for `p[i].arg`. * Run mypy over itself in --strict-optional mode. Add mypy.ini to suppress most errors (for now).
1 parent 640921b commit 20aea69

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

mypy.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[mypy]
2+
show_none_errors = False
3+
4+
[mypy-mypy/test/*]
5+
show_none_errors = True

mypy/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> Optional[Cache
766766
return m
767767

768768

769-
def is_meta_fresh(meta: CacheMeta, id: str, path: str, manager: BuildManager) -> bool:
769+
def is_meta_fresh(meta: Optional[CacheMeta], id: str, path: str, manager: BuildManager) -> bool:
770770
if meta is None:
771771
return False
772772

mypy/test/data.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ def parse_test_cases(
5353
while i < len(p) and p[i].id != 'case':
5454
if p[i].id == 'file':
5555
# Record an extra file needed for the test case.
56-
files.append((os.path.join(base_path, p[i].arg),
56+
arg = p[i].arg
57+
assert arg is not None
58+
files.append((os.path.join(base_path, arg),
5759
'\n'.join(p[i].data)))
5860
elif p[i].id in ('builtins', 'builtins_py2'):
5961
# Use a custom source file for the std module.
60-
mpath = os.path.join(os.path.dirname(path), p[i].arg)
62+
arg = p[i].arg
63+
assert arg is not None
64+
mpath = os.path.join(os.path.dirname(path), arg)
6165
if p[i].id == 'builtins':
6266
fnam = 'builtins.pyi'
6367
else:
@@ -66,15 +70,17 @@ def parse_test_cases(
6670
with open(mpath) as f:
6771
files.append((os.path.join(base_path, fnam), f.read()))
6872
elif p[i].id == 'stale':
69-
if p[i].arg is None:
73+
arg = p[i].arg
74+
if arg is None:
7075
stale_modules = set()
7176
else:
72-
stale_modules = {item.strip() for item in p[i].arg.split(',')}
77+
stale_modules = {item.strip() for item in arg.split(',')}
7378
elif p[i].id == 'rechecked':
74-
if p[i].arg is None:
79+
arg = p[i].arg
80+
if arg is None:
7581
rechecked_modules = set()
7682
else:
77-
rechecked_modules = {item.strip() for item in p[i].arg.split(',')}
83+
rechecked_modules = {item.strip() for item in arg.split(',')}
7884
elif p[i].id == 'out' or p[i].id == 'out1':
7985
tcout = p[i].data
8086
if native_sep and os.path.sep == '\\':
@@ -95,7 +101,9 @@ def parse_test_cases(
95101
# If the set of rechecked modules isn't specified, make it the same as the set of
96102
# modules with a stale public interface.
97103
rechecked_modules = stale_modules
98-
if stale_modules is not None and not stale_modules.issubset(rechecked_modules):
104+
if (stale_modules is not None
105+
and rechecked_modules is not None
106+
and not stale_modules.issubset(rechecked_modules)):
99107
raise ValueError(
100108
'Stale modules must be a subset of rechecked modules ({})'.format(path))
101109

@@ -225,15 +233,15 @@ class TestItem:
225233
"""
226234

227235
id = ''
228-
arg = ''
236+
arg = '' # type: Optional[str]
229237

230238
# Text data, array of 8-bit strings
231239
data = None # type: List[str]
232240

233241
file = ''
234242
line = 0 # Line number in file
235243

236-
def __init__(self, id: str, arg: str, data: List[str], file: str,
244+
def __init__(self, id: str, arg: Optional[str], data: List[str], file: str,
237245
line: int) -> None:
238246
self.id = id
239247
self.arg = arg
@@ -248,8 +256,8 @@ def parse_test_data(l: List[str], fnam: str) -> List[TestItem]:
248256
ret = [] # type: List[TestItem]
249257
data = [] # type: List[str]
250258

251-
id = None # type: str
252-
arg = None # type: str
259+
id = None # type: Optional[str]
260+
arg = None # type: Optional[str]
253261

254262
i = 0
255263
i0 = 0

mypy/test/testcheck.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import typed_ast
99
import typed_ast.ast35
1010

11-
from typing import Tuple, List, Dict, Set
11+
from typing import Dict, List, Optional, Set, Tuple
1212

1313
from mypy import build, defaults
1414
from mypy.main import parse_version, process_options
@@ -149,15 +149,15 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental=0) -> None:
149149
sources = []
150150
for module_name, program_path, program_text in module_data:
151151
# Always set to none so we're forced to reread the module in incremental mode
152-
program_text = None if incremental else program_text
153-
sources.append(BuildSource(program_path, module_name, program_text))
152+
sources.append(BuildSource(program_path, module_name,
153+
None if incremental else program_text))
154+
res = None
154155
try:
155156
res = build.build(sources=sources,
156157
options=options,
157158
alt_lib_path=test_temp_dir)
158159
a = res.errors
159160
except CompileError as e:
160-
res = None
161161
a = e.messages
162162
a = normalize_error_messages(a)
163163

@@ -191,7 +191,8 @@ def run_case_once(self, testcase: DataDrivenTestCase, incremental=0) -> None:
191191
testcase.expected_stale_modules,
192192
res.manager.stale_modules)
193193

194-
def check_module_equivalence(self, name: str, expected: Set[str], actual: Set[str]) -> None:
194+
def check_module_equivalence(self, name: str,
195+
expected: Optional[Set[str]], actual: Set[str]) -> None:
195196
if expected is not None:
196197
assert_string_arrays_equal(
197198
list(sorted(expected)),

mypy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ class CallableType(FunctionLike):
560560
def __init__(self,
561561
arg_types: List[Type],
562562
arg_kinds: List[int],
563-
arg_names: List[str],
563+
arg_names: List[Optional[str]],
564564
ret_type: Type,
565565
fallback: Instance,
566566
name: str = None,

runtests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def add_mypy_modules(self, name: str, modules: Iterable[str],
8888
args = list(itertools.chain(*(['-m', mod] for mod in modules)))
8989
self.add_mypy_cmd(name, args, cwd=cwd)
9090

91-
def add_mypy_package(self, name: str, packagename: str) -> None:
92-
self.add_mypy_cmd(name, ['-p', packagename])
91+
def add_mypy_package(self, name: str, packagename: str, *flags: str) -> None:
92+
self.add_mypy_cmd(name, ['-p', packagename] + list(flags))
9393

9494
def add_mypy_string(self, name: str, *args: str, cwd: Optional[str] = None) -> None:
9595
self.add_mypy_cmd(name, ['-c'] + list(args), cwd=cwd)
@@ -168,6 +168,7 @@ def add_basic(driver: Driver) -> None:
168168

169169
def add_selftypecheck(driver: Driver) -> None:
170170
driver.add_mypy_package('package mypy', 'mypy')
171+
driver.add_mypy_package('package mypy', 'mypy', '--strict-optional')
171172

172173

173174
def find_files(base: str, prefix: str = '', suffix: str = '') -> List[str]:

0 commit comments

Comments
 (0)
0