Skip to content

Commit 82d21cd

Browse files
committed
Reformat according to format-incremental
1 parent fd6456e commit 82d21cd

53 files changed

Lines changed: 286 additions & 290 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dev_tools/env_tools.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ def get_unhidden_ungenerated_python_files(directory: str) -> Iterable[str]:
4040
yield os.path.join(dirpath, filename)
4141

4242

43-
def create_virtual_env(venv_path: str, requirements_paths: Iterable[str],
44-
python_path: str, verbose: bool) -> None:
43+
def create_virtual_env(
44+
venv_path: str, requirements_paths: Iterable[str], python_path: str, verbose: bool
45+
) -> None:
4546
"""Creates a new virtual environment and then installs dependencies.
4647
4748
Args:
@@ -50,30 +51,24 @@ def create_virtual_env(venv_path: str, requirements_paths: Iterable[str],
5051
python_path: The python binary to use.
5152
verbose: When set, more progress output is produced.
5253
"""
53-
shell_tools.run_cmd('virtualenv',
54-
None if verbose else '--quiet',
55-
'-p',
56-
python_path,
57-
venv_path,
58-
out=sys.stderr)
54+
shell_tools.run_cmd(
55+
'virtualenv', None if verbose else '--quiet', '-p', python_path, venv_path, out=sys.stderr
56+
)
5957
pip_path = os.path.join(venv_path, 'bin', 'pip')
6058
for req_path in requirements_paths:
61-
shell_tools.run_cmd(pip_path,
62-
'install',
63-
None if verbose else '--quiet',
64-
'-r',
65-
req_path,
66-
out=sys.stderr)
59+
shell_tools.run_cmd(
60+
pip_path, 'install', None if verbose else '--quiet', '-r', req_path, out=sys.stderr
61+
)
6762

6863

6964
def prepare_temporary_test_environment(
70-
destination_directory: str,
71-
repository: GithubRepository,
72-
pull_request_number: Optional[int],
73-
verbose: bool,
74-
env_name: str = '.test_virtualenv',
75-
python_path: str = sys.executable,
76-
commit_ids_known_callback: Callable[[PreparedEnv], None] = None
65+
destination_directory: str,
66+
repository: GithubRepository,
67+
pull_request_number: Optional[int],
68+
verbose: bool,
69+
env_name: str = '.test_virtualenv',
70+
python_path: str = sys.executable,
71+
commit_ids_known_callback: Callable[[PreparedEnv], None] = None,
7772
) -> PreparedEnv:
7873
"""Prepares a temporary test environment at the (existing empty) directory.
7974
@@ -101,10 +96,12 @@ def prepare_temporary_test_environment(
10196
destination_directory=destination_directory,
10297
repository=repository,
10398
pull_request_number=pull_request_number,
104-
verbose=verbose)
99+
verbose=verbose,
100+
)
105101
else:
106102
env = git_env_tools.fetch_local_files(
107-
destination_directory=destination_directory, verbose=verbose)
103+
destination_directory=destination_directory, verbose=verbose
104+
)
108105

109106
if commit_ids_known_callback is not None:
110107
commit_ids_known_callback(env)
@@ -113,18 +110,19 @@ def prepare_temporary_test_environment(
113110
base_path = cast(str, env.destination_directory)
114111
env_path = os.path.join(base_path, env_name)
115112
req_path = os.path.join(base_path, 'tutorial-requirements.txt')
116-
dev_req_path = os.path.join(base_path, 'dev_tools', 'conf',
117-
'pip-list-dev-tools.txt')
118-
contrib_req_path = os.path.join(base_path, 'cirq', 'contrib',
119-
'contrib-tutorial-requirements.txt')
113+
dev_req_path = os.path.join(base_path, 'dev_tools', 'conf', 'pip-list-dev-tools.txt')
114+
contrib_req_path = os.path.join(
115+
base_path, 'cirq', 'contrib', 'contrib-tutorial-requirements.txt'
116+
)
120117
rev_paths = [req_path, dev_req_path, contrib_req_path]
121-
create_virtual_env(venv_path=env_path,
122-
python_path=python_path,
123-
requirements_paths=rev_paths,
124-
verbose=verbose)
125-
126-
return PreparedEnv(github_repo=env.repository,
127-
actual_commit_id=env.actual_commit_id,
128-
compare_commit_id=env.compare_commit_id,
129-
destination_directory=env.destination_directory,
130-
virtual_env_path=env_path)
118+
create_virtual_env(
119+
venv_path=env_path, python_path=python_path, requirements_paths=rev_paths, verbose=verbose
120+
)
121+
122+
return PreparedEnv(
123+
github_repo=env.repository,
124+
actual_commit_id=env.actual_commit_id,
125+
compare_commit_id=env.compare_commit_id,
126+
destination_directory=env.destination_directory,
127+
virtual_env_path=env_path,
128+
)

dev_tools/github_repository.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
class GithubRepository:
1919
"""Details how to access a repository on github."""
2020

21-
def __init__(self, organization: str, name: str,
22-
access_token: Optional[str]) -> None:
21+
def __init__(self, organization: str, name: str, access_token: Optional[str]) -> None:
2322
"""
2423
Args:
2524
organization: The github organization the repository is under.

dev_tools/incremental_coverage.py

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050
EXPLICIT_OPT_OUT_COMMENT = '#coverage:ignore'
5151

5252

53-
def diff_to_new_interesting_lines(unified_diff_lines: List[str]
54-
) -> Dict[int, str]:
53+
def diff_to_new_interesting_lines(unified_diff_lines: List[str]) -> Dict[int, str]:
5554
"""
5655
Extracts a set of 'interesting' lines out of a GNU unified diff format.
5756
@@ -95,7 +94,7 @@ def diff_to_new_interesting_lines(unified_diff_lines: List[str]
9594
# Parse the 'new file' range parts of the unified diff.
9695
if not diff_line.startswith('@@ '):
9796
continue
98-
change = diff_line[3:diff_line.index(' @@', 3)]
97+
change = diff_line[3 : diff_line.index(' @@', 3)]
9998
new = change.split(' ')[1]
10099
start = int(new.split(',')[0])
101100
count = 1 if ',' not in new else int(new.split(',')[1])
@@ -117,9 +116,9 @@ def fix_line_from_coverage_file(line):
117116
return line
118117

119118

120-
def get_incremental_uncovered_lines(abs_path: str, base_commit: str,
121-
actual_commit: Optional[str]
122-
) -> List[Tuple[int, str, str]]:
119+
def get_incremental_uncovered_lines(
120+
abs_path: str, base_commit: str, actual_commit: Optional[str]
121+
) -> List[Tuple[int, str, str]]:
123122
"""
124123
Uses git diff and the annotation files created by
125124
`pytest --cov-report annotate` to find touched but uncovered lines in the
@@ -139,12 +138,10 @@ def get_incremental_uncovered_lines(abs_path: str, base_commit: str,
139138
if not os.path.isfile(abs_path):
140139
return []
141140

142-
unified_diff_lines_str = shell_tools.output_of('git', 'diff', '--unified=0',
143-
base_commit, actual_commit,
144-
'--', abs_path)
145-
unified_diff_lines = [
146-
e for e in unified_diff_lines_str.split('\n') if e.strip()
147-
]
141+
unified_diff_lines_str = shell_tools.output_of(
142+
'git', 'diff', '--unified=0', base_commit, actual_commit, '--', abs_path
143+
)
144+
unified_diff_lines = [e for e in unified_diff_lines_str.split('\n') if e.strip()]
148145

149146
touched_lines = diff_to_new_interesting_lines(unified_diff_lines)
150147

@@ -155,10 +152,12 @@ def get_incremental_uncovered_lines(abs_path: str, base_commit: str,
155152
has_cover_file = os.path.isfile(cover_path)
156153
content_file = cover_path if has_cover_file else abs_path
157154
with open(content_file, 'r') as annotated_coverage_file:
158-
return [(i, fix_line_from_coverage_file(line), touched_lines[i])
159-
for i, line in enumerate(annotated_coverage_file, start=1)
160-
if i in touched_lines and i not in ignored_lines
161-
if line_counts_as_uncovered(line, has_cover_file)]
155+
return [
156+
(i, fix_line_from_coverage_file(line), touched_lines[i])
157+
for i, line in enumerate(annotated_coverage_file, start=1)
158+
if i in touched_lines and i not in ignored_lines
159+
if line_counts_as_uncovered(line, has_cover_file)
160+
]
162161

163162

164163
def line_content_counts_as_uncovered_manual(content: str) -> bool:
@@ -211,15 +210,13 @@ def determine_ignored_lines(content: str) -> Set[int]:
211210
def naive_find_end_of_scope(lines: List[str], i: int) -> int:
212211
# TODO: deal with line continuations, which may be less indented.
213212
line = lines[i]
214-
indent = line[:len(line) - len(line.lstrip())]
215-
while i < len(lines) and (not lines[i].strip() or
216-
lines[i].startswith(indent)):
213+
indent = line[: len(line) - len(line.lstrip())]
214+
while i < len(lines) and (not lines[i].strip() or lines[i].startswith(indent)):
217215
i += 1
218216
return i
219217

220218

221-
def line_counts_as_uncovered(line: str,
222-
is_from_cover_annotation_file: bool) -> bool:
219+
def line_counts_as_uncovered(line: str, is_from_cover_annotation_file: bool) -> bool:
223220
"""
224221
Args:
225222
line: The line of code (including coverage annotation).
@@ -244,14 +241,13 @@ def line_counts_as_uncovered(line: str,
244241
# Ignore end-of-line comments.
245242
# TODO: avoid # in strings, etc.
246243
if '#' in content:
247-
content = content[:content.index('#')].strip()
244+
content = content[: content.index('#')].strip()
248245

249246
# Ignored line pattern?
250247
if any(re.search(pat, content) for pat in IGNORED_LINE_PATTERNS):
251248
return False
252249

253-
return (is_from_cover_annotation_file or
254-
line_content_counts_as_uncovered_manual(content))
250+
return is_from_cover_annotation_file or line_content_counts_as_uncovered_manual(content)
255251

256252

257253
def is_applicable_python_file(rel_path: str) -> bool:
@@ -263,8 +259,9 @@ def is_applicable_python_file(rel_path: str) -> bool:
263259
Returns:
264260
Whether to include the file.
265261
"""
266-
return (rel_path.endswith('.py') and
267-
not any(re.search(pat, rel_path) for pat in IGNORED_FILE_PATTERNS))
262+
return rel_path.endswith('.py') and not any(
263+
re.search(pat, rel_path) for pat in IGNORED_FILE_PATTERNS
264+
)
268265

269266

270267
def check_for_uncovered_lines(env: env_tools.PreparedEnv) -> int:
@@ -279,31 +276,36 @@ def check_for_uncovered_lines(env: env_tools.PreparedEnv) -> int:
279276

280277
base_path = cast(str, env.destination_directory)
281278
uncovered_lines = get_incremental_uncovered_lines(
282-
os.path.join(base_path, changed_file), env.compare_commit_id,
283-
env.actual_commit_id)
279+
os.path.join(base_path, changed_file), env.compare_commit_id, env.actual_commit_id
280+
)
284281

285282
if uncovered_lines:
286283
uncovered_count += len(uncovered_lines)
287284
print(
288-
shell_tools.highlight('************* {} ({} uncovered)'.format(
289-
changed_file, len(uncovered_lines)),
290-
color_code=shell_tools.RED))
285+
shell_tools.highlight(
286+
'************* {} ({} uncovered)'.format(changed_file, len(uncovered_lines)),
287+
color_code=shell_tools.RED,
288+
)
289+
)
291290
for index, line, reason in uncovered_lines:
292-
print('Line {} {} but not covered: {}'.format(
293-
shell_tools.highlight(str(index).rjust(4),
294-
color_code=shell_tools.BOLD), reason,
295-
shell_tools.highlight(line, color_code=shell_tools.YELLOW)))
291+
print(
292+
'Line {} {} but not covered: {}'.format(
293+
shell_tools.highlight(str(index).rjust(4), color_code=shell_tools.BOLD),
294+
reason,
295+
shell_tools.highlight(line, color_code=shell_tools.YELLOW),
296+
)
297+
)
296298

297299
# Inform of aggregate result.
298300
print()
299301
if uncovered_count:
300302
print(
301303
shell_tools.highlight(
302304
'Found {} uncovered touched lines.'.format(uncovered_count),
303-
color_code=shell_tools.RED))
305+
color_code=shell_tools.RED,
306+
)
307+
)
304308
else:
305-
print(
306-
shell_tools.highlight('All touched lines covered',
307-
color_code=shell_tools.GREEN))
309+
print(shell_tools.highlight('All touched lines covered', color_code=shell_tools.GREEN))
308310
print()
309311
return uncovered_count

0 commit comments

Comments
 (0)