-
-
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
False Positive: Constant name "logger" doesn't conform to UPPER_CASE naming style #2166
Comments
This is not a false positive. Pylint expects all variables at the module level to be upper case. This behaviour can be configured by passing an updated const-rgx setting in the configuration file. |
Just want to add that another solution is to add |
Why does pylint assume all module variables are CONSTANT? This assumption feels invalid. this_path = os.path.dirname(os.path.abspath(__file__)) This is dynamic per module, useful as a global member in the module, but definitely NOT a constant. |
Also this is not a great solution as it only solves that single variable name. |
(can we re-open this issue report for discussion+decision+action?) |
Yes i am also getting same issue on this line |
@anjneeksharma has a very good point, module-level globals are first-class constructs. Maybe a better heuristics to suspect a constant is a variable that (a) assigned once and (b) to a (more or less loosely defined) constant on the rhs. Why, I'll just leave this here: :)
70 matches in a nearly stock installation, Another fairly less than uncommon pattern seen in
pylint gets angry at such "constants", too. And, FWIW, it's used in astroid itself. I grok it that static code analysis is an extremely unsimple thing, and doubly so given that Python lacks a syntactic concept of constant. This comment by @mcallaghan-bsm in this very thread is a good illustration of the semantic borderline: |
@PCManticore Given your Jun 4, 2018 comment, please change the diagnostic from
to
In my opinion, the upper-case requirement should only apply to constants, wherever they are defined. However, I can live with this quirk if the diagnostic was at least telling the real reason for flagging the statement. My example source code:
pylint stdout:
|
@PCManticore , per community notes we should re-open this issue for re-consideration. |
@mcallaghan-bsm @texadactyl Since 3422e4a we no longer emit |
nice, that precisely addresses my concerns (i guess the PR could also note it addresses this ticket or this is a duplicate of #3111 & #3132) |
My point 8 days ago is that in my example, "xvar" was being called a constant (C0103). It isn't a constant. That's why I suggested the wording change in the diagnostic string. |
Maybe I'm missing something but is it correct that the following code
gives this output from pylint?
I'm running
|
Same problem on windows when I have multiple interfaces. I found a fix at stack overflow
So I changed my local copy of discovery.py as follows:
It now works a treat for me! |
This still appear to be an issue. |
@PCManticore A small program:
Pylint flags variable xvar as follows:
Variable xvar is not a "Constant" because it changes value. The diagnostic is not true. |
The 3422e4a commit referenced above was first included in the v2.5.0 tag, as indicated on the commit's webpage. You need to upgrade your pylint to v2.5.0 or later for non-constant variables such as |
@plwalsh ..... essai.py:
pylint --version
pylint essai.py
|
This issue should be re-opened unless I am missing something (possible!).
I could live with that even though I disagree with it. |
@texadactyl: my response was directed at people like @dickreuter and @hernan-erasmo who were still having issues with non-string/non-number "module-level names". @PCManticore stated above that simple strings and numbers will still be treated as constants. Pylint is static code analysis; they can't execute the code to see if a variable truly holds a constant value or not, which is why he says this new rule is "as good as it gets." Additionally, rewording the diagnostic as you suggest would then invalidate the rule as they've now defined it. They're no longer saying that all "module-level names" should conform to UPPER_CASE, because they are excluding what they deem to be "module-level non-constants", such as expressions that are not simple strings or numbers. |
@plwalsh |
@texadactyl: Truthfully, I think the real misnomer is the use of the word "constant" at all, since Python has no such concept. To me, it would make more sense for this rule to be talking about "globals" rather than "constants". Because I think what they're really saying with C0103 is that non-object globals (such as strings and numbers) should be UPPER_CASE. Whereas instantiated objects stored in globals (such as loggers and parsers) do not need to be upper case. "simple" in the previous context was meant to refer to built-in data types like |
Its okay, I black-listed C0103 - I'll not see it again. (-: I more need help with issue #3750 because that is preventing some team members from using pylint. I am trying my best to get them to pylint to diagnose syntax and semantic errors before integration + execution. Yes, we are heavy astropy users. |
Am I missing something here. It has gone from being able to supply a regex to match only using upper or lower case, to no C0103 reporting of pascal/camelcase naming at the global scope.
The following was run with 2.4.4 and 3.15.0
With 2.4.4 it was at least detecting that the name in the global scope did not conform, although reporting it as a Constant
With 3.15.0 it doesn't care.
|
@dmail00 This sounds like a regression. Is this change only on |
@DanielNoord @mbyrnepr2 I understand the logic behind the change, but for it to not care about the name of something in the global scope is, in my opinion, an error. I specified snake_case on the command line for variables and whilst pylint may not be able to detect if it is a variable or constant it should report some error number as it does not match UPPER_CASE or snake_case.
Then I would patch the message in a custom reporter class and report that a global name doesn't conform to the naming standards. This is something which I use in an educational setting as part of a test stratergy for student's work. After seeing the change introduced in 2.5.0 I thought that I may no longer need to do this and could just rely on the standard reporting, but for it to not care about global names seems bizarre, it is a variable after all. The library went from reporting incorrectly module level names as constants to ignoring module level names which have the same conventions to functions as per PEP-8.[1][2] In my situation, I can not use anything greater than 2.4.4 with this change. [1] https://peps.python.org/pep-0008/#global-variable-names |
Indeed @dmail00 I think the change which led to no warning being emitted for global non-constant assign-names was unintentional; we should fix that. In your example, changing the list to a string will emit the warning as expected because it is seen as a constant. We need to extend that logic to also consider assigning non-constants. |
It would probably best to open a new issue for this as this will likely get lost otherwise. |
Steps to reproduce
logging
in a module.logger
.Current behavior
Pylint interprets
logger
as a constant and warns that it should be capitalized.Expected behavior
A
logger
object should not be interpreted as a constant, so there should not be a warning forlogger
.pylint --version output
The text was updated successfully, but these errors were encountered: