cold-soccer-63228
04/28/2023, 7:52 PM./pants --changed-since=origin/master dependees
. The error I’m seeing is the following.
AttributeError: type object 'PythonSourceField' has no attribute 'is_applicable'
Happy to provide code snippets or other additional detailsfast-nail-55400
04/28/2023, 8:03 PMcold-soccer-63228
04/28/2023, 8:04 PMfast-nail-55400
04/28/2023, 8:04 PMis_applicable
is a method on FieldSet
)cold-soccer-63228
04/28/2023, 8:04 PMThen for eachMy code looks as follows., theInferDependenciesRequest
class variable should now point to a relevantinfer_from
subclass type.FieldSet
class MyInferDependenciesRequest(InferDependenciesRequest):
infer_from = PythonSourceField
What should the new infer_from
point to?pants.backend.python.goals.run_python_source.PythonSourceFieldSet
?AttributeError: 'MyInferDependenciesRequest' object has no attribute 'sources_field
fast-nail-55400
04/29/2023, 9:05 PMinfer_from
should be a subclass of FieldSet
with whatever fields you need to access on the target in order to implement your dependency inference rule.cold-soccer-63228
04/29/2023, 9:06 PMpants.backend.python.goals.run_python_source.PythonSourceFieldSet
fast-nail-55400
04/29/2023, 9:08 PMcold-soccer-63228
04/30/2023, 12:28 AMfrom pathlib import PurePath
from typing import Iterable
from pants.backend.python.target_types import PythonSourceField
from pants.engine.rules import Get, Rule, rule
from pants.engine.target import AllTargets, AllTargetsRequest, InferDependenciesRequest, InferredDependencies
from pants.engine.unions import UnionRule
from front_porch.modules.package_management.constants import PACKAGE_CONFIG_FILENAME, PACKAGE_CONFIG_UTIL_PATH
class InferPackageConfigDependenciesRequest(InferDependenciesRequest):
infer_from = PythonSourceField # noqa
@rule(desc="Inferring Python package config dependencies by analyzing source")
async def infer_python_package_config_dependencies(
request: InferPackageConfigDependenciesRequest,
) -> InferredDependencies:
"""
Infers dependencies on all `_package_config.py` targets for the Python module located at
`PACKAGE_CONFIG_UTIL_PATH`.
"""
if request.sources_field.file_path != PACKAGE_CONFIG_UTIL_PATH:
return InferredDependencies([])
package_config_targets = set()
all_targets = await Get(AllTargets, AllTargetsRequest())
for target in all_targets:
if (
target.has_field(PythonSourceField)
and PurePath(target[PythonSourceField].file_path).name == PACKAGE_CONFIG_FILENAME
):
package_config_targets.add(target)
if len(package_config_targets) == 0:
return InferredDependencies([])
return InferredDependencies(target.address for target in package_config_targets)
def rules() -> Iterable[Rule]:
return [
infer_python_package_config_dependencies,
UnionRule(InferDependenciesRequest, InferPackageConfigDependenciesRequest),
]
PythonSourceField
needs to be changed to the corresponding FieldSet
, I’m not sure what to use, exactly. I found PythonSourceFieldSet
arbitrarily in the source code, but that was only by randomly searching for things that looked similar. Since it’s under the run goal, it feels like it’s not exactly right here (?)PythonSourceField
with PythonSourceFieldSet
, and the full output I received was only the following.
If set to true, removing the option will cause directory arguments like `./pants test project/dir` to now match all files and targets in the directory, whereas before it matched the target `project/dir:dir`. To keep the old semantics, use the explicit address syntax.
17:54:17.02 [INFO] Initializing scheduler...
17:54:17.21 [INFO] Scheduler initialized.
17:54:31.09 [ERROR] 1 Exception encountered:
AttributeError: 'InferPackageConfigDependenciesRequest' object has no attribute 'sources_field'
fast-nail-55400
05/02/2023, 5:00 PMif request.sources_field.file_path != PACKAGE_CONFIG_UTIL_PATH:
return InferredDependencies([])
s/sources_field/field_set/request.field_set
for the particular field you needcold-soccer-63228
05/04/2023, 3:23 PMAttributeError: 'PythonSourceFieldSet' object has no attribute 'file_path'
from typing import Iterable
from pants.backend.python.goals.run_python_source import PythonSourceFieldSet
from pants.engine.rules import Rule, rule
from pants.engine.target import InferDependenciesRequest, InferredDependencies
from pants.engine.unions import UnionRule
class InferMyDependenciesRequest(InferDependenciesRequest):
infer_from = PythonSourceFieldSet # noqa
@rule(desc="...")
async def infer_my_dependencies(
request: InferMyDependenciesRequest,
) -> InferredDependencies:
return InferredDependencies([])
def rules() -> Iterable[Rule]:
return [
infer_my_dependencies,
UnionRule(InferDependenciesRequest, InferMyDependenciesRequest),
]
fast-nail-55400
05/05/2023, 9:15 PMcold-soccer-63228
05/15/2023, 3:41 PMpants.backend.python.goals.run_python_source.PythonSourceFieldSet
wasn’t the correct one to be using. I should’ve been using pants.backend.python.dependency_inference.rules.PythonImportDependenciesInferenceFieldSet