def test_group_field_sets_by_constraints_with_unsorted_inputs() -> None: py3_fs = [ MockFieldSet.create_for_test( Address("src/python/a_dir/path.py", target_name="test"), "==3.6.*" ), MockFieldSet.create_for_test( Address("src/python/b_dir/path.py", target_name="test"), ">2.7,<3" ), MockFieldSet.create_for_test( Address("src/python/c_dir/path.py", target_name="test"), "==3.6.*" ), ] ic_36 = InterpreterConstraints([Requirement.parse("CPython==3.6.*")]) output = InterpreterConstraints.group_field_sets_by_constraints( py3_fs, python_setup=create_subsystem(PythonSetup, interpreter_constraints=[]), ) assert output[ic_36] == ( MockFieldSet.create_for_test( Address("src/python/a_dir/path.py", target_name="test"), "==3.6.*" ), MockFieldSet.create_for_test( Address("src/python/c_dir/path.py", target_name="test"), "==3.6.*" ), )
async def bandit_lint(request: BanditRequest, bandit: Bandit, python_setup: PythonSetup) -> LintResults: if bandit.skip: return LintResults([], linter_name="Bandit") # NB: Bandit output depends upon which Python interpreter version it's run with # ( https://github.com/PyCQA/bandit#under-which-version-of-python-should-i-install-bandit). We # batch targets by their constraints to ensure, for example, that all Python 2 targets run # together and all Python 3 targets run together. constraints_to_field_sets = InterpreterConstraints.group_field_sets_by_constraints( request.field_sets, python_setup) partitioned_results = await MultiGet( Get(LintResult, BanditPartition(partition_field_sets, partition_compatibility)) for partition_compatibility, partition_field_sets in constraints_to_field_sets.items()) return LintResults(partitioned_results, linter_name="Bandit")
async def flake8_lint(request: Flake8Request, flake8: Flake8, python_setup: PythonSetup) -> LintResults: if flake8.skip: return LintResults([], linter_name="Flake8") # NB: Flake8 output depends upon which Python interpreter version it's run with # (http://flake8.pycqa.org/en/latest/user/invocation.html). We batch targets by their # constraints to ensure, for example, that all Python 2 targets run together and all Python 3 # targets run together. constraints_to_field_sets = InterpreterConstraints.group_field_sets_by_constraints( request.field_sets, python_setup) partitioned_results = await MultiGet( Get(LintResult, Flake8Partition(partition_field_sets, partition_compatibility)) for partition_compatibility, partition_field_sets in constraints_to_field_sets.items()) return LintResults(partitioned_results, linter_name="Flake8")
def test_group_field_sets_by_constraints() -> None: py2_fs = MockFieldSet.create_for_test(Address("", target_name="py2"), ">=2.7,<3") py3_fs = [ MockFieldSet.create_for_test(Address("", target_name="py3"), "==3.6.*"), MockFieldSet.create_for_test(Address("", target_name="py3_second"), "==3.6.*"), ] no_constraints_fs = MockFieldSet.create_for_test( Address("", target_name="no_constraints"), None ) assert InterpreterConstraints.group_field_sets_by_constraints( [py2_fs, *py3_fs, no_constraints_fs], python_setup=create_subsystem(PythonSetup, interpreter_constraints=[]), ) == FrozenDict( { InterpreterConstraints(): (no_constraints_fs,), InterpreterConstraints(["CPython>=2.7,<3"]): (py2_fs,), InterpreterConstraints(["CPython==3.6.*"]): tuple(py3_fs), } )