Skip to content

Commit

Permalink
Upgrade default Black version to 20.8b1 (#10696)
Browse files Browse the repository at this point in the history
See https://github.com/psf/black/blob/master/CHANGES.md. The main improvement is better handling of trailing commas.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
Eric-Arellano committed Aug 28, 2020
1 parent fb7cb5d commit f32a136
Show file tree
Hide file tree
Showing 106 changed files with 735 additions and 298 deletions.
2 changes: 1 addition & 1 deletion build-support/bin/bootstrap_and_deploy_ci_pants_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def create_parser() -> argparse.ArgumentParser:
def calculate_native_engine_so_hash() -> str:
return (
subprocess.run(
["build-support/bin/native/print_engine_hash.sh"], stdout=subprocess.PIPE, check=True,
["build-support/bin/native/print_engine_hash.sh"], stdout=subprocess.PIPE, check=True
)
.stdout.decode()
.strip()
Expand Down
2 changes: 1 addition & 1 deletion build-support/bin/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def get_tpl(name: str) -> str:
options_scope_tpl = get_tpl("options_scope_reference.md.mustache")
single_option_tpl = get_tpl("single_option_reference.md.mustache")
self._renderer = pystache.Renderer(
partials={"scoped_options": options_scope_tpl, "single_option": single_option_tpl,}
partials={"scoped_options": options_scope_tpl, "single_option": single_option_tpl}
)
self._category_id = None # Fetched lazily.

Expand Down
4 changes: 1 addition & 3 deletions build-support/bin/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,7 @@ def check_release_prereqs() -> None:
def list_owners() -> None:
for package in PACKAGES:
if not package.exists_on_pypi():
print(
f"The {package.name} package is new! There are no owners yet.", file=sys.stderr,
)
print(f"The {package.name} package is new! There are no owners yet.", file=sys.stderr)
continue
formatted_owners = "\n".join(sorted(package.owners()))
print(f"Owners of {package.name}:\n{formatted_owners}\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create_parser() -> argparse.ArgumentParser:
description="Convert deprecated `source` fields to `sources`.",
)
parser.add_argument(
"folders", type=Path, nargs="+", help="Folders to recursively search for `BUILD` files",
"folders", type=Path, nargs="+", help="Folders to recursively search for `BUILD` files"
)
parser.add_argument(
"-p",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def test_no_op_when_already_valid(line: str) -> None:


@pytest.mark.parametrize(
"line", ["\n", " 123 ", "python_library()", "name='hello'", "name='sources'",],
"line",
["\n", " 123 ", "python_library()", "name='hello'", "name='sources'"],
)
def test_safe_with_unrelated_lines(line: str) -> None:
assert maybe_rewrite_line(line) is None
Expand Down
8 changes: 4 additions & 4 deletions build-support/migration-support/fix_deprecated_globs_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create_parser() -> argparse.ArgumentParser:
description="Modernize BUILD files to no longer use globs, rglobs, and zglobs.",
)
parser.add_argument(
"folders", type=Path, nargs="+", help="Folders to recursively search for `BUILD` files",
"folders", type=Path, nargs="+", help="Folders to recursively search for `BUILD` files"
)
parser.add_argument(
"-p",
Expand Down Expand Up @@ -165,7 +165,7 @@ def parse(cls, glob_func: ast.Call, *, build_file: Path) -> Optional["GlobFuncti
if glob_type == GlobType.rglobs:
include_globs = [cls.normalize_rglob(include) for include in include_globs]

return GlobFunction(glob_type=glob_type, includes=include_globs, excludes=exclude_globs,)
return GlobFunction(glob_type=glob_type, includes=include_globs, excludes=exclude_globs)

def convert_to_sources_list(self, *, use_single_quotes: bool = False) -> str:
escaped_excludes = [f"!{exclude}" for exclude in self.excludes or ()]
Expand All @@ -181,7 +181,7 @@ def use_single_quotes(line: str) -> bool:


def warning_msg(
*, build_file: Path, lineno: int, field_name: str, replacement: str, script_restriction: str,
*, build_file: Path, lineno: int, field_name: str, replacement: str, script_restriction: str
) -> str:
return (
f"Could not update {build_file} at line {lineno}. This script {script_restriction}. Please "
Expand Down Expand Up @@ -255,7 +255,7 @@ def generate_possibly_new_build(build_file: Path) -> Optional[List[str]]:
)
)
sources_arg: Optional[ast.keyword] = next(
(kwarg for kwarg in target.keywords if kwarg.arg == "sources"), None,
(kwarg for kwarg in target.keywords if kwarg.arg == "sources"), None
)
if not sources_arg or not isinstance(sources_arg.value, ast.Call):
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_excludes() -> None:

# `exclude` elements are globs
assert_rewrite(
original="sources=globs(exclude=[globs('ignore.py')]),", expected="sources=['!ignore.py'],",
original="sources=globs(exclude=[globs('ignore.py')]),", expected="sources=['!ignore.py'],"
)
assert_rewrite(
original="sources=globs(exclude=[globs('ignore.py'), globs('ignore2.py')]),",
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_normalizes_rglobs() -> None:

# Check the intersection with the `exclude` clause
assert_rewrite(
original="sources=rglobs('foo.py', exclude=['*']),", expected="sources=['foo.py', '!*'],",
original="sources=rglobs('foo.py', exclude=['*']),", expected="sources=['foo.py', '!*'],"
)
assert_rewrite(
original="sources=globs('foo.py', exclude=[rglobs('*')]),",
Expand Down Expand Up @@ -206,7 +206,7 @@ def test_correctly_formats_rewrite() -> None:

# Maintain insertion order for includes
assert_rewrite(
original="sources=globs('dog.py', 'cat.py'),", expected="sources=['dog.py', 'cat.py'],",
original="sources=globs('dog.py', 'cat.py'),", expected="sources=['dog.py', 'cat.py'],"
)


Expand All @@ -219,7 +219,7 @@ def test_warns_when_sources_shares_a_line(caplog) -> None:
script_restriction=SCRIPT_RESTRICTIONS["sources_must_be_distinct_line"],
)
assert_warning(
build_file_content="files(sources=globs('foo.py'))", warning_num=0, line_number=1,
build_file_content="files(sources=globs('foo.py'))", warning_num=0, line_number=1
)
assert_warning(
build_file_content=dedent(
Expand Down
8 changes: 3 additions & 5 deletions build-support/migration-support/migrate_to_toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ def main() -> None:


def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Convert INI config files to TOML config files.",)
parser.add_argument(
"files", type=Path, nargs="*", help="Config files to convert.",
)
parser = argparse.ArgumentParser(description="Convert INI config files to TOML config files.")
parser.add_argument("files", type=Path, nargs="*", help="Config files to convert.")
parser.add_argument(
"-p",
"--preview",
Expand Down Expand Up @@ -84,7 +82,7 @@ def generate_new_config(config: Path) -> List[str]:
before_value_regex = rf"\s*{option_regex}\s*[:=]\s*"
valid_value_characters = r"a-zA-Z0-9_.@!:%\*\=\>\<\-\(\)\/"
value_regex = rf"(?P<value>[{valid_value_characters}]+)"
parsed_line = re.match(rf"{before_value_regex}{value_regex}\s*$", line,)
parsed_line = re.match(rf"{before_value_regex}{value_regex}\s*$", line)

if parsed_line:
option, value = parsed_line.groups()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def setup_lambdex(lambdex: Lambdex) -> LambdexSetup:
entry_point=lambdex.entry_point,
),
)
return LambdexSetup(requirements_pex=requirements_pex,)
return LambdexSetup(requirements_pex=requirements_pex)


def rules():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_to_interpreter_version(runtime, expected_major, expected_minor):
).to_interpreter_version()


@pytest.mark.parametrize(["invalid_runtime"], (["python88.99"], ["fooobar"],))
@pytest.mark.parametrize(["invalid_runtime"], (["python88.99"], ["fooobar"]))
def test_runtime_validation(invalid_runtime):
with pytest.raises(InvalidFieldException):
PythonAwsLambdaRuntime(raw_value=invalid_runtime, address=Address.parse("foo/bar:baz"))
2 changes: 1 addition & 1 deletion src/python/pants/backend/codegen/protobuf/python/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def generate_python_from_protobuf(
),
)
target_stripped_sources_request = Get(
StrippedSourceFiles, SourceFilesRequest([request.protocol_target[ProtobufSources]]),
StrippedSourceFiles, SourceFilesRequest([request.protocol_target[ProtobufSources]])
)

(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def assert_files_generated(
)
tgt = self.request_product(WrappedTarget, [Address.parse(spec), bootstrapper]).target
protocol_sources = self.request_product(
HydratedSources, [HydrateSourcesRequest(tgt[ProtobufSources]), bootstrapper],
HydratedSources, [HydrateSourcesRequest(tgt[ProtobufSources]), bootstrapper]
)
generated_sources = self.request_product(
GeneratedSources,
Expand Down
4 changes: 1 addition & 3 deletions src/python/pants/backend/project_info/dependees_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ def test_closed(self) -> None:
)

def test_transitive(self) -> None:
self.assert_dependees(
targets=["base"], transitive=True, expected=["intermediate", "leaf"],
)
self.assert_dependees(targets=["base"], transitive=True, expected=["intermediate", "leaf"])
self.assert_dependees(
targets=["base"],
transitive=True,
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/project_info/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Dependencies(Goal):

@goal_rule
async def dependencies(
console: Console, addresses: Addresses, dependencies_subsystem: DependenciesSubsystem,
console: Console, addresses: Addresses, dependencies_subsystem: DependenciesSubsystem
) -> Dependencies:
if dependencies_subsystem.transitive:
transitive_targets = await Get(TransitiveTargets, Addresses, addresses)
Expand Down
4 changes: 1 addition & 3 deletions src/python/pants/backend/project_info/dependencies_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ def test_no_target(self) -> None:
def test_no_dependencies(self) -> None:
self.create_python_library(path="some/target")
self.assert_dependencies(specs=["some/target"], expected=[])
self.assert_dependencies(
specs=["some/target"], expected=[], transitive=True,
)
self.assert_dependencies(specs=["some/target"], expected=[], transitive=True)

def test_python_dependencies(self) -> None:
self.create_python_requirement_library(name="req1")
Expand Down
16 changes: 10 additions & 6 deletions src/python/pants/backend/project_info/filedeps_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def setup_target(
if sources:
self.create_files(path, sources)
self.add_to_build_file(
path, f"tgt(sources={sources or []}, dependencies={dependencies or []})",
path,
f"tgt(sources={sources or []}, dependencies={dependencies or []})",
)

def assert_filedeps(
Expand Down Expand Up @@ -86,13 +87,15 @@ def test_one_target_one_source_with_dep(self) -> None:
self.setup_target("dep/target", sources=["file.py"])
self.setup_target("some/target", sources=["file.py"], dependencies=["dep/target"])
direct_files = {"some/target/BUILD", "some/target/file.py"}
self.assert_filedeps(
targets=["some/target"], expected=direct_files,
)
self.assert_filedeps(targets=["some/target"], expected=direct_files)
self.assert_filedeps(
targets=["some/target"],
transitive=True,
expected={*direct_files, "dep/target/BUILD", "dep/target/file.py",},
expected={
*direct_files,
"dep/target/BUILD",
"dep/target/file.py",
},
)

def test_multiple_targets_one_source(self) -> None:
Expand Down Expand Up @@ -120,7 +123,8 @@ def test_multiple_targets_one_source_with_dep(self) -> None:
"other/target/file.py",
}
self.assert_filedeps(
targets=["some/target", "other/target"], expected=direct_files,
targets=["some/target", "other/target"],
expected=direct_files,
)
self.assert_filedeps(
targets=["some/target", "other/target"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ def register_options(cls, register):
"""
)
super().register_options(register)
register(
"--config", type=dict, fromfile=True, help=schema_help,
)
register("--config", type=dict, fromfile=True, help=schema_help)

@memoized_method
def get_multi_matcher(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ async def map_third_party_modules_to_addresses() -> ThirdPartyModuleToAddressMap
module_map = tgt.get(ModuleMappingField).value or {} # type: ignore[var-annotated]
for python_req in tgt[PythonRequirementsField].value:
modules = module_map.get(
python_req.project_name, [python_req.project_name.lower().replace("-", "_")],
python_req.project_name,
[python_req.project_name.lower().replace("-", "_")],
)
for module in modules:
if module in modules_to_addresses:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ def test_map_first_party_modules_to_addresses(self) -> None:
assert result.mapping == FrozenDict(
{
"project.util.dirutil": Address(
"src/python/project/util", relative_file_path="dirutil.py", target_name="util",
"src/python/project/util",
relative_file_path="dirutil.py",
target_name="util",
),
"project.util.tarutil": Address(
"src/python/project/util", relative_file_path="tarutil.py", target_name="util",
"src/python/project/util",
relative_file_path="tarutil.py",
target_name="util",
),
"project_test.demo_test": Address(
"tests/python/project_test/demo_test",
Expand Down Expand Up @@ -195,7 +199,9 @@ def get_owner(module: str) -> Optional[Address]:
self.create_file("source_root2/project/subdir/__init__.py")
self.add_to_build_file("source_root2/project/subdir", "python_library()")
assert get_owner("project.subdir") == Address(
"source_root2/project/subdir", relative_file_path="__init__.py", target_name="subdir",
"source_root2/project/subdir",
relative_file_path="__init__.py",
target_name="subdir",
)

# Test a module with no owner (stdlib). This also sanity checks that we can handle when
Expand Down
9 changes: 6 additions & 3 deletions src/python/pants/backend/python/dependency_inference/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ async def infer_python_init_dependencies(
# Locate __init__.py files not already in the Snapshot.
hydrated_sources = await Get(HydratedSources, HydrateSourcesRequest(request.sources_field))
extra_init_files = await Get(
AncestorFiles, AncestorFilesRequest("__init__.py", hydrated_sources.snapshot),
AncestorFiles,
AncestorFilesRequest("__init__.py", hydrated_sources.snapshot),
)

# And add dependencies on their owners.
Expand All @@ -171,15 +172,17 @@ class InferConftestDependencies(InferDependenciesRequest):

@rule
async def infer_python_conftest_dependencies(
request: InferConftestDependencies, python_inference: PythonInference,
request: InferConftestDependencies,
python_inference: PythonInference,
) -> InferredDependencies:
if not python_inference.conftests:
return InferredDependencies([], sibling_dependencies_inferrable=False)

# Locate conftest.py files not already in the Snapshot.
hydrated_sources = await Get(HydratedSources, HydrateSourcesRequest(request.sources_field))
extra_conftest_files = await Get(
AncestorFiles, AncestorFilesRequest("conftest.py", hydrated_sources.snapshot),
AncestorFiles,
AncestorFilesRequest("conftest.py", hydrated_sources.snapshot),
)

# And add dependencies on their owners.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def run_dep_inference(

def test_infer_python_inits(self) -> None:
options_bootstrapper = create_options_bootstrapper(
args=["--backend-packages=pants.backend.python", "--source-root-patterns=src/python",]
args=[
"--backend-packages=pants.backend.python",
"--source-root-patterns=src/python",
]
)

self.create_file("src/python/root/__init__.py")
Expand Down Expand Up @@ -159,7 +162,10 @@ def run_dep_inference(address: Address) -> InferredDependencies:

def test_infer_python_conftests(self) -> None:
options_bootstrapper = create_options_bootstrapper(
args=["--backend-packages=pants.backend.python", "--source-root-patterns=src/python",]
args=[
"--backend-packages=pants.backend.python",
"--source-root-patterns=src/python",
]
)

self.create_file("src/python/root/conftest.py")
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/lint/black/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Setup:
original_digest: Digest


def generate_args(*, source_files: SourceFiles, black: Black, check_only: bool,) -> Tuple[str, ...]:
def generate_args(*, source_files: SourceFiles, black: Black, check_only: bool) -> Tuple[str, ...]:
args = []
if check_only:
args.append("--check")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_respects_config_file(self) -> None:
def test_respects_passthrough_args(self) -> None:
target = self.make_target([self.needs_config_source])
lint_results, fmt_result = self.run_black(
[target], passthrough_args="--skip-string-normalization",
[target], passthrough_args="--skip-string-normalization"
)
assert len(lint_results) == 1
assert lint_results[0].exit_code == 0
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/lint/black/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Black(PythonToolBase):
"""The Black Python code formatter (https://black.readthedocs.io/)."""

options_scope = "black"
default_version = "black==19.10b0"
default_version = "black==20.8b1"
default_extra_requirements = ["setuptools"]
default_entry_point = "black:patched_main"
default_interpreter_constraints = ["CPython>=3.6"]
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/lint/docformatter/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Setup:


def generate_args(
*, source_files: SourceFiles, docformatter: Docformatter, check_only: bool,
*, source_files: SourceFiles, docformatter: Docformatter, check_only: bool
) -> Tuple[str, ...]:
return ("--check" if check_only else "--in-place", *docformatter.args, *source_files.files)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def make_target(self, source_files: List[FileContent]) -> Target:
return PythonLibrary({}, address=Address.parse(":target"))

def run_docformatter(
self, targets: List[Target], *, passthrough_args: Optional[str] = None, skip: bool = False,
self, targets: List[Target], *, passthrough_args: Optional[str] = None, skip: bool = False
) -> Tuple[Sequence[LintResult], FmtResult]:
args = ["--backend-packages=pants.backend.python.lint.docformatter"]
if passthrough_args:
Expand Down

0 comments on commit f32a136

Please sign in to comment.