Skip to content

Commit

Permalink
mypy/test: run pyupgrade (#12710)
Browse files Browse the repository at this point in the history
Re-attempt of #10741
Ran: `pyupgrade --py36-plus $(fd -e py) --keep-runtime-typing`
I mostly only needed to change things where NamedTuple comments got
dropped.

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja authored May 1, 2022
1 parent a56ebec commit 1662fe8
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 83 deletions.
27 changes: 16 additions & 11 deletions mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

root_dir = os.path.normpath(PREFIX)


# File modify/create operation: copy module contents from source_path.
UpdateFile = NamedTuple('UpdateFile', [('module', str),
('content', str),
('target_path', str)])
class UpdateFile(NamedTuple):
module: str
content: str
target_path: str


# File delete operation: delete module file.
DeleteFile = NamedTuple('DeleteFile', [('module', str),
('path', str)])
class DeleteFile(NamedTuple):
module: str
path: str


FileOperation = Union[UpdateFile, DeleteFile]

Expand Down Expand Up @@ -100,9 +105,9 @@ def parse_test_case(case: 'DataDrivenTestCase') -> None:
# File/directory to delete during a multi-step test case
assert item.arg is not None
m = re.match(r'(.*)\.([0-9]+)$', item.arg)
assert m, 'Invalid delete section: {}'.format(item.arg)
assert m, f'Invalid delete section: {item.arg}'
num = int(m.group(2))
assert num >= 2, "Can't delete during step {}".format(num)
assert num >= 2, f"Can't delete during step {num}"
full = join(base_path, m.group(1))
deleted_paths.setdefault(num, set()).add(full)
elif re.match(r'out[0-9]*$', item.id):
Expand Down Expand Up @@ -271,7 +276,7 @@ def runtest(self) -> None:
if save_dir:
assert self.tmpdir is not None
target_dir = os.path.join(save_dir, os.path.basename(self.tmpdir.name))
print("Copying data from test {} to {}".format(self.name, target_dir))
print(f"Copying data from test {self.name} to {target_dir}")
if not os.path.isabs(target_dir):
assert self.old_cwd
target_dir = os.path.join(self.old_cwd, target_dir)
Expand Down Expand Up @@ -339,7 +344,7 @@ def repr_failure(self, excinfo: Any, style: Optional[Any] = None) -> str:
self.parent._prunetraceback(excinfo)
excrepr = excinfo.getrepr(style='short')

return "data: {}:{}:\n{}".format(self.file, self.line, excrepr)
return f"data: {self.file}:{self.line}:\n{excrepr}"

def find_steps(self) -> List[List[FileOperation]]:
"""Return a list of descriptions of file operations for each incremental step.
Expand Down Expand Up @@ -493,7 +498,7 @@ def expand_errors(input: List[str], output: List[str], fnam: str) -> None:
message = message.replace('\\#', '#') # adds back escaped # character
if col is None:
output.append(
'{}:{}: {}: {}'.format(fnam, i + 1, severity, message))
f'{fnam}:{i + 1}: {severity}: {message}')
else:
output.append('{}:{}:{}: {}: {}'.format(
fnam, i + 1, col, severity, message))
Expand Down Expand Up @@ -625,7 +630,7 @@ def collect(self) -> Iterator['DataFileCollector']:
suite: DataSuite = self.obj

assert os.path.isdir(suite.data_prefix), \
'Test data prefix ({}) not set correctly'.format(suite.data_prefix)
f'Test data prefix ({suite.data_prefix}) not set correctly'

for data_file in suite.files:
yield DataFileCollector.from_parent(parent=self, name=data_file)
Expand Down
12 changes: 6 additions & 6 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def assert_string_arrays_equal(expected: List[str], actual: List[str],
if i >= len(actual) or expected[i] != actual[i]:
if first_diff < 0:
first_diff = i
sys.stderr.write(' {:<45} (diff)'.format(expected[i]))
sys.stderr.write(f' {expected[i]:<45} (diff)')
else:
e = expected[i]
sys.stderr.write(' ' + e[:width])
Expand All @@ -96,7 +96,7 @@ def assert_string_arrays_equal(expected: List[str], actual: List[str],

for j in range(num_skip_start, len(actual) - num_skip_end):
if j >= len(expected) or expected[j] != actual[j]:
sys.stderr.write(' {:<45} (diff)'.format(actual[j]))
sys.stderr.write(f' {actual[j]:<45} (diff)')
else:
a = actual[j]
sys.stderr.write(' ' + a[:width])
Expand Down Expand Up @@ -217,8 +217,8 @@ def show_align_message(s1: str, s2: str) -> None:
extra = '...'

# Write a chunk of both lines, aligned.
sys.stderr.write(' E: {}{}\n'.format(s1[:maxw], extra))
sys.stderr.write(' A: {}{}\n'.format(s2[:maxw], extra))
sys.stderr.write(f' E: {s1[:maxw]}{extra}\n')
sys.stderr.write(f' A: {s2[:maxw]}{extra}\n')
# Write an indicator character under the different columns.
sys.stderr.write(' ')
for j in range(min(maxw, max(len(s1), len(s2)))):
Expand Down Expand Up @@ -370,7 +370,7 @@ def parse_options(program_text: str, testcase: DataDrivenTestCase,
options = Options()
flags = re.search('# flags: (.*)$', program_text, flags=re.MULTILINE)
if incremental_step > 1:
flags2 = re.search('# flags{}: (.*)$'.format(incremental_step), program_text,
flags2 = re.search(f'# flags{incremental_step}: (.*)$', program_text,
flags=re.MULTILINE)
if flags2:
flags = flags2
Expand Down Expand Up @@ -457,7 +457,7 @@ def check_test_output_files(testcase: DataDrivenTestCase,
raise AssertionError(
'Expected file {} was not produced by test case{}'.format(
path, ' on step %d' % step if testcase.output2 else ''))
with open(path, 'r', encoding='utf8') as output_file:
with open(path, encoding='utf8') as output_file:
actual_output_content = output_file.read()

if isinstance(expected_content, Pattern):
Expand Down
12 changes: 6 additions & 6 deletions mypy/test/test_find_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def isdir(self, dir: str) -> bool:
def listdir(self, dir: str) -> List[str]:
if not dir.endswith(os.sep):
dir += os.sep
return list(set(f[len(dir):].split(os.sep)[0] for f in self.files if f.startswith(dir)))
return list({f[len(dir):].split(os.sep)[0] for f in self.files if f.startswith(dir)})

def init_under_package_root(self, file: str) -> bool:
return False
Expand Down Expand Up @@ -278,15 +278,15 @@ def test_find_sources_exclude(self) -> None:

# default
for excluded_dir in ["site-packages", ".whatever", "node_modules", ".x/.z"]:
fscache = FakeFSCache({"/dir/a.py", "/dir/venv/{}/b.py".format(excluded_dir)})
fscache = FakeFSCache({"/dir/a.py", f"/dir/venv/{excluded_dir}/b.py"})
assert find_sources(["/"], options, fscache) == [("a", "/dir")]
with pytest.raises(InvalidSourceList):
find_sources(["/dir/venv/"], options, fscache)
assert find_sources(["/dir/venv/{}".format(excluded_dir)], options, fscache) == [
("b", "/dir/venv/{}".format(excluded_dir))
assert find_sources([f"/dir/venv/{excluded_dir}"], options, fscache) == [
("b", f"/dir/venv/{excluded_dir}")
]
assert find_sources(["/dir/venv/{}/b.py".format(excluded_dir)], options, fscache) == [
("b", "/dir/venv/{}".format(excluded_dir))
assert find_sources([f"/dir/venv/{excluded_dir}/b.py"], options, fscache) == [
("b", f"/dir/venv/{excluded_dir}")
]

files = {
Expand Down
10 changes: 5 additions & 5 deletions mypy/test/testargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ def test_executable_inference(self) -> None:
base = ['file.py'] # dummy file

# test inference given one (infer the other)
matching_version = base + ['--python-version={}'.format(sys_ver_str)]
matching_version = base + [f'--python-version={sys_ver_str}']
_, options = process_options(matching_version)
assert options.python_version == sys.version_info[:2]
assert options.python_executable == sys.executable

matching_version = base + ['--python-executable={}'.format(sys.executable)]
matching_version = base + [f'--python-executable={sys.executable}']
_, options = process_options(matching_version)
assert options.python_version == sys.version_info[:2]
assert options.python_executable == sys.executable

# test inference given both
matching_version = base + ['--python-version={}'.format(sys_ver_str),
'--python-executable={}'.format(sys.executable)]
matching_version = base + [f'--python-version={sys_ver_str}',
f'--python-executable={sys.executable}']
_, options = process_options(matching_version)
assert options.python_version == sys.version_info[:2]
assert options.python_executable == sys.executable

# test that --no-site-packages will disable executable inference
matching_version = base + ['--python-version={}'.format(sys_ver_str),
matching_version = base + [f'--python-version={sys_ver_str}',
'--no-site-packages']
_, options = process_options(matching_version)
assert options.python_version == sys.version_info[:2]
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def parse_module(self,
"""
m = re.search('# cmd: mypy -m ([a-zA-Z0-9_. ]+)$', program_text, flags=re.MULTILINE)
if incremental_step > 1:
alt_regex = '# cmd{}: mypy -m ([a-zA-Z0-9_. ]+)$'.format(incremental_step)
alt_regex = f'# cmd{incremental_step}: mypy -m ([a-zA-Z0-9_. ]+)$'
alt_m = re.search(alt_regex, program_text, flags=re.MULTILINE)
if alt_m is not None:
# Optionally return a different command if in a later step
Expand Down
4 changes: 2 additions & 2 deletions mypy/test/testcmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
program_path = os.path.join(test_temp_dir, program)
with open(program_path, 'w', encoding='utf8') as file:
for s in testcase.input:
file.write('{}\n'.format(s))
file.write(f'{s}\n')
args = parse_args(testcase.input[0])
custom_cwd = parse_cwd(testcase.input[1]) if len(testcase.input) > 1 else None
args.append('--show-traceback')
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None:
out = normalize_error_messages(err + out)
obvious_result = 1 if out else 0
if obvious_result != result:
out.append('== Return code: {}'.format(result))
out.append(f'== Return code: {result}')
expected_out = testcase.output if step == 1 else testcase.output2[step]
# Strip "tmp/" out of the test so that # E: works...
expected_out = [s.replace("tmp" + os.sep, "") for s in expected_out]
Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
if source.startswith(('<enum', '<typing', '<mypy')):
# Remove noise.
continue
line = '%s -> %s' % (source, ', '.join(sorted(targets)))
line = '{} -> {}'.format(source, ', '.join(sorted(targets)))
# Clean up output a bit
line = line.replace('__main__', 'm')
a.append(line)
Expand Down
4 changes: 2 additions & 2 deletions mypy/test/testfinegrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def parse_sources(self, program_text: str,
"""
m = re.search('# cmd: mypy ([a-zA-Z0-9_./ ]+)$', program_text, flags=re.MULTILINE)
regex = '# cmd{}: mypy ([a-zA-Z0-9_./ ]+)$'.format(incremental_step)
regex = f'# cmd{incremental_step}: mypy ([a-zA-Z0-9_./ ]+)$'
alt_m = re.search(regex, program_text, flags=re.MULTILINE)
if alt_m is not None:
# Optionally return a different command if in a later step
Expand Down Expand Up @@ -328,7 +328,7 @@ def maybe_suggest(self, step: int, server: Server, src: str, tmp_dir: str) -> Li
def get_suggest(self, program_text: str,
incremental_step: int) -> List[Tuple[str, str]]:
step_bit = '1?' if incremental_step == 1 else str(incremental_step)
regex = '# suggest{}: (--[a-zA-Z0-9_\\-./=?^ ]+ )*([a-zA-Z0-9_.:/?^ ]+)$'.format(step_bit)
regex = f'# suggest{step_bit}: (--[a-zA-Z0-9_\\-./=?^ ]+ )*([a-zA-Z0-9_.:/?^ ]+)$'
m = re.findall(regex, program_text, flags=re.MULTILINE)
return m

Expand Down
2 changes: 1 addition & 1 deletion mypy/test/testgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_topsort(self) -> None:
def test_scc(self) -> None:
vertices = {"A", "B", "C", "D"}
edges: Dict[str, List[str]] = {"A": ["B", "C"], "B": ["C"], "C": ["B", "D"], "D": []}
sccs = set(frozenset(x) for x in strongly_connected_components(vertices, edges))
sccs = {frozenset(x) for x in strongly_connected_components(vertices, edges)}
assert_equal(sccs,
{frozenset({'A'}),
frozenset({'B', 'C'}),
Expand Down
10 changes: 5 additions & 5 deletions mypy/test/testmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ def dump_symbol_tables(self, modules: Dict[str, MypyFile]) -> List[str]:
return a

def dump_symbol_table(self, module_id: str, symtable: SymbolTable) -> List[str]:
a = ['{}:'.format(module_id)]
a = [f'{module_id}:']
for name in sorted(symtable):
if name.startswith('__'):
continue
a.append(' {}: {}'.format(name, self.format_symbol_table_node(symtable[name])))
a.append(f' {name}: {self.format_symbol_table_node(symtable[name])}')
return a

def format_symbol_table_node(self, node: SymbolTableNode) -> str:
Expand All @@ -180,11 +180,11 @@ def format_symbol_table_node(self, node: SymbolTableNode) -> str:
s = '{}<{}>'.format(str(type(node.node).__name__),
self.id_mapper.id(node.node))
else:
s = '? ({})'.format(type(node.node))
s = f'? ({type(node.node)})'
if (isinstance(node.node, Var) and node.node.type and
not node.node.fullname.startswith('typing.')):
typestr = self.format_type(node.node.type)
s += '({})'.format(typestr)
s += f'({typestr})'
return s

def dump_typeinfos(self, modules: Dict[str, MypyFile]) -> List[str]:
Expand Down Expand Up @@ -226,7 +226,7 @@ def dump_types(self, manager: FineGrainedBuildManager) -> List[str]:
for node in get_subexpressions(tree)
if node in all_types}
if type_map:
a.append('## {}'.format(module_id))
a.append(f'## {module_id}')
for expr in sorted(type_map, key=lambda n: (n.line, short_type(n),
str(n) + str(type_map[n]))):
typ = type_map[expr]
Expand Down
12 changes: 6 additions & 6 deletions mypy/test/testpep561.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def virtualenv(
proc = subprocess.run([sys.executable,
'-m',
'virtualenv',
'-p{}'.format(python_executable),
f'-p{python_executable}',
venv_dir], cwd=os.getcwd(), stdout=PIPE, stderr=PIPE)
if proc.returncode != 0:
err = proc.stdout.decode('utf-8') + proc.stderr.decode('utf-8')
Expand Down Expand Up @@ -118,12 +118,12 @@ def test_pep561(testcase: DataDrivenTestCase) -> None:
program = testcase.name + '.py'
with open(program, 'w', encoding='utf-8') as f:
for s in testcase.input:
f.write('{}\n'.format(s))
f.write(f'{s}\n')
cmd_line.append(program)

cmd_line.extend(['--no-error-summary'])
if python_executable != sys.executable:
cmd_line.append('--python-executable={}'.format(python_executable))
cmd_line.append(f'--python-executable={python_executable}')

steps = testcase.find_steps()
if steps != [[]]:
Expand All @@ -144,7 +144,7 @@ def test_pep561(testcase: DataDrivenTestCase) -> None:
# Normalize paths so that the output is the same on Windows and Linux/macOS.
line = line.replace(test_temp_dir + os.sep, test_temp_dir + '/')
output.append(line.rstrip("\r\n"))
iter_count = '' if i == 0 else ' on iteration {}'.format(i + 1)
iter_count = '' if i == 0 else f' on iteration {i + 1}'
expected = testcase.output if i == 0 else testcase.output2.get(i + 1, [])

assert_string_arrays_equal(expected, output,
Expand Down Expand Up @@ -189,14 +189,14 @@ def test_mypy_path_is_respected() -> None:
mypy_config_path = os.path.join(temp_dir, 'mypy.ini')
with open(mypy_config_path, 'w') as mypy_file:
mypy_file.write('[mypy]\n')
mypy_file.write('mypy_path = ./{}\n'.format(packages))
mypy_file.write(f'mypy_path = ./{packages}\n')

with virtualenv() as venv:
venv_dir, python_executable = venv

cmd_line_args = []
if python_executable != sys.executable:
cmd_line_args.append('--python-executable={}'.format(python_executable))
cmd_line_args.append(f'--python-executable={python_executable}')
cmd_line_args.extend(['--config-file', mypy_config_path,
'--package', pkg_name])

Expand Down
4 changes: 2 additions & 2 deletions mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def test_python_evaluation(testcase: DataDrivenTestCase, cache_dir: str) -> None
mypy_cmdline.append(program_path)
with open(program_path, 'w', encoding='utf8') as file:
for s in testcase.input:
file.write('{}\n'.format(s))
mypy_cmdline.append('--cache-dir={}'.format(cache_dir))
file.write(f'{s}\n')
mypy_cmdline.append(f'--cache-dir={cache_dir}')
output = []
# Type check the program.
out, err, returncode = api.run(mypy_cmdline)
Expand Down
8 changes: 4 additions & 4 deletions mypy/test/testsemanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_semanal_error(testcase: DataDrivenTestCase) -> None:
options=get_semanal_options(src, testcase),
alt_lib_path=test_temp_dir)
a = res.errors
assert a, 'No errors reported in {}, line {}'.format(testcase.file, testcase.line)
assert a, f'No errors reported in {testcase.file}, line {testcase.line}'
except CompileError as e:
# Verify that there was a compile error and that the error messages
# are equivalent.
Expand All @@ -135,7 +135,7 @@ def test_semanal_error(testcase: DataDrivenTestCase) -> None:
a = normalize_error_messages(a)
assert_string_arrays_equal(
testcase.output, a,
'Invalid compiler output ({}, line {})'.format(testcase.file, testcase.line))
f'Invalid compiler output ({testcase.file}, line {testcase.line})')


# SymbolNode table export test cases
Expand All @@ -158,7 +158,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
raise CompileError(a)
for f in sorted(result.files.keys()):
if f not in ('builtins', 'typing', 'abc'):
a.append('{}:'.format(f))
a.append(f'{f}:')
for s in str(result.files[f].names).split('\n'):
a.append(' ' + s)
except CompileError as e:
Expand Down Expand Up @@ -212,6 +212,6 @@ def __str__(self) -> str:
not x.startswith('typing.') and
not x.startswith('abc.')):
ti = ('\n' + ' ').join(str(y).split('\n'))
a.append(' {} : {}'.format(x, ti))
a.append(f' {x} : {ti}')
a[-1] += ')'
return '\n'.join(a)
2 changes: 1 addition & 1 deletion mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def add_file(self, path: str, result: List[str], header: bool) -> None:
result.append('<%s was not generated>' % path.replace('\\', '/'))
return
if header:
result.append('# {}'.format(path[4:]))
result.append(f'# {path[4:]}')
with open(path, encoding='utf8') as file:
result.extend(file.read().splitlines())

Expand Down
Loading

0 comments on commit 1662fe8

Please sign in to comment.