Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

2.7.4 expects any ClassVar to be a costant, results in invalid-name #4277

Closed
contrivable opened this issue Apr 1, 2021 · 4 comments · Fixed by #4279
Closed

2.7.4 expects any ClassVar to be a costant, results in invalid-name #4277

contrivable opened this issue Apr 1, 2021 · 4 comments · Fixed by #4279
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Regression
Milestone

Comments

@contrivable
Copy link

Typing a class attribute as ClassVar leads to the invalid assumption that the attribute must be constant and has pylint produce invalid-name. This also happens for Supscriptions (more elaborately typed variables, e. g. ClassVar[List]).

Searching for "invalid-name" and "classvar" I find three issues - a closed one concerning Enum (#4149) and an open and a closed one concerning dataclasses (#3405, #4154). I deem them tangential.

The preview release did not solve my problem - see below.

Steps to reproduce

Given a file a.py:

# pylint: disable=missing-module-docstring,missing-class-docstring,too-few-public-methods
import typing
from typing import ClassVar


class SomeClass:
    shared_var_1: ClassVar = list()
    shared_var_1: typing.ClassVar = list()

Current behavior

Result of pylint a.py:

************* Module a
a.py:7:4: C0103: Class constant name "shared_var_1" doesn't conform to UPPER_CASE naming style (invalid-name)
a.py:8:4: C0103: Class constant name "shared_var_1" doesn't conform to UPPER_CASE naming style (invalid-name)

-------------------------------------------------------------------
Your code has been rated at 6.00/10 (previous run: 10.00/10, -4.00)

Expected behavior

Mutable ClassVar should not be judged like constants and should not result in invalid-name.

PEP-591 states:

Type checkers should infer a final attribute that is initialized in a class body as being a class variable. Variables should not be annotated with both ClassVar and Final.

While that makes all constants also ClassVar, ClassVar is not necessarily constant.

pylint --version output

Result of pylint --version output:

pylint 2.7.4
astroid 2.5.2
Python 3.8.8 (default, Feb 24 2021, 21:46:12) 
[GCC 7.3.0]

The preview release (pylint 3.0.0a1, other versions the same) produces the same pylint complains as 2.7.4 - invalid-name. For pylint 2.7.2 (other versions the same) there are no complains.

#4266 recently changed corresponding code. I suspect these code changes enabled actual checking for ClassVar in the first place (and work well enough for actual constants) but now lead to the problems mentioned above for non-constants.

@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Regression labels Apr 1, 2021
@Pierre-Sassoulas
Copy link
Member

Thanks for this very detailed issue :)

@cdce8p
Copy link
Member

cdce8p commented Apr 1, 2021

That one is one me. I confused the meaning of ClassVar and Final in a class context, but as you said: ClassVars can be mutable. I'll work on a fix for it soon.

@cdce8p
Copy link
Member

cdce8p commented Apr 1, 2021

Opened #4279

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.7.5 milestone Apr 1, 2021
@contrivable
Copy link
Author

Outstanding response time!
Thank-yous all around.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants