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

Specifying rules with strings #85

Open
MementoRC opened this issue Jan 13, 2023 · 0 comments
Open

Specifying rules with strings #85

MementoRC opened this issue Jan 13, 2023 · 0 comments

Comments

@MementoRC
Copy link
Contributor

MementoRC commented Jan 13, 2023

While using RuleCollection, a case of recursive import was annoying and it seemed interesting to be able to define the rules as strings and add a rudimentary lookup for class definitions within the same module or import of pascal_to_snake modules in sub-directories of the module defining the RuleCollection

While working on this, it felt interesting (though, of unforeseeable necessity/use case) to infer the calling frame of the Session and do the lookup in the calling module

Is this something that would be interesting?

@pytest.mark.parametrize(
    "source, expected, rules",
    [
        ("1+1", "1 - 1", "PlusToMinusRule"),
        ("1+1", "1+1", "Plus_MinusRule"),
    ],
)

First case : Find the class definition and applies the PlusToMinusRule
Second case: Typo in the string, ignore the non-existing rule

With RuleCollection:

class CollectPlusToMinusMultToMinusRuleWithString(RuleCollection):
    rules: ClassVar = ["PlusToMinusRule", MultToMinusRule, ]


class CollectPlus_MinusMultToMinusRuleWithString(RuleCollection):
    rules: ClassVar = ["Plus_MinusRule", MultToMinusRule, ]


@pytest.mark.parametrize(
    "source, expected, rules",
    [
        ("1+1*2", "1 - 1 - 2", CollectPlusToMinusMultToMinusRuleWithString),
        ("1+1*2", "1+1 - 2", CollectPlus_MinusMultToMinusRuleWithString),
    ]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant