I tried to be a good boy scout and refactor some d...
# plugins
b
I tried to be a good boy scout and refactor some duplicated code in
pants.backend.python.target_types_rules
into a helper method, but then Pants gets upset
Copy code
Exception: pants.backend.python.target_types_rules:134:generate_targets_from_python_tests() returned a result value that did not satisfy its constraints: <coroutine object _generate_targets_helper at 0x7f84820224c0>
I believe I got the annotations right
c
Do you use `Get`s outside of the
async def
rule?
That’s a gotcha that it isn’t supported… due to pants having a rudimentary parser going through all rule bodies to pick up the rule dependencies from
Get
and
MultiGet
invocations.
b
Oops, rookie mistake. I forgot to
await
my helper 😮
👍 1
(I'm new to async code :D)
c
Why is the helper async?
b
I assumed only
async
functions could
await
things?
c
That’s true.. but from the limitation mentioned above, your helper will not have to await anything…
b
But it sounds like I can't use a helper because the rudimentary parser isn't smart enough to follow along and see the
Get
s int he helper?
c
Indeed, so helper methods are limited to work on what they get, without invoking other rules.
b
Bummer
c
Agree.
b
A coroutine could maybe work, but I'm not sure it'd result in the most readable code 🤔
c
what are you trying to break out?
b
I'm adding a 4th
generate_targets_from_*
function to
src/python/pants/backend/python/target_types_rules.py
. It has slightly different way of collecting sources, so I thought using a helper would not only clean up some duplication but allow me to highlight the difference (that way whoever adds the 5th one doesn't copy mine)
c
Ah 🙂
The helper in that case would be a rule, with a dedicated request intput type, that could hold the different types etc used to collect stuff, and return the sources and overrides.
I.e. turn your helper into a rule.
As you say, they are doing pretty much the same thing, but with various types of source fields etc.
b
I'll probably flounder around trying to puzzle out how to do that with all the different types involved. I think I'll wait until my code works... duplicated, and then ask for some coaching
👍 1
h
If your helper needs to
await Get
then it needs to be an async
@rule
and you need to
await Get
it, yes.
☑️ 1