Skip to content

Commit e1db303

Browse files
author
Guido van Rossum
committed
Fix strict none errors in the test subpackage.
This shows several different approaches to dealing with #2199 for p[i].arg.
1 parent a9fac0b commit e1db303

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

mypy/build.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> Optional[Cache
770770
return m
771771

772772

773-
def is_meta_fresh(meta: CacheMeta, id: str, path: str, manager: BuildManager) -> bool:
773+
def is_meta_fresh(meta: Optional[CacheMeta], id: str, path: str, manager: BuildManager) -> bool:
774774
if meta is None:
775775
return False
776776

mypy/test/data.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ 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+
files.append((os.path.join(base_path, p[i].arg or ''),
5757
'\n'.join(p[i].data)))
5858
elif p[i].id in ('builtins', 'builtins_py2'):
5959
# Use a custom source file for the std module.
60-
mpath = os.path.join(os.path.dirname(path), p[i].arg)
60+
mpath = os.path.join(os.path.dirname(path), p[i].arg or '')
6161
if p[i].id == 'builtins':
6262
fnam = 'builtins.pyi'
6363
else:
@@ -66,15 +66,17 @@ def parse_test_cases(
6666
with open(mpath) as f:
6767
files.append((os.path.join(base_path, fnam), f.read()))
6868
elif p[i].id == 'stale':
69-
if p[i].arg is None:
69+
arg = p[i].arg
70+
if arg is None:
7071
stale_modules = set()
7172
else:
72-
stale_modules = {item.strip() for item in p[i].arg.split(',')}
73+
stale_modules = {item.strip() for item in arg.split(',')}
7374
elif p[i].id == 'rechecked':
74-
if p[i].arg is None:
75+
arg = p[i].arg
76+
if arg is None:
7577
rechecked_modules = set()
7678
else:
77-
rechecked_modules = {item.strip() for item in p[i].arg.split(',')}
79+
rechecked_modules = {item.strip() for item in arg.split(',')}
7880
elif p[i].id == 'out' or p[i].id == 'out1':
7981
tcout = p[i].data
8082
if native_sep and os.path.sep == '\\':
@@ -95,7 +97,9 @@ def parse_test_cases(
9597
# If the set of rechecked modules isn't specified, make it the same as the set of
9698
# modules with a stale public interface.
9799
rechecked_modules = stale_modules
98-
if stale_modules is not None and not stale_modules.issubset(rechecked_modules):
100+
if (stale_modules is not None
101+
and rechecked_modules is not None
102+
and not stale_modules.issubset(rechecked_modules)):
99103
raise ValueError(
100104
'Stale modules must be a subset of rechecked modules ({})'.format(path))
101105

@@ -225,15 +229,15 @@ class TestItem:
225229
"""
226230

227231
id = ''
228-
arg = ''
232+
arg = '' # type: Optional[str]
229233

230234
# Text data, array of 8-bit strings
231235
data = None # type: List[str]
232236

233237
file = ''
234238
line = 0 # Line number in file
235239

236-
def __init__(self, id: str, arg: str, data: List[str], file: str,
240+
def __init__(self, id: str, arg: Optional[str], data: List[str], file: str,
237241
line: int) -> None:
238242
self.id = id
239243
self.arg = arg
@@ -248,8 +252,8 @@ def parse_test_data(l: List[str], fnam: str) -> List[TestItem]:
248252
ret = [] # type: List[TestItem]
249253
data = [] # type: List[str]
250254

251-
id = None # type: str
252-
arg = None # type: str
255+
id = None # type: Optional[str]
256+
arg = None # type: Optional[str]
253257

254258
i = 0
255259
i0 = 0

mypy/test/testcheck.py

+6-5
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

+1-1
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,

0 commit comments

Comments
 (0)