Skip to content

Commit

Permalink
fmt check lint
Browse files Browse the repository at this point in the history
  • Loading branch information
thejcannon committed Jan 5, 2022
1 parent 43c03d8 commit 39b6655
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from collections import defaultdict
from json.decoder import JSONDecodeError
import pathlib
import json
import pathlib
from collections import defaultdict
from dataclasses import dataclass
from json.decoder import JSONDecodeError
from typing import Tuple

from pants.backend.python.target_types import PythonSourceField
Expand Down Expand Up @@ -165,8 +165,8 @@ class ParsedPythonImports(DeduplicatedCollection[str]):
class ParsedPythonResources(DeduplicatedCollection[Tuple[str, str]]):
"""All the discovered possible resources from a Python source file.
The tuple is of (containing module, relative filename), similar to
the arguments of `pkgutil.get_data`.
The tuple is of (containing module, relative filename), similar to the arguments of
`pkgutil.get_data`.
"""

# N.B. Don't set `sort_input`, as the input is already sorted
Expand All @@ -187,9 +187,11 @@ class ParsePythonDependenciesRequest:
string_resources: bool
string_resources_min_slashes: int

def _filepath_to_modname(filepath: str):

def _filepath_to_modname(filepath: str) -> str:
return str(pathlib.Path(filepath).with_suffix("")).replace("/", ".")


@rule
async def parse_python_dependencies(
request: ParsePythonDependenciesRequest,
Expand Down Expand Up @@ -237,13 +239,15 @@ async def parse_python_dependencies(

return ParsedPythonDependencies(
imports=output["imports"],
resources=[
(
_filepath_to_modname(request.source.file_path) if pkgname is None else pkgname,
filepath
)
for pkgname, filepath in output["resources"]
],
resources=ParsedPythonResources(
[
(
_filepath_to_modname(request.source.file_path) if pkgname is None else pkgname,
filepath,
)
for pkgname, filepath in output["resources"]
]
),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ def assert_deps_parsed(
],
)
assert list(result.imports) == sorted(expected_imports)
assert list(result.resources) == sorted(("project.foo", resource) for resource in expected_resources)
assert list(result.resources) == sorted(
("project.foo", resource) for resource in expected_resources
)


def test_normal_imports(rule_runner: RuleRunner) -> None:
Expand Down Expand Up @@ -121,6 +123,7 @@ def test_normal_imports(rule_runner: RuleRunner) -> None:
],
)


@pytest.mark.parametrize("basename", ["foo.py", "__init__.py"])
def test_relative_imports(rule_runner: RuleRunner, basename: str) -> None:
content = dedent(
Expand Down Expand Up @@ -193,9 +196,12 @@ def test_imports_from_strings(rule_runner: RuleRunner, min_dots: int) -> None:
]
expected = [sym for sym in potentially_valid if sym.count(".") >= min_dots]

assert_deps_parsed(rule_runner, content, expected_imports=expected, string_imports_min_dots=min_dots)
assert_deps_parsed(
rule_runner, content, expected_imports=expected, string_imports_min_dots=min_dots
)
assert_deps_parsed(rule_runner, content, string_imports=False, expected_imports=[])


@pytest.mark.parametrize("min_slashes", [1, 2, 3, 4])
def test_resources_from_strings(rule_runner: RuleRunner, min_slashes: int) -> None:
content = dedent(
Expand All @@ -222,17 +228,19 @@ def test_resources_from_strings(rule_runner: RuleRunner, min_slashes: int) -> No
)

potentially_valid = [
'a/b.txt',
'a/Foo.txt',
'a/b/d.json',
'a/b2/d.data',
'a/b/c/d.gz.tar',
'a/b_c/d.7zip',
'a/b/c_狗.txt',
"a/b.txt",
"a/Foo.txt",
"a/b/d.json",
"a/b2/d.data",
"a/b/c/d.gz.tar",
"a/b_c/d.7zip",
"a/b/c_狗.txt",
]
expected = [sym for sym in potentially_valid if sym.count("/") >= min_slashes]

assert_deps_parsed(rule_runner, content, expected_resources=expected, string_resources_min_slashes=min_slashes)
assert_deps_parsed(
rule_runner, content, expected_resources=expected, string_resources_min_slashes=min_slashes
)
assert_deps_parsed(rule_runner, content, string_resources=False, expected_resources=[])


Expand Down
17 changes: 10 additions & 7 deletions src/python/pants/backend/python/dependency_inference/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import pathlib
from enum import Enum
from typing import cast, Dict
from typing import Dict, cast

from pants.backend.python.dependency_inference import dependency_parser, module_mapper
from pants.backend.python.dependency_inference.default_unowned_dependencies import (
Expand All @@ -23,6 +23,7 @@
from pants.backend.python.util_rules import ancestor_files, pex
from pants.backend.python.util_rules.ancestor_files import AncestorFiles, AncestorFilesRequest
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.core.target_types import FileSourceField, FileTarget, ResourceSourceField, ResourceTarget
from pants.core.util_rules import stripped_source_files
from pants.engine.addresses import Address
from pants.engine.internals.graph import Owners, OwnersRequest
Expand All @@ -40,14 +41,15 @@
from pants.option.subsystem import Subsystem
from pants.util.docutil import doc_url
from pants.util.strutil import bullet_list
from pants.core.target_types import FileSourceField, FileTarget, ResourceSourceField, ResourceTarget

logger = logging.getLogger(__name__)

# @TODO: Find a home for these
# @TODO: Find a home for these

from dataclasses import dataclass # noqa: E402

from pants.engine.target import AllTargets, Target # noqa: E402

from pants.engine.target import AllTargets, Target
from dataclasses import dataclass

@dataclass(frozen=True)
class AllResourcesTargets:
Expand All @@ -66,6 +68,7 @@ def find_all_resources(all_targets: AllTargets) -> AllResourcesTargets:
files.append(tgt)
return AllResourcesTargets(tuple(resources), tuple(files))


# ==========================


Expand Down Expand Up @@ -248,7 +251,7 @@ async def infer_python_dependencies_via_imports(
resources_by_path[path] = resource_tgt

for pkgname, filepath in detected_resources:
resource_path = pathlib.Path(*pkgname.split('.')).parent / filepath
resource_path = pathlib.Path(*pkgname.split(".")).parent / filepath
inferred_resource_tgt = resources_by_path.get(resource_path)
if inferred_resource_tgt:
inferred_deps.add(inferred_resource_tgt.address)
Expand Down Expand Up @@ -357,6 +360,7 @@ def import_rules():
UnionRule(InferDependenciesRequest, InferPythonImportDependencies),
]


def rules():
return [
*import_rules(),
Expand All @@ -366,4 +370,3 @@ def rules():
UnionRule(InferDependenciesRequest, InferInitDependencies),
UnionRule(InferDependenciesRequest, InferConftestDependencies),
]

Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
PythonTestsGeneratorTarget,
PythonTestUtilsGeneratorTarget,
)
from pants.backend.python.util_rules import ancestor_files
from pants.core.target_types import ResourcesGeneratorTarget
from pants.core.target_types import rules as core_target_types_rules
from pants.backend.python.util_rules import ancestor_files
from pants.engine.addresses import Address
from pants.engine.internals.scheduler import ExecutionError
from pants.engine.rules import SubsystemRule
Expand Down Expand Up @@ -165,7 +165,11 @@ def test_infer_python_resources() -> None:
*core_target_types_rules(),
QueryRule(InferredDependencies, [InferPythonImportDependencies]),
],
target_types=[PythonSourcesGeneratorTarget, PythonRequirementTarget, ResourcesGeneratorTarget],
target_types=[
PythonSourcesGeneratorTarget,
PythonRequirementTarget,
ResourcesGeneratorTarget,
],
)
rule_runner.write_files(
{
Expand Down

0 comments on commit 39b6655

Please sign in to comment.