bitter-ability-32190
08/25/2022, 2:22 PMSubclassed union bases should inherit the superclass's union members.but...
@union
class Base: ...
@union
class Sub(Base): ...
class BaseInput(Base): ...
class SubInput(Sub): ...
class Result: ...
# ...
UnionRule(Sub, SubInput)
UnionRule(Base, BaseInput)
# ...
await Get(Result, Sub(...))
is complaining:
No installed rules return the type Result, and it was not provided by potential callers of Query(Result for BaseInput).
Sub
I see <class BaseInput>
.hundreds-father-404
08/25/2022, 2:34 PMbitter-ability-32190
08/25/2022, 2:34 PMUnion subclasses of union base classes should inherit the base class's members
hundreds-father-404
08/25/2022, 2:43 PMbitter-ability-32190
08/25/2022, 2:43 PMfor sub_union in union_base.__subclasses__():
...
should be:
for sub_union in union_base.__subclasses__():
if is_union(sub_union):
...
The result right now is the union membership has a lot of mapping from our union subclasses (E.g. BlackRequest
) back to the parent (e.g. FmtTargetsRequest
) members (so like every formatter...)print(union_membership[BlackRequest])
09:56:48.25 [INFO] stdout: "FrozenOrderedSet([<class 'pants.backend.python.lint.autoflake.rules.AutoflakeRequest'>, <class 'pants.backend.python.lint.black.rules.BlackRequest'>, <class 'pants.backend.python.lint.docformatter.rules.DocformatterRequest'>, <class 'pants.backend.python.lint.isort.rules.IsortRequest'>, <class 'pants.backend.shell.lint.shfmt.rules.ShfmtRequest'>, <class 'pants.backend.go.lint.gofmt.rules.GofmtRequest'>, <class 'pants.backend.java.lint.google_java_format.rules.GoogleJavaFormatRequest'>, <class 'pants.backend.scala.lint.scalafmt.rules.ScalafmtRequest'>])"
womp womp, lol$ git diff main
diff --git a/src/python/pants/engine/unions_test.py b/src/python/pants/engine/unions_test.py
index f302f180e..40b7d7f32 100644
--- a/src/python/pants/engine/unions_test.py
+++ b/src/python/pants/engine/unions_test.py
@@ -10,10 +10,10 @@ def test_union_membership_from_rules() -> None:
class Base:
pass
- class A:
+ class A(Base):
pass
- class B:
+ class B(Base):
pass
assert UnionMembership.from_rules([UnionRule(Base, A), UnionRule(Base, B)]) == UnionMembership(
A
to B
and vice-versa.
Proof of fix:
$ git diff main
diff --git a/src/python/pants/engine/unions.py b/src/python/pants/engine/unions.py
index 13dd10c75..47389c956 100644
--- a/src/python/pants/engine/unions.py
+++ b/src/python/pants/engine/unions.py
@@ -77,9 +77,10 @@ class UnionMembership:
while len(bases) > 0:
union_base = bases.pop()
for sub_union in union_base.__subclasses__():
- if sub_union not in mapping:
- bases.append(sub_union)
- mapping[sub_union].update(mapping[union_base])
+ if is_union(sub_union):
+ if sub_union not in mapping:
+ bases.append(sub_union)
+ mapping[sub_union].update(mapping[union_base])
return cls(mapping)
def __init__(self, union_rules: Mapping[type, Iterable[type]]) -> None:
Now the test passes ๐unions_test
is waaaaaaaaaaaaaaaaaaaaaaaaay too small for the complexityOtherwise union.get(SomeTarget.PluginFields) wouldn't give you anything when you register a plugin field on Target.
target.py
with the new behavior I thinkcurved-television-6568
08/25/2022, 4:10 PMyou implemented the code (and didnโt update tests, tsk tsk)waddaโya mean? I see I didnโt add any unit tests for the union directly, but the change is tested indirectly by the target tests.. ๐
bitter-ability-32190
08/25/2022, 4:14 PMunion_membership
mapping non-union subclasses ๐curved-television-6568
08/25/2022, 4:23 PMbitter-ability-32190
08/25/2022, 4:23 PM