https://pantsbuild.org/ logo
#development
Title
# development
c

careful-address-89803

11/17/2022, 4:19 AM
hey folks, I'm working on helpers to generate the lockfile rules. For testing, I'd like to set a query rule for the lockfile sentinel (eg
Flake8LockfileSentinel
). Problem is, that class now exists only at runtime. Is there a simple way to go from the tool (
class Flake8(PythonToolBase):
) to whatever the concrete class of the UnionRule is ( whatever was used as the 2nd argument in`UnionRule(GenerateToolLockfileSentinel, Flake8LockfileSentinel)`) ? I could fish it out of the generated rules, but that doesn't seem like something Pants wants anyone to do.
c

curved-television-6568

11/17/2022, 2:18 PM
I guess this information exists in the
UnionMembership
? so perhaps something like:
Copy code
@rule
async def get_concrete_class(union_membership: UnionMembership) -> x:
  for member_cls in union_membership.get(GenerateToolLockfileSentinel):
    if is_what_i_want(member_cls):
      ...
would need some common base class or other to identify the concrete class with among all the registered sentinel classes I guess..
c

careful-address-89803

11/17/2022, 2:32 PM
Yeah, I think that would work. In small tests there's probably only one class in the union membership, and I think there might be a options_scope parameter for that sort of unionmembership
oh hm, seems this doesn't quite work. I need the class before I create the rule runner so I can use it in a QueryRule like
QueryRule(GeneratePythonLockfile, [lockfile_cls])
. Is there a better way to formulate the QueryRule?
c

curved-television-6568

11/20/2022, 11:22 PM
do you have a link to the code for this? I guess you want to make the dynamic class memoized per class instance, then you can pull the exact same class for your test that is going to be registered in the union rule. c.f. https://github.com/pantsbuild/pants/blob/eee5863b85a995c25d210ecee57b93d2dfaedbc3/src/python/pants/engine/target.py#L479-L481
c

careful-address-89803

11/20/2022, 11:28 PM
I'll look into that, thanks. Here's the method I have currently link, and here's how I use it link
c

curved-television-6568

11/20/2022, 11:48 PM
and where is the lockfile sentinel generated?
looked at the one for Pylint, but it didn’t seem generated to me… so I’m likely missing something 😛
c

careful-address-89803

11/20/2022, 11:53 PM
Invocation of generation here, definition of generation resolves to here
c

curved-television-6568

11/21/2022, 12:04 AM
Ah.. right. Buried deep 😛 Fishing the class out from the union rule in the rules graph seems fine to me for testing purposes..
👍 1