Is the rule graph subclass aware? I'm thinking abo...
# plugins
p
Is the rule graph subclass aware? I'm thinking about this class hierarchy: • PackageFieldSet • NfpmPackageFieldSet • NfpmRpmPackageFieldSet With rules: • UnionRule(PackageFieldSet, NfpmRpmPackageFieldSet) • nfpm_package(NfpmPackageFieldSet) -> BuiltArtifact I can't tell what's missing from the role graph errors I'm seeing, but I suspect the rule graph does not treat NfpmRpmPackageFieldSet as compatible with NfpmPackageFieldSet, so it can't find the last rule given the subclass. Is that right?
1
f
> Is the rule graph subclass aware? It is not.
👍 1
c
I'm not too sure of it. But I think it isn't, and that `UnionRule`s act as a rule that allows you to move between the parent class and the child class. To illustrate,
Get(InferredDependencies, InferDependenciesRequest)
might go
InferDependenciesRequest--[UnionRule]-->InferFortranDependencies--[infer_fortran_dependencies]-->InferredDependencies
👍 1
f
The rule engine treats all types as distinct regardless of subclass relationship. You can use subclassing for code sharing but the engine won't consider the types as compatible for purposes of finding rules to apply.
@careful-address-89803 is correct.
UnionRule
rules exist to handle situations where you might need multiple dispatch type operations. However, it does not involve subclassing. Implementations of
InferDependenciesRequest
for example have an associated
FieldSet
which is used to find the applicable targets for each implementation. https://github.com/pantsbuild/pants/blob/992b233deb0dd95519051f04faf223c9fdc7b890/src/python/pants/engine/target.py#L2788
And the core Pants rules will loop over the implementations (available from the
UnionMembership
type and ask the engine to apply the relevant rules).
p
Thank you! That clears up why the rule wasn't working.
h
But shouldn't you be using call-by-name anyway for new rules? 🙂
p
@happy-kitchen-89482 well, now I am using call-by-name. I ran migrate-call-by-name, fmt, and then fixed the issues mypy found. https://github.com/pantsbuild/pants/pull/19308/files/2bee818cbe5b81eb44d008530fb34f4d5e3aeb2f..3f9b63826d1fcc3060cafd876ba8989f359b5789
🎉 1