Thinking about a change to `@rule` that allows you...
# plugins
b
Thinking about a change to
@rule
that allows you to specify the param types in the decorator and it ignores the annotation. Use-case is "inner" rules:
Copy code
def rule_maker(a):
    @rule(param_type_override={"foo": a.sometype})
    def my_rule(foo: Foo) -> ...:
        ...
1
w
Chicanery!
b
Let me edit to be more realistic
Ok edited. Also SJ this is for boilerplate-reduction of the new fmt/lint 😂
w
.... Anti-chicanery!
How does
mypy
(or
pyright
or whatever) treat this?
b
Just use the provided annotation. 🤷‍♂️
Pants uses the type annotation of the param as a convenient way to get graph info. Nothing says that has to be where pants gets it from 🙂
w
Right, but would
mypy
or whatever end up using the override? Or would it still think
Foo
is the type?
I thought
mypy
had weird behaviour when it came to this, I think it was even in the FAQ (or that might have been
mypyc
Ah okay, I'm thinking about this specifically: https://mypy.readthedocs.io/en/stable/common_issues.html#narrowing-and-inner-functions So, not quite the same
b
Yeah in this case, mypy only sees the annotation. The rule argument is for pants
👍 1
c
I know it says that it's not meant for direct consumption, but what about instantiating the TaskRule directly? There are several levels of introspecting decorators that this would need to be plumbed through, it might just be easier to have a
Copy code
def rule_maker(a)
    def my_rule(foo: Foo) -> Tout:
        ...

    return = TaskRule(
        output_type=Tout,
        input_selectors=[a.sometype],
        func=my_rule,
        ...
    )
I guess
_make_rule
already does much of that. Pros: exists; Cons: no helpful validation
although actually it might be possible to insert it just within rule_decorator and have it introspect there only if it hasn't had args passed in (or warn if it sees something that obviously won't work, like the wrong number of arguments)
b
Yeah that was my plan
Oh interesting. this works unless you have
from __future__ import annotations
.
I'm prone to punt
h
Pants uses the type annotation of the param as a convenient way to get graph info. Nothing says that has to be where pants gets it from
FYI we used to have a much more verbose syntax in the early days of v2. I've forgotten what it was lol. Using type hints was a huge simplification, but you are correct that it need not come from there always
b
Thats my point. We can augment it 😛 (Although now its a moot point because I know how to workaround it)
(although my workarounds include using invalid type hints 😕)
So maybe we do want this change after all 🤔