Skip to content

Commit

Permalink
chore: upgrade black in noxfile.py to 22.3.0 (#1377)
Browse files Browse the repository at this point in the history
* chore: upgrade black to 22.3.0

* update lint also

* run blacken session

* ci: use click>8.0 for blacken/lint sessions

* chore: fix typo

Co-authored-by: nicain <nicholascain@google.com>

* rebase error

Co-authored-by: nicain <nicholascain@google.com>
  • Loading branch information
parthea and nicain authored Apr 1, 2022
1 parent 65e3263 commit 0dcf739
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 43 deletions.
7 changes: 4 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ def generate_protos(session):
session.run(
"python", "-m", "grpc_tools.protoc", "-Isynthtool/protos", "--python_out=synthtool/protos", "synthtool/protos/metadata.proto", "synthtool/protos/preconfig.proto")

@nox.session(python=['3.6', '3.8'])
@nox.session(python=['3.6', '3.9'])
def blacken(session):
session.install('black==19.10b0')
session.install('black==22.3.0', 'click>8.0')
session.run('black', 'synthtool', 'tests')


@nox.session(python=['3.6', '3.8', '3.9'])
def lint(session):
session.install('mypy==0.790', 'flake8', 'black==19.10b0')
session.install('mypy==0.790', 'flake8', 'black==22.3.0')
session.run('pip', 'install', '-e', '.')
session.run('pip', 'install', 'click>8.0')
session.run('black', '--check', 'synthtool', 'tests')
session.run('flake8', 'synthtool', 'tests')
session.run('mypy', 'synthtool')
Expand Down
2 changes: 1 addition & 1 deletion synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def detect_versions(


def decamelize(value: str):
""" parser to convert fooBar.js to Foo Bar. """
"""Parser to convert fooBar.js to Foo Bar."""
if not value:
return ""
str_decamelize = re.sub("^.", value[0].upper(), value) # apple -> Apple.
Expand Down
2 changes: 1 addition & 1 deletion synthtool/gcp/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def all_samples(sample_globs: List[str]) -> List[Dict[str, str]]:


def _decamelize(value: str):
""" parser to convert fooBar.js to Foo Bar. """
"""Parser to convert fooBar.js to Foo Bar."""
if not value:
return ""
str_decamelize = re.sub("^.", value[0].upper(), value) # apple -> Apple.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def str2bool(v):
args = parser.parse_args()

notebooks = execute_changed_notebooks_helper.get_changed_notebooks(
test_paths_file=args.test_paths_file, base_branch=args.base_branch,
test_paths_file=args.test_paths_file,
base_branch=args.base_branch,
)

execute_changed_notebooks_helper.process_and_execute_notebooks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class NotebookExecutionResult:


def _process_notebook(
notebook_path: str, variable_project_id: str, variable_region: str,
notebook_path: str,
variable_project_id: str,
variable_region: str,
):
# Read notebook
with open(notebook_path) as f:
Expand All @@ -77,7 +79,10 @@ def _process_notebook(
)

# Use no-execute preprocessor
(nb, resources,) = remove_no_execute_cells_preprocessor.preprocess(nb)
(
nb,
resources,
) = remove_no_execute_cells_preprocessor.preprocess(nb)

(nb, resources) = update_variables_preprocessor.preprocess(nb, resources)

Expand Down Expand Up @@ -183,7 +188,8 @@ def process_and_execute_notebook(


def get_changed_notebooks(
test_paths_file: str, base_branch: Optional[str] = None,
test_paths_file: str,
base_branch: Optional[str] = None,
) -> List[str]:
"""
Get the notebooks that exist under the folders defined in the test_paths_file.
Expand Down Expand Up @@ -293,7 +299,9 @@ def process_and_execute_notebooks(
print("\n=== RESULTS ===\n")

results_sorted = sorted(
notebook_execution_results, key=lambda result: result.is_pass, reverse=True,
notebook_execution_results,
key=lambda result: result.is_pass,
reverse=True,
)

# Print results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@


def execute_notebook(
notebook_source: str, output_file_or_uri: str, should_log_output: bool,
notebook_source: str,
output_file_or_uri: str,
should_log_output: bool,
):
"""Execute a single notebook using Papermill"""
file_name = os.path.basename(os.path.normpath(notebook_source))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def preprocess(self, notebook, resources=None):
for cell in notebook.cells:
if cell.cell_type == "code":
cell.source = self.update_variables(
content=cell.source, replacement_map=self._replacement_map,
content=cell.source,
replacement_map=self._replacement_map,
)

executable_cells.append(cell)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def download_file(bucket_name: str, blob_name: str, destination_file: str) -> st
return destination_file


def upload_file(local_file_path: str, remote_file_path: str,) -> str:
def upload_file(
local_file_path: str,
remote_file_path: str,
) -> str:
"""Copies a local file to a GCS path"""
subprocess.check_output(
["gsutil", "cp", local_file_path, remote_file_path], encoding="UTF-8"
Expand Down
3 changes: 2 additions & 1 deletion synthtool/languages/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ def _common_template_metadata() -> Dict[str, Any]:
)

metadata["latest_bom_version"] = latest_maven_version(
group_id="com.google.cloud", artifact_id="libraries-bom",
group_id="com.google.cloud",
artifact_id="libraries-bom",
)

metadata["samples"] = samples.all_samples(["samples/**/src/main/java/**/*.java"])
Expand Down
17 changes: 7 additions & 10 deletions synthtool/languages/php.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def _merge(src: str, dest: str, path: Path):


def _find_copy_target(src: Path, version_string: str) -> typing.Optional[Path]:
"""Returns a directory contains the version subdirectory.
"""
"""Returns a directory contains the version subdirectory."""
logger.debug("_find_copy_target called with %s and %s", src, version_string)
entries = os.scandir(src)
if not entries:
Expand All @@ -81,10 +80,11 @@ def _find_copy_target(src: Path, version_string: str) -> typing.Optional[Path]:


def owlbot_copy_version(
src: Path, dest: Path, copy_excludes: typing.Optional[typing.List[str]] = None,
src: Path,
dest: Path,
copy_excludes: typing.Optional[typing.List[str]] = None,
) -> None:
"""Copies files from a version subdirectory.
"""
"""Copies files from a version subdirectory."""
logger.debug("owlbot_copy_version called from %s to %s", src, dest)

if copy_excludes is None:
Expand Down Expand Up @@ -146,8 +146,7 @@ def owlbot_main(
copy_excludes: typing.Optional[typing.List[str]] = None,
patch_func: typing.Callable[[], None] = owlbot_patch,
) -> None:
"""Copies files from generated tree.
"""
"""Copies files from generated tree."""
entries = os.scandir(src)
if not entries:
logger.info("there is no version subdirectory to copy")
Expand All @@ -161,9 +160,7 @@ def owlbot_main(


def owlbot_entrypoint(staging_dir: str = STAGING_DIR) -> None:
"""Copies files from staging and template directories into current working dir.
"""
"""Copies files from staging and template directories into current working dir."""
logging.basicConfig(level=logging.INFO)

logger.debug("owlbot_main called")
Expand Down
18 changes: 9 additions & 9 deletions synthtool/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def fix_pb2_headers(*, proto_root: str = "**/*_pb2.py") -> None:
s.replace(
proto_root,
PB2_HEADER,
fr"\g<1>{LICENSE}\n\n\g<2>", # change order to avoid stacking replacements
rf"\g<1>{LICENSE}\n\n\g<2>", # change order to avoid stacking replacements
flags=re.DOTALL | re.MULTILINE,
)

Expand All @@ -65,7 +65,7 @@ def fix_pb2_grpc_headers(*, proto_root: str = "**/*_pb2_grpc.py") -> None:
s.replace(
proto_root,
PB2_GRPC_HEADER,
fr"{LICENSE}\n\n\g<1>\n\n\g<2>", # add line breaks to avoid stacking replacements
rf"{LICENSE}\n\n\g<1>\n\n\g<2>", # add line breaks to avoid stacking replacements
)


Expand Down Expand Up @@ -150,15 +150,15 @@ def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None:

def configure_previous_major_version_branches() -> None:
"""Configure releases from previous major version branches by editing
`.github/release-please.yml`.
`.github/release-please.yml`.
The current library version is obtained from `version.py` in `google/**/version.py`,
or the `setup.py`.
The current library version is obtained from `version.py` in `google/**/version.py`,
or the `setup.py`.
Releases are configured for all previous major versions. For example,
if the library version is currently 3.5.1, the release-please config
will include v2, v1, and v0.
"""
Releases are configured for all previous major versions. For example,
if the library version is currently 3.5.1, the release-please config
will include v2, v1, and v0.
"""
if list(Path(".").glob("google/**/version.py")):
version_file = list(Path(".").glob("google/**/version.py"))[0]
else:
Expand Down
4 changes: 3 additions & 1 deletion synthtool/protos/metadata_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion synthtool/protos/preconfig_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion synthtool/sources/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def _local_default_branch(path: pathlib.Path) -> Union[str, None]:


def clone(
url: str, dest: pathlib.Path = None, committish: str = None, force: bool = False,
url: str,
dest: pathlib.Path = None,
committish: str = None,
force: bool = False,
) -> pathlib.Path:
"""Clones a remote git repo.
Expand Down
7 changes: 5 additions & 2 deletions synthtool/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def _copy_dir_to_existing_dir(
_merge_file(source_path, dest_path, merge)
except Exception:
logger.exception(
"_merge_file failed for %s, fall back to copy", source_path,
"_merge_file failed for %s, fall back to copy",
source_path,
)
shutil.copy2(str(source_path), str(dest_path))
else:
Expand All @@ -147,7 +148,9 @@ def _copy_dir_to_existing_dir(
return copied


def dont_overwrite(patterns: ListOfPathsOrStrs,) -> Callable[[str, str, Path], str]:
def dont_overwrite(
patterns: ListOfPathsOrStrs,
) -> Callable[[str, str, Path], str]:
"""Returns a merge function that doesn't overwrite the specified files.
Pass the return value to move() or copy() to avoid overwriting existing
Expand Down
6 changes: 2 additions & 4 deletions tests/test_php.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def docker_image():

@pytest.fixture(scope="session")
def hybrid_tmp_path():
"""A tmp dir implementation both for local run and on Kokoro.
"""
"""A tmp dir implementation both for local run and on Kokoro."""
# Trampoline mount KOKORO_ROOT at the same path.
# So we can mount files under there with docker in docker.
hybrid_dir = os.environ.get("KOKORO_ROOT", None)
Expand All @@ -65,8 +64,7 @@ def hybrid_tmp_path():

@pytest.fixture(scope="function", params=["php_asset"])
def copy_fixture(request, hybrid_tmp_path):
"""A fixture for preparing test data.
"""
"""A fixture for preparing test data."""
param = request.param
test_dir = Path(f"{hybrid_tmp_path}/{param}")

Expand Down
5 changes: 4 additions & 1 deletion tests/test_python_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@
)
def test_library_noxfile(template_kwargs, expected_text):
t = templates.Templates(PYTHON_LIBRARY)
result = t.render("noxfile.py.j2", **template_kwargs,).read_text()
result = t.render(
"noxfile.py.j2",
**template_kwargs,
).read_text()
# Validate Python syntax.
result_code = compile(result, "noxfile.py", "exec")
assert result_code is not None
Expand Down

0 comments on commit 0dcf739

Please sign in to comment.