def run_mypy(
    rule_runner: RuleRunner,
    targets: List[Target],
    *,
    config: Optional[str] = None,
    passthrough_args: Optional[str] = None,
    skip: bool = False,
    additional_args: Optional[List[str]] = None,
) -> Sequence[TypecheckResult]:
    args = list(GLOBAL_ARGS)
    if config:
        rule_runner.create_file(relpath="mypy.ini", contents=config)
        args.append("--mypy-config=mypy.ini")
    if passthrough_args:
        args.append(f"--mypy-args='{passthrough_args}'")
    if skip:
        args.append("--mypy-skip")
    if additional_args:
        args.extend(additional_args)
    rule_runner.set_options(args)
    result = rule_runner.request(
        TypecheckResults,
        [MyPyRequest(MyPyFieldSet.create(tgt) for tgt in targets)],
    )
    return result.results
예제 #2
0
 def run_mypy(
     self,
     targets: List[Target],
     *,
     config: Optional[str] = None,
     passthrough_args: Optional[str] = None,
     skip: bool = False,
     additional_args: Optional[List[str]] = None,
 ) -> Sequence[TypecheckResult]:
     args = list(self.global_args)
     if config:
         self.create_file(relpath="mypy.ini", contents=config)
         args.append("--mypy-config=mypy.ini")
     if passthrough_args:
         args.append(f"--mypy-args='{passthrough_args}'")
     if skip:
         args.append("--mypy-skip")
     if additional_args:
         args.extend(additional_args)
     result = self.request_product(
         TypecheckResults,
         [
             MyPyRequest(MyPyFieldSet.create(tgt) for tgt in targets),
             create_options_bootstrapper(args=args),
         ],
     )
     return result.results
예제 #3
0
 def run_mypy(
     self,
     targets: List[TargetWithOrigin],
     *,
     config: Optional[str] = None,
     passthrough_args: Optional[str] = None,
     skip: bool = False,
     additional_args: Optional[List[str]] = None,
 ) -> TypecheckResults:
     args = [
         "--backend-packages=pants.backend.python",
         "--backend-packages=pants.backend.python.typecheck.mypy",
         "--source-root-patterns=['src/python', 'tests/python']",
     ]
     if config:
         self.create_file(relpath="mypy.ini", contents=config)
         args.append("--mypy-config=mypy.ini")
     if passthrough_args:
         args.append(f"--mypy-args='{passthrough_args}'")
     if skip:
         args.append("--mypy-skip")
     if additional_args:
         args.extend(additional_args)
     return self.request_single_product(
         TypecheckResults,
         Params(
             MyPyRequest(MyPyFieldSet.create(tgt) for tgt in targets),
             create_options_bootstrapper(args=args),
         ),
     )
예제 #4
0
def run_mypy(
    rule_runner: RuleRunner, targets: list[Target], *, extra_args: list[str] | None = None
) -> tuple[CheckResult, ...]:
    rule_runner.set_options(extra_args or (), env_inherit={"PATH", "PYENV_ROOT", "HOME"})
    result = rule_runner.request(
        CheckResults, [MyPyRequest(MyPyFieldSet.create(tgt) for tgt in targets)]
    )
    return result.results
예제 #5
0
def run_mypy(rule_runner: RuleRunner,
             targets: list[Target],
             *,
             extra_args: list[str] | None = None) -> tuple[CheckResult, ...]:
    rule_runner.set_options(
        [
            "--backend-packages=pants.backend.python.typecheck.mypy",
            *(extra_args or ())
        ],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )
    result = rule_runner.request(
        CheckResults,
        [MyPyRequest(MyPyFieldSet.create(tgt) for tgt in targets)],
    )
    return result.results
예제 #6
0
def test_partition_targets(rule_runner: RuleRunner) -> None:
    def create_folder(folder: str, resolve: str,
                      interpreter: str) -> dict[str, str]:
        return {
            f"{folder}/dep.py":
            "",
            f"{folder}/root.py":
            "",
            f"{folder}/BUILD":
            dedent(f"""\
                python_source(
                    name='dep',
                    source='dep.py',
                    resolve='{resolve}',
                    interpreter_constraints=['=={interpreter}.*'],
                )
                python_source(
                    name='root',
                    source='root.py',
                    resolve='{resolve}',
                    interpreter_constraints=['=={interpreter}.*'],
                    dependencies=[':dep'],
                )
                """),
        }

    files = {
        **create_folder("resolveA_py38", "a", "3.8"),
        **create_folder("resolveA_py39", "a", "3.9"),
        **create_folder("resolveB_1", "b", "3.9"),
        **create_folder("resolveB_2", "b", "3.9"),
    }
    rule_runner.write_files(files)  # type: ignore[arg-type]
    rule_runner.set_options(
        ["--python-resolves={'a': '', 'b': ''}", "--python-enable-resolves"],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )

    resolve_a_py38_dep = rule_runner.get_target(
        Address("resolveA_py38", target_name="dep"))
    resolve_a_py38_root = rule_runner.get_target(
        Address("resolveA_py38", target_name="root"))
    resolve_a_py39_dep = rule_runner.get_target(
        Address("resolveA_py39", target_name="dep"))
    resolve_a_py39_root = rule_runner.get_target(
        Address("resolveA_py39", target_name="root"))
    resolve_b_dep1 = rule_runner.get_target(
        Address("resolveB_1", target_name="dep"))
    resolve_b_root1 = rule_runner.get_target(
        Address("resolveB_1", target_name="root"))
    resolve_b_dep2 = rule_runner.get_target(
        Address("resolveB_2", target_name="dep"))
    resolve_b_root2 = rule_runner.get_target(
        Address("resolveB_2", target_name="root"))
    request = MyPyRequest(
        MyPyFieldSet.create(t) for t in (
            resolve_a_py38_root,
            resolve_a_py39_root,
            resolve_b_root1,
            resolve_b_root2,
        ))

    partitions = rule_runner.request(MyPyPartitions, [request])
    assert len(partitions) == 3

    def assert_partition(
        partition: MyPyPartition,
        roots: list[Target],
        deps: list[Target],
        interpreter: str,
        resolve: str,
    ) -> None:
        root_addresses = {t.address for t in roots}
        assert {fs.address
                for fs in partition.root_field_sets} == root_addresses
        assert {t.address
                for t in partition.closure} == {
                    *root_addresses,
                    *(t.address for t in deps),
                }
        ics = [f"CPython=={interpreter}.*"]
        assert partition.interpreter_constraints == InterpreterConstraints(ics)
        assert partition.description() == f"{resolve}, {ics}"

    assert_partition(partitions[0], [resolve_a_py38_root],
                     [resolve_a_py38_dep], "3.8", "a")
    assert_partition(partitions[1], [resolve_a_py39_root],
                     [resolve_a_py39_dep], "3.9", "a")
    assert_partition(
        partitions[2],
        [resolve_b_root1, resolve_b_root2],
        [resolve_b_dep1, resolve_b_dep2],
        "3.9",
        "b",
    )