You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make invalid-name distinguish between module constants and module variables #3585
"""False-positive invalid-name: `variable` is not constant, should not be UPPER_CASE."""foriin (0, 1, 2):
variable=i
Current behavior
************* Module cia.bug
bug.py:3:4: C0103: Constant name "variable" doesn't conform to UPPER_CASE naming style (invalid-name)
------------------------------------------------------------------
Your code has been rated at 5.00/10 (previous run: 5.00/10, +0.00)
The variable is assigned once but as long as it is a Const in astroid's parlance (that is string, numbers, floats, bools), pylint asks you to use the UPPER_CASE style when defining these variables at module level. This is expected behaviour regardless if you assign the variable in a loop or not. I don't quite though why you have to set this variable at module level multiple times.
Reacted by Alisa Sireneva, Mihail-Cosmin Munteanu, Aaron, Dave Chevell, Rosco Kalis, Finite State Machine, DavidJCobb and Jeff Goeders
Thanks for the additional examples. Something is definitely inconsistent with this check. In the latter examples you gave, pylint cannot fully infer that the variable is a constant or not, while it can do so in the original example. I believe in this case we should simply emit the invalid-name as long as the variable is assigned only once at module level (thus indicating that it's an actual constant)
Whoever works on this, please consider adapting the error message if required. Currently, it says
Constant name "variable" doesn't conform to UPPER_CASE naming style
variable is not a constant as I understand it. In particular, the "name" variable is not constant. But digesting #3585 (comment) made me realize that what apparently counts is not (only) if variable is itself constant (that is, variable has the same value at all times), but (also) if variable has a value that is constant - in the sense of immutable. I hope this interpretation is correct - if it is, it may help to clarify the error message.
Activity
Project-Magenta commentedon May 4, 2020
variable is only assigned once, why is it not constant?
bersbersbers commentedon May 4, 2020
@Project-Magenta
variableis assigned not once, but three times. Consider this:which prints
Project-Magenta commentedon May 4, 2020
oh ok i see
PCManticore commentedon May 5, 2020
The variable is assigned once but as long as it is a
Constin astroid's parlance (that is string, numbers, floats, bools),pylintasks you to use the UPPER_CASE style when defining these variables at module level. This is expected behaviour regardless if you assign the variable in a loop or not. I don't quite though why you have to set this variable at module level multiple times.bersbersbers commentedon May 5, 2020
Interesting. Coming from C++, I thought I knew what a "constant" is - will have to adapt apparently.
Now, the fact that this code does not give
invalid-nameis a bug, then?Or this one?
Or this one?
Something is inconsistent here, I don't think this is ready to be closed.
PCManticore commentedon May 5, 2020
Thanks for the additional examples. Something is definitely inconsistent with this check. In the latter examples you gave, pylint cannot fully infer that the variable is a constant or not, while it can do so in the original example. I believe in this case we should simply emit the
invalid-nameas long as the variable is assigned only once at module level (thus indicating that it's an actual constant)Project-Magenta commentedon May 5, 2020
but it is assigned 3 times because python variables are module/function scoped
???
bersbersbers commentedon Jun 4, 2020
Yes, something is off with this check. While I get a warning in the original code,
I do not get any in this case:
bodograumann commentedon Sep 15, 2020
This seems related:
Here pylint also says
xis a constant, when it is obviously mutated.bersbersbers commentedon Sep 15, 2020
Whoever works on this, please consider adapting the error message if required. Currently, it says
variableis not a constant as I understand it. In particular, the "name"variableis not constant. But digesting #3585 (comment) made me realize that what apparently counts is not (only) ifvariableis itself constant (that is,variablehas the same value at all times), but (also) ifvariablehas a value that is constant - in the sense of immutable. I hope this interpretation is correct - if it is, it may help to clarify the error message.34 remaining items
Apply variable name regex to module-level names that are reassigned #…
chore(deps): update dependency pylint to v4 (#384)
chore(deps): update dependency pylint to v4 (#168)
chore(deps): update dependency pylint to v4 (#516)
chore(deps): update dependency pylint to v4 (#213)
chore(deps): update dependency pylint to v4 (#399)
chore(deps): update dependency pylint to v4 (#302)
chore(deps): update pylint (major)
Merge branch 'renovate/pylint-4.x' into 'main'
Delete 8 closed-issue branches
Delete 8 closed-issue branches
Delete 8 closed-issue branches
Rename module variable in json generation script