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

πŸ§‘πŸΌβ€πŸ’» [feat/0.8] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) #592

Open
wants to merge 4 commits into
base: 0.8
Choose a base branch
from

Conversation

Synss
Copy link
Collaborator

@Synss Synss commented Nov 1, 2024

Issue #562

ArthurD1
ArthurD1 previously approved these changes Nov 3, 2024
Copy link
Collaborator

@ArthurD1 ArthurD1 left a comment

Choose a reason for hiding this comment

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

This seems to break when harp is run with --enable rules and no rules are configured.

@ArthurD1 ArthurD1 requested a review from hartym November 3, 2024 15:27
@ArthurD1 ArthurD1 dismissed their stale review November 3, 2024 15:38

Having no rules configured might trigger an error

Copy link
Collaborator

@ArthurD1 ArthurD1 left a comment

Choose a reason for hiding this comment

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

This seems to break when harp is run with --enable rules and no rules are configured.

It breaks with TypeError: 'NoneType' object is not iterable

@Synss
Copy link
Collaborator Author

Synss commented Nov 5, 2024

I’ll have a look.

@hartym hartym changed the title Handle rule defaults in match_level πŸ‘€ [FEATURE] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) Nov 6, 2024
@hartym hartym changed the base branch from 0.7 to 0.8 November 6, 2024 09:51
@hartym hartym changed the title πŸ‘€ [FEATURE] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) πŸ‘€ [feat/0.8] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) Nov 7, 2024
@Synss Synss requested a review from ArthurD1 November 9, 2024 17:59
@Synss
Copy link
Collaborator Author

Synss commented Nov 9, 2024

I've just added a follow-up commit that fixes this issue by adding a simple guard clause upon entering the function. The rules matching will now be skipped entirely when no rules are configured.

I've tried the patch with

python -m harp server --enable rules

from the repo.

@hartym hartym removed the request for review from ArthurD1 November 10, 2024 07:24
Copy link
Contributor

@hartym hartym left a comment

Choose a reason for hiding this comment

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

We're on a good path, but not quite there yet.

I pushed some fixes related to comments I made, especially the fact that the changes to match item was now skipping matches from different subtrees (because it now used return instead of yield from.

I also added some tests for the potential issue we discussed at pycon, and I have a good and a bad news: the bug discussed does not and did not exist: the harp_apps.rules.models.rulesets.BaseRuleSet.match method "flattens" the rules, level by level, and all subtrees are considered. The bad news is, the new default behaviour is having hard time there, because it means that if we have two subtrees with default rules, having a nondefault rule match in the first subtree will make the second subtree default not matching, which is not what I believe we want.

For example:

a:
  foo: ...
b:
  __default__: ...

If "the "foo" rule match, then default will never be considered.

I added a failing test for this, and I guess we need to address that before we can merge. Another thing needed before it can be merged is some bits of documentation: one should be able to find references to default in the docs, even if it's only a few sentences + examples here and there (no need to overdo it). An entry should also be added to the docs/changelogs/unreleased.rst file.

Comment on lines 24 to 37
def _match_level(rules, against):
default = None
has_match = False
for pattern, rules in rules:
if pattern == DEFAULT_RULE_MATCHER:
default = rules
continue

if pattern.match(against):
if hasattr(rules, "items"):
yield from rules.items()
else:
yield from rules
has_match = True
return Itemize(rules)

if not has_match and default:
return Itemize(default)
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I understand, this changes the behaviour of rules, allowing only one match to be made amongst one level (by returning instead of yielding from). The Β«itemizeΒ» object brings more confusion than clarity, on top of that.

the current test suite is a bit incomplete on the rules side, but catches an issue there (see harp_apps.rules.tests.test_models_rulesets.test_multilevel).

Comment on lines 87 to 88
if not self.rules:
return
Copy link
Contributor

Choose a reason for hiding this comment

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

Is that related to the current patch or a preexisting bug ? Asking because I want to make sure I understand what have caused the change in behaviour if it worked previously.

Anyway, can't do real harm to check, but probably the empty rules should be iterable anyway (empty iterator). More a note for myself here than anything else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This guards against the TypeError: 'NoneType' object is not iterable when no rule is configured. This was reported above.

@hartym hartym changed the title πŸ‘€ [feat/0.8] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) πŸ§‘πŸΌβ€πŸ’» [feat/0.8] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) Nov 10, 2024
@Synss
Copy link
Collaborator Author

Synss commented Nov 10, 2024

Thank you for the detailed feedback! This is really appreciated.

The feature indeed seems more involved than I first thought. I'm still interested in working on this but I must let you know that I'm really busy this month. I apologize if this causes any inconvenience.

@msqd msqd deleted a comment from allcontributors bot Nov 16, 2024
@hartym
Copy link
Contributor

hartym commented Nov 16, 2024

The feature indeed seems more involved than I first thought. I'm still interested in working on this but I must let you know that I'm really busy this month. I apologize if this causes any inconvenience.

No worries, take your time, your help is also really appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants