From ca9ed5473ff9e5fb2353b349c7ff958a7e4b84a7 Mon Sep 17 00:00:00 2001 From: Eric Arellano Date: Thu, 2 Jan 2020 14:50:23 -0700 Subject: [PATCH] Fix `./pants lint2` for Black and isort (#8877) https://github.com/pantsbuild/pants/pull/8823 broke `./pants lint2` for formatters like Black and Isort because `python_lint_target.py` was not passing the argument `prior_formatter_result_digest` to the constructors for `IsortTarget` and `BlackTarget`. However, `prior_formatter_result_digest` is only relevant to `fmt2` and the lint logic should never have to deal with the argument. So, here, we set the field to `Optional` with a default of `None`. `python_fmt_target.py` will ensure that the value is always set when formatting, whereas `python_lint_target.py` will ignore the value as before. This also updates tests to ensure that both code paths are tested. --- pants.ini | 2 +- .../pants/backend/python/lint/black/rules.py | 4 +-- .../lint/black/rules_integration_test.py | 33 +++++++++---------- .../pants/backend/python/lint/isort/rules.py | 4 +-- .../lint/isort/rules_integration_test.py | 33 +++++++++---------- 5 files changed, 37 insertions(+), 39 deletions(-) diff --git a/pants.ini b/pants.ini index 0550b8e9cb5..2fe576eda92 100644 --- a/pants.ini +++ b/pants.ini @@ -45,7 +45,6 @@ pythonpath: [ ] backend_packages: +[ - "pants.backend.python.lint.isort", "pants.backend.docgen", "pants.contrib.avro", "pants.contrib.awslambda.python", @@ -71,6 +70,7 @@ backend_packages: +[ backend_packages2: [ "pants.backend.project_info", "pants.backend.python", + "pants.backend.python.lint.isort", "pants.backend.native", "internal_backend.rules_for_testing", ] diff --git a/src/python/pants/backend/python/lint/black/rules.py b/src/python/pants/backend/python/lint/black/rules.py index 6fbd8776f1e..f61e057f533 100644 --- a/src/python/pants/backend/python/lint/black/rules.py +++ b/src/python/pants/backend/python/lint/black/rules.py @@ -33,7 +33,7 @@ @dataclass(frozen=True) class BlackTarget: target: TargetAdaptor - prior_formatter_result_digest: Digest + prior_formatter_result_digest: Optional[Digest] = None # unused by `lint` @dataclass(frozen=True) @@ -99,7 +99,7 @@ async def create_black_request( subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> ExecuteProcessRequest: target = wrapped_target.target - sources_digest = wrapped_target.prior_formatter_result_digest + sources_digest = wrapped_target.prior_formatter_result_digest or target.sources.snapshot.directory_digest merged_input_files = await Get[Digest]( DirectoriesToMerge( directories=( diff --git a/src/python/pants/backend/python/lint/black/rules_integration_test.py b/src/python/pants/backend/python/lint/black/rules_integration_test.py index ae769d1f5d1..62ba203e41c 100644 --- a/src/python/pants/backend/python/lint/black/rules_integration_test.py +++ b/src/python/pants/backend/python/lint/black/rules_integration_test.py @@ -70,33 +70,32 @@ def run_black( if config is not None: self.create_file(relpath="pyproject.toml", contents=config) input_snapshot = self.request_single_product(Snapshot, InputFilesContent(source_files)) - target = BlackTarget( - TargetAdaptor( - sources=EagerFilesetWithSpec('test', {'globs': []}, snapshot=input_snapshot), - address=Address.parse("test:target"), - ), - prior_formatter_result_digest=input_snapshot.directory_digest, + target_adaptor = TargetAdaptor( + sources=EagerFilesetWithSpec('test', {'globs': []}, snapshot=input_snapshot), + address=Address.parse("test:target"), ) + lint_target = BlackTarget(target_adaptor) + fmt_target = BlackTarget(target_adaptor, prior_formatter_result_digest=input_snapshot.directory_digest) black_subsystem = global_subsystem_instance( Black, options={Black.options_scope: { "config": "pyproject.toml" if config else None, "args": passthrough_args or [], }} ) + python_subsystems = [ + PythonNativeCode.global_instance(), + PythonSetup.global_instance(), + SubprocessEnvironment.global_instance(), + ] black_setup = self.request_single_product( - BlackSetup, - Params( - black_subsystem, - PythonNativeCode.global_instance(), - PythonSetup.global_instance(), - SubprocessEnvironment.global_instance(), - ) + BlackSetup, Params(black_subsystem, *python_subsystems) ) - fmt_and_lint_params = Params( - target, black_setup, PythonSetup.global_instance(), SubprocessEnvironment.global_instance() + lint_result = self.request_single_product( + LintResult, Params(lint_target, black_setup, *python_subsystems) + ) + fmt_result = self.request_single_product( + FmtResult, Params(fmt_target, black_setup, *python_subsystems) ) - lint_result = self.request_single_product(LintResult, fmt_and_lint_params) - fmt_result = self.request_single_product(FmtResult, fmt_and_lint_params) return lint_result, fmt_result def get_digest(self, source_files: List[FileContent]) -> Digest: diff --git a/src/python/pants/backend/python/lint/isort/rules.py b/src/python/pants/backend/python/lint/isort/rules.py index 3b52e6af5cc..3695d57f8a8 100644 --- a/src/python/pants/backend/python/lint/isort/rules.py +++ b/src/python/pants/backend/python/lint/isort/rules.py @@ -31,7 +31,7 @@ @dataclass(frozen=True) class IsortTarget: target: TargetAdaptor - prior_formatter_result_digest: Digest + prior_formatter_result_digest: Optional[Digest] = None # unused by `lint` @dataclass(frozen=True) @@ -91,7 +91,7 @@ async def create_isort_request( subprocess_encoding_environment: SubprocessEncodingEnvironment, ) -> ExecuteProcessRequest: target = wrapped_target.target - sources_digest = wrapped_target.prior_formatter_result_digest + sources_digest = wrapped_target.prior_formatter_result_digest or target.sources.snapshot.directory_digest merged_input_files = await Get[Digest]( DirectoriesToMerge( directories=( diff --git a/src/python/pants/backend/python/lint/isort/rules_integration_test.py b/src/python/pants/backend/python/lint/isort/rules_integration_test.py index aeb46775935..84458ada2b1 100644 --- a/src/python/pants/backend/python/lint/isort/rules_integration_test.py +++ b/src/python/pants/backend/python/lint/isort/rules_integration_test.py @@ -78,33 +78,32 @@ def run_isort( if config is not None: self.create_file(relpath=".isort.cfg", contents=config) input_snapshot = self.request_single_product(Snapshot, InputFilesContent(source_files)) - target = IsortTarget( - TargetAdaptor( - sources=EagerFilesetWithSpec('test', {'globs': []}, snapshot=input_snapshot), - address=Address.parse("test:target"), - ), - prior_formatter_result_digest=input_snapshot.directory_digest, + target_adaptor = TargetAdaptor( + sources=EagerFilesetWithSpec('test', {'globs': []}, snapshot=input_snapshot), + address=Address.parse("test:target"), ) + lint_target = IsortTarget(target_adaptor) + fmt_target = IsortTarget(target_adaptor, prior_formatter_result_digest=input_snapshot.directory_digest) isort_subsystem = global_subsystem_instance( Isort, options={Isort.options_scope: { "config": [".isort.cfg"] if config else None, "args": passthrough_args or [], }} ) + python_subsystems = [ + PythonNativeCode.global_instance(), + PythonSetup.global_instance(), + SubprocessEnvironment.global_instance(), + ] isort_setup = self.request_single_product( - IsortSetup, - Params( - isort_subsystem, - PythonNativeCode.global_instance(), - PythonSetup.global_instance(), - SubprocessEnvironment.global_instance(), - ) + IsortSetup, Params(isort_subsystem, *python_subsystems) ) - fmt_and_lint_params = Params( - target, isort_setup, PythonSetup.global_instance(), SubprocessEnvironment.global_instance() + lint_result = self.request_single_product( + LintResult, Params(lint_target, isort_setup, *python_subsystems) + ) + fmt_result = self.request_single_product( + FmtResult, Params(fmt_target, isort_setup, *python_subsystems) ) - lint_result = self.request_single_product(LintResult, fmt_and_lint_params) - fmt_result = self.request_single_product(FmtResult, fmt_and_lint_params) return lint_result, fmt_result def get_digest(self, source_files: List[FileContent]) -> Digest: