refactor
refactor copied to clipboard
Specifying rules with strings
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),
]
)