Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental: plugins: extended inferred dependencies from a plugin #121

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions helloworld/greet/greeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from helloworld.translator.translator import LanguageTranslator

from helloworld.translator.foobar import baz


class Greeter:
def __init__(
Expand Down
Empty file added pants-plugins/__init__.py
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from internal_plugins.unowned_dependencies.rules import rules as unowned_deps_rules


def rules():
return [*unowned_deps_rules()]
38 changes: 38 additions & 0 deletions pants-plugins/internal_plugins/unowned_dependencies/rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from dataclasses import dataclass

from pants.backend.python.target_types import PythonSourcesGeneratingSourcesField
from pants.engine.internals.graph import Owners, OwnersRequest
from pants.engine.internals.selectors import Get
from pants.engine.rules import rule, collect_rules
from pants.engine.target import InferDependenciesRequest, InferredDependencies, FieldSet, MultipleSourcesField
from pants.engine.unions import UnionRule

import logging

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class SpecialDependenciesInferenceFieldSet(FieldSet):
required_fields = (PythonSourcesGeneratingSourcesField,)
sources: MultipleSourcesField


class InferSpecialDependenciesRequest(InferDependenciesRequest):
infer_from = SpecialDependenciesInferenceFieldSet


@rule
async def infer_special_dependencies(request: InferSpecialDependenciesRequest) -> InferredDependencies:
logger.warning(request)
owners = await Get(Owners, OwnersRequest(("helloworld/translator/foobar.py",)))
logger.warning(owners)
return InferredDependencies(include=owners)


def rules():
return [
*collect_rules(),
UnionRule(InferDependenciesRequest, InferSpecialDependenciesRequest),
]

7 changes: 6 additions & 1 deletion pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

[GLOBAL]
pants_version = "2.14.0"
pythonpath = ["%(buildroot)s/pants-plugins"]
backend_packages.add = [
"pants.backend.build_files.fmt.black",
"pants.backend.python",
Expand All @@ -11,6 +12,7 @@ backend_packages.add = [
"pants.backend.python.lint.flake8",
"pants.backend.python.lint.isort",
"pants.backend.python.typecheck.mypy",
"internal_plugins.unowned_dependencies",
]

# Pants' sponsor, Toolchain, offers remote caching, which can improve your CI performance with minimal configuration.
Expand All @@ -37,7 +39,7 @@ repo_id = "3B1D361B-E9F1-49A8-B761-03DCC41FD58E"

[source]
# The Python source root is the repo root. See https://www.pantsbuild.org/docs/source-roots.
root_patterns = ["/"]
root_patterns = ["/", "/pants-plugins"]

[python]
# The default interpreter constraints for code in this repo. Individual targets can override
Expand All @@ -62,3 +64,6 @@ resolves = { python-default = "python-default.lock"}
# problematic system Pythons. See
# https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path.
search_path = ["<PATH>", "<PYENV>"]

[python-infer]
unowned_dependency_behavior = "error"