Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support overriding param types for rule code #16929

Merged
merged 4 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/python/pants/engine/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _ensure_type_annotation(
return type_annotation


PUBLIC_RULE_DECORATOR_ARGUMENTS = {"canonical_name", "desc", "level"}
PUBLIC_RULE_DECORATOR_ARGUMENTS = {"canonical_name", "desc", "level", "param_type_overrides"}
# We don't want @rule-writers to use 'rule_type' or 'cacheable' as kwargs directly,
# but rather set them implicitly based on the rule annotation.
# So we leave it out of PUBLIC_RULE_DECORATOR_ARGUMENTS.
Expand Down Expand Up @@ -183,6 +183,7 @@ def rule_decorator(func, **kwargs) -> Callable:

rule_type: RuleType = kwargs["rule_type"]
cacheable: bool = kwargs["cacheable"]
param_type_overrides: dict[str, type] = kwargs.get("param_type_overrides", {})

func_id = f"@rule {func.__module__}:{func.__name__}"
type_hints = get_type_hints(func)
Expand All @@ -191,13 +192,21 @@ def rule_decorator(func, **kwargs) -> Callable:
name=f"{func_id} return",
raise_type=MissingReturnTypeAnnotation,
)

func_params = inspect.signature(func).parameters
for parameter in param_type_overrides:
if parameter not in func_params:
raise ValueError(f"Unknown parameter name in `param_type_overrides`: {parameter}")
thejcannon marked this conversation as resolved.
Show resolved Hide resolved

parameter_types = tuple(
_ensure_type_annotation(
param_type_overrides[parameter]
if parameter in param_type_overrides
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this bypasses the type check performed by _ensure_type_annotation, maybe check that in the previous for loop?

for parameter, value in param_type_overrides.items():
  ...

else _ensure_type_annotation(
thejcannon marked this conversation as resolved.
Show resolved Hide resolved
type_annotation=type_hints.get(parameter),
name=f"{func_id} parameter {parameter}",
raise_type=MissingParameterTypeAnnotation,
)
for parameter in inspect.signature(func).parameters
for parameter in func_params
)
is_goal_cls = issubclass(return_type, Goal)

Expand Down
16 changes: 16 additions & 0 deletions src/python/pants/engine/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,22 @@ async def dup_a() -> B: # noqa: F811
return B()


def test_param_type_overrides() -> None:
type1 = int # use a runtime type

@rule(param_type_overrides={"param1": type1, "param2": dict})
async def dont_injure_humans(param1: str, param2, param3: list) -> A:
return A()

assert dont_injure_humans.rule.input_selectors == (int, dict, list)

with pytest.raises(ValueError, match="paramX"):

@rule(param_type_overrides={"paramX": int})
async def obey_human_orders() -> A:
return A()


def test_invalid_rule_helper_name() -> None:
with pytest.raises(ValueError, match="must be private"):

Expand Down