-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Update disallowed-name to flag module-level variables #7808
Update disallowed-name to flag module-level variables #7808
Conversation
I'm going to also point out a couple interesting examples from functional tests that don't seem to be flagged correctly. In this first example, why would only Alias5 and Alias6 be flagged, and not all?
in this example, the first is a constant, so it's good, but the second
|
@@ -27,7 +27,7 @@ class Dummy: | |||
"""A class defined in this module.""" | |||
pass | |||
|
|||
DUMMY = Dummy() | |||
dummy = Dummy() |
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 don't really understand what you're changing in this PR, but this change is incorrect.
DUMMY
is a "constant" in pylint
/Python
terms and should be uppercased.
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.
So full disclosure I was very hesitant with these code changes (the first commit is the only important one) bc I was surprised pylint wasn't doing this initially, but then I posted test cases in the issue and was confirmed we needed this change #3701 (comment)
My interpretation of PEP8 regarding module-level constants is what I posted on the PR description:
- if it's a module level CONSTANT, that is, a true constant such as assignment to str, int, or tuple (?), then it's CAPITAL, so
CONSTANT = 1
- if it's a module level variable, that is, assignment to something like a list, a class instance, then it should follow same rules as all other variables, that is, lower case snake... so
dummy = Dummy()
would be correct according to this.
What do you think? Is my interpretation wrong? If so, how to I interpret the test case suggestions in the issue?
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.
DUMMY
is a "constant" inpylint
/Python
terms and should be uppercased.
I agree with Daniel here. PEP 8 never differentiates between a constants and (true) constants. The pylint interpretation has been "consider every variable at module level a constant" for a long time. Changing it now would result in a massive breaking change that's just unreasonable.
Just an example. The change would result in over 7500+ invalid-name
errors for Home-Assistant.
I'm strong -1.
This comment has been minimized.
This comment has been minimized.
@@ -27,7 +27,7 @@ class Dummy: | |||
"""A class defined in this module.""" | |||
pass | |||
|
|||
DUMMY = Dummy() | |||
dummy = Dummy() |
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.
DUMMY
is a "constant" inpylint
/Python
terms and should be uppercased.
I agree with Daniel here. PEP 8 never differentiates between a constants and (true) constants. The pylint interpretation has been "consider every variable at module level a constant" for a long time. Changing it now would result in a massive breaking change that's just unreasonable.
Just an example. The change would result in over 7500+ invalid-name
errors for Home-Assistant.
I'm strong -1.
Counter argument to not doing anything would be that What we can't do do is make it even more annoying by increasing the constraints (7500+ new messages in a well maintained code base is just nuts). There's two way forward on this particular problem imo:
I think it's clear what I would favor here π |
I would disagree. In my personal experience it works fine as it is. Clearly not everybody feels that way π but that might rather be a thing that could be solved by config profiles. There was a small discussion about it in #3512.
I would prefer the first option. For me it isn't so much weather or not it's a true constant but rather to know if a variable is defined at module level. Having the distinction between upper case and lower case does help with that. Additionally, it adds a common style that, even if not perfect, everybody (who uses it regularly) understands. Relaxing the requirement will just lead to mixed cases which will require additional attention from reviewers. Especially for beginners, without experience in other languages, it won't be obvious when to use what and I suspect they will frequently use the wrong one. |
The issue is that there is no consensus on what the style should be for module level variable, pep8 is lax about it:
The solution I would propose is to permit only consistent uppercase or consistent snake case, (or dunder casing, which is consistent snake case) but not anything else. We could add an option deactivated by default for that. The discussion about this is shattered in a dozen issues but I thought this was consensual. |
We don't do that. The difference is based on the scope a variable is defined in, e.g.
That's already possible with naming styles. https://pylint.pycqa.org/en/latest/user_guide/messages/convention/invalid-name.html#predefined-naming-styles For example, this should work # pylintrc
const-naming-style=snake_case
variable-naming-style=snake_case |
See #7808 (comment)
Well if you do |
4763522
to
0e131a4
Compare
I've now updated this PR to only modify disallowed-name, not invalid-name. |
if match is None and not _should_exempt_from_invalid_name(node): | ||
if ( | ||
match is None | ||
and not disallowed_check_only |
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.
This could've been done in other ways, too, including integrating with _should_exempt_from_invalid_name
above but I chose this for simplicity. Lmk if another implementation makes more sense.
This comment has been minimized.
This comment has been minimized.
0e131a4
to
834aaa4
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
Makes sense to me, but I'd like the opinion of others as well.
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 ! I don't think we have to resolve the previous discussion to fix this issue and merge. We're going to have to do it regardless of the result of the discussion.
@@ -1,4 +0,0 @@ | |||
# pylint: disable=missing-docstring |
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.
π good work merging those two
This was a bug, but it's going to make new messages appear so let's delay it to 2.16.0 as we advertise the patch version to only remove message in the doc: https://pylint.pycqa.org/en/latest/user_guide/installation/upgrading_pylint.html#upgrading-pylint |
Pull Request Test Coverage Report for Build 3708409930
π - Coveralls |
This comment has been minimized.
This comment has been minimized.
c8ae706
any reason this can't go out with 2.16.0? |
No but, I'm trying to wrap up 2.16.0 and focus on what is absolutely necessary (known unreleased issues). |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #7808 +/- ##
=======================================
Coverage 95.46% 95.46%
=======================================
Files 176 176
Lines 18543 18544 +1
=======================================
+ Hits 17702 17703 +1
Misses 841 841
|
This comment has been minimized.
This comment has been minimized.
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.
Two nits and let's merge, good job!
@@ -0,0 +1,5 @@ | |||
disallowed-name:3:0:3:7:baz:"Disallowed name ""baz""":UNDEFINED |
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 think it's high confidence, right ?
c8ae706
to
8e278c2
Compare
π€ According to the primer, this change has no effect on the checked open source code. π€π This comment was generated for commit 8e278c2 |
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.
Great !
The change was limited in scope in the following changes.
Type of Changes
Description
Closes #3701
The original issue brought up the fact that
foo = {}.keys()
was not getting flagged for disallowed-name. I discovered that in some past refactoring, oneelse:
case wasn't included, which is checking all module-level var names.