-
Notifications
You must be signed in to change notification settings - Fork 65
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
Allow glob patterns instead of parent dirs for matching configs #156
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are lint errors.
ae2c12e
to
2651106
Compare
The CI builds has a test case failure. It looks like a bug in the new version of LibCST related to the new Assignment/Access changes. CC @zsol It changes |
2651106
to
06cd4ac
Compare
@Kronuz there is a type check error regarding |
Codecov Report
@@ Coverage Diff @@
## master #156 +/- ##
==========================================
+ Coverage 84.71% 84.80% +0.08%
==========================================
Files 86 86
Lines 3532 3566 +34
==========================================
+ Hits 2992 3024 +32
- Misses 540 542 +2
Continue to review full report at Codecov.
|
6f6b652
to
d413093
Compare
fixit/rules/import_constraints.py
Outdated
class Action(Enum): | ||
ALLOW = "allow" # allow everywhere | ||
DENY = "deny" # deny everywhere | ||
ALLOW_GLOBAL = "allow_global" # allow in global scope | ||
ALLOW_LOCAL = "allow_local" # allow in local scopes | ||
DENY_GLOBAL = "deny_global" # deny in global scope | ||
DENY_LOCAL = "deny_local" # deny in local scopes | ||
|
||
|
||
ACTIONS_MAP: Dict[str, Action] = { | ||
"allow": Action.ALLOW, | ||
"deny": Action.DENY, | ||
"allow global": Action.ALLOW_GLOBAL, | ||
"allow local": Action.ALLOW_LOCAL, | ||
"deny global": Action.DENY_GLOBAL, | ||
"deny local": Action.DENY_LOCAL, | ||
# Aliases: | ||
"allow globally": Action.ALLOW_GLOBAL, | ||
"allow locally": Action.ALLOW_LOCAL, | ||
"deny globally": Action.DENY_GLOBAL, | ||
"deny locally": Action.DENY_LOCAL, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACTIONS_MAP
looks duplicated.
To check a string is a valid Enum class attribute, just do action in Action.__members__
.
To get the enum value from a string, simply do Action[action]
class Action(Enum): | |
ALLOW = "allow" # allow everywhere | |
DENY = "deny" # deny everywhere | |
ALLOW_GLOBAL = "allow_global" # allow in global scope | |
ALLOW_LOCAL = "allow_local" # allow in local scopes | |
DENY_GLOBAL = "deny_global" # deny in global scope | |
DENY_LOCAL = "deny_local" # deny in local scopes | |
ACTIONS_MAP: Dict[str, Action] = { | |
"allow": Action.ALLOW, | |
"deny": Action.DENY, | |
"allow global": Action.ALLOW_GLOBAL, | |
"allow local": Action.ALLOW_LOCAL, | |
"deny global": Action.DENY_GLOBAL, | |
"deny local": Action.DENY_LOCAL, | |
# Aliases: | |
"allow globally": Action.ALLOW_GLOBAL, | |
"allow locally": Action.ALLOW_LOCAL, | |
"deny globally": Action.DENY_GLOBAL, | |
"deny locally": Action.DENY_LOCAL, | |
} | |
class Action(Enum): | |
ALLOW = "allow" # allow everywhere | |
DENY = "deny" # deny everywhere | |
ALLOW_GLOBAL = "allow_global" # allow in global scope | |
ALLOW_LOCAL = "allow_local" # allow in local scopes | |
DENY_GLOBAL = "deny_global" # deny in global scope | |
DENY_LOCAL = "deny_local" # deny in local scopes | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the idea behind the map was to also allow other aliases (such as "allow globally", "allow global", and "allow_global"). But yes, we can use the simple Enum, if we don't want that support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not supporting different alias doesn't seem to be a big issue. It can also keep the config more consistent.
fixit/rules/import_constraints.py
Outdated
if action not in ACTIONS_MAP: | ||
raise ValueError( | ||
"A rule should either allow or deny a pattern (possibly globally or locally)" | ||
) | ||
return _ImportRule(module, ACTIONS_MAP[action]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if action not in ACTIONS_MAP: | |
raise ValueError( | |
"A rule should either allow or deny a pattern (possibly globally or locally)" | |
) | |
return _ImportRule(module, ACTIONS_MAP[action]) | |
if action not in Action.__members__: | |
raise ValueError( | |
f"A rule should have a valid action ({list(Action.__members__)})" | |
) | |
return _ImportRule(module, Action[action]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could make this whole block even simpler if we don't want that map for aliases, albeit with a more generic error (ValueError: 'invalid_action' is not a valid RuleAction
):
return _ImportRule(module, Action(action))
fixit/common/testing.py
Outdated
if test_case.message is not None and test_case.message != report.message: | ||
raise AssertionError( | ||
f"Expected message:\n {test_case.message}\nBut got:\n {report.message}" | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message in InvalidTestCase is only used in one of the new test case. Do we really need to add this new field?
Ideally, the test case example code should explain itself clearly. The example code can also have some comments to explain it better. I'd suggest not to add InvalidTestCase.message to keep it simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to test the custom messages are working, not only show they exist. This allows testing custom messages and it doesn't add much complexity, only a new field that can be tested. we could call the field "expected_message
" though. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, expected_message
is a better name. I can see it can be useful for rules provide custom messages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
When releasing this, we need to update existing config file to avoid the new RuleAction value error.
What do you mean? |
I read it wrong, so there won't be value error since we keep the lower case allow/deny. We can review existing rule to see if we need to refine them. |
Summary
Allow using globs as configuration for rules, not only parent directories.
This allows for creating specific rules for specific files, not only directories. For example:
Will allow specifying rules for all
urls
modules anywhere.This also allows specifying if the allow or deny rule are meant for global or local scopes only:
Allowing to import
common.*
anywhere and everything else only inside functions, as inner imports.Test Plan
Run tests