Skip to content

Make invalid-name distinguish between module constants and module variables #3585

Description

@bersbersbers

Steps to reproduce

  1. pylint bug.py
"""False-positive invalid-name: `variable` is not constant, should not be UPPER_CASE."""
for i in (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)

Expected behavior

Perfect score.

pylint --version output

pylint 2.5.0
astroid 2.4.0
Python 3.8.2 (default, Feb 26 2020, 09:52:54) 
[GCC 7.5.0]

Problem does not appear with range(3). Problem does not appear with pylint<2.5.

Activity

  1. Project-Magenta commented on May 4, 2020

    @Project-Magenta

    variable is only assigned once, why is it not constant?

  2. bersbersbers commented on May 4, 2020

    @bersbersbers
    Author

    @Project-Magenta variable is assigned not once, but three times. Consider this:

    for i in (0, 1, 2):
        variable = i
        print(variable)
    

    which prints

    >>> for i in (0, 1, 2):
    ...     variable = i
    ...     print(variable)
    ...
    0
    1
    2
    
  3. Project-Magenta commented on May 4, 2020

    @Project-Magenta

    oh ok i see

  4. PCManticore commented on May 5, 2020

    @PCManticore
    Contributor

    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.

  5. bersbersbers commented on May 5, 2020

    @bersbersbers
    Author

    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-name is a bug, then?

    """False-negative invalid-name: should behave the same as (0, 1, 2)."""
    for i in range(3):
        variable = i
    

    Or this one?

    """False-negative invalid-name: should behave the same as (0, 1, 2)."""
    for i in tuple(range(3)):
        variable = i
    

    Or this one?

    """False-negative invalid-name: should behave the same as [0, 1, 2]."""
    for i in list(range(3)):
        variable = i
    

    Something is inconsistent here, I don't think this is ready to be closed.

  6. PCManticore commented on May 5, 2020

    @PCManticore
    Contributor

    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)

  7. Project-Magenta commented on May 5, 2020

    @Project-Magenta

    but it is assigned 3 times because python variables are module/function scoped
    ???

  8. bersbersbers commented on Jun 4, 2020

    @bersbersbers
    Author

    Yes, something is off with this check. While I get a warning in the original code,

    for i in (0, 1, 2):
        variable = i
    

    I do not get any in this case:

    for i in (0, 1, 2):
        variable, another = i, i
    
  9. bodograumann commented on Sep 15, 2020

    @bodograumann

    This seems related:

    x = 0
    for i in range(7):
        x+=1

    Here pylint also says x is a constant, when it is obviously mutated.

  10. bersbersbers commented on Sep 15, 2020

    @bersbersbers
    Author

    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.

  11. 34 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C: invalid-nameEnhancement ✨Improvement to a componentNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions