Skip to content
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 for UPPER_CASE variable name #3132

Closed
lesurp opened this issue Sep 25, 2019 · 2 comments
Closed

False positive for UPPER_CASE variable name #3132

lesurp opened this issue Sep 25, 2019 · 2 comments
Labels
Bug 🪲 Usability Issues related to the usability/UX of pylint

Comments

@lesurp
Copy link

lesurp commented Sep 25, 2019

Steps to reproduce

Code:

import random

if __name__ == "__main__":
    number = random.randint(1, 10)

Current behavior

************* Module ok
ok.py:1:0: C0114: Missing module docstring (missing-module-docstring)
ok.py:4:4: C0103: Constant name "number" doesn't conform to UPPER_CASE naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

The second error implies number is constant, which it is not.

pylint --version output

pylint 2.4.0
astroid 2.3.0
Python 3.7.3 (default, May  9 2019, 15:28:41) 
[GCC 5.4.0 20160609]
@lesurp
Copy link
Author

lesurp commented Sep 25, 2019

Note: it seems the bug is actually because pylint treats the if __name__ == '__main__' as module level code e.g.:

import random


def compute(number):
    return 2 * number


if __name__ == "__main__":
    number = random.randint(1, 10)

produces:

************* Module ok
ok.py:1:0: C0114: Missing module docstring (missing-module-docstring)
ok.py:4:0: C0116: Missing function or method docstring (missing-function-docstring)
ok.py:4:12: W0621: Redefining name 'number' from outer scope (line 9) (redefined-outer-name)
ok.py:9:4: C0103: Constant name "number" doesn't conform to UPPER_CASE naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 2.00/10 (previous run: 2.00/10, +0.00)

The re-use of number should be fine here, but we have Redefining name 'number' from outer scope (line 9) (redefined-outer-name)

@PCManticore
Copy link
Contributor

We probably should change the interpretation of "constant" as right now it's not entirely correct. There was another recent issue about the same thing and I agree that our interpretation of "constant" from PEP 8 was erroneous.

@PCManticore PCManticore added Bug 🪲 Usability Issues related to the usability/UX of pylint labels Sep 26, 2019
pastelmind added a commit to pastelmind/d2txt that referenced this issue Jan 21, 2020
This check causes many false positives, especially for global variables
used in simple scripts.

Looks like this will be fixed in Pylint 2.5.0, though. It's already been
integrated into the main branch:
pylint-dev/pylint@3422e4a

Also see relevant issues that motivated the change:
- pylint-dev/pylint#3111
- pylint-dev/pylint#3132

The latest changelog with the fix mentioned under 2.5.0 changes:
https://github.com/PyCQA/pylint/blob/f2f4e6f42416644471ab003d4df7ecf052c3c411/ChangeLog
pastelmind added a commit to pastelmind/d2txt that referenced this issue Jan 21, 2020
Move all CLI script code under `if __name__ == "__main__"` to separate
module-level `main()` methods that serve as the entrypoint. This fixes
many invalid-name errors and all redefined-outer-name errors.

Sort imports alphabetically, and group them in the order:
builtins, 3rd-parties, local stuff. This eliminates import-related
warnings.

Other minor warnings are dealt with by fixing the code, or disabling the
warning if it is a false positive.

Notably, refactor warnings (Rxxxx) are not handled. These require
considerably more thought to work out. Since VS code doesn't show them
as errors, we're in no rush.

Unrelated to Pylint warnings: In sample scripts, remove the statements
that directly added the project root to sys.path. This was a hack used
to make d2txt.py importable in sample scripts. Now that we have
`pip install -e .`, there's no need to tamper with sys.path.

Note: As of Pylint 2.4.4, it incorrectly believes that all module-level
variables are constants, and emits invalid-name on them. This check had
to be disabled via comments sprinkled throughout the codebase.
Looks like this will be fixed in Pylint 2.5.0, though. It's already been
integrated into the main branch:
pylint-dev/pylint@3422e4a

Also see relevant issues that motivated the change:
- pylint-dev/pylint#3111
- pylint-dev/pylint#3132

The latest changelog with the fix mentioned under 2.5.0 changes:
https://github.com/PyCQA/pylint/blob/f2f4e6f42416644471ab003d4df7ecf052c3c411/ChangeLog
pastelmind added a commit to pastelmind/d2txt that referenced this issue Jan 22, 2020
Move all CLI script code under `if __name__ == "__main__"` to separate
module-level `main()` methods that serve as the entrypoint. This fixes
many invalid-name errors and all redefined-outer-name errors.

Sort imports alphabetically, and group them in the order:
builtins, 3rd-parties, local stuff. This eliminates import-related
warnings.

Other minor warnings are dealt with by fixing the code, or disabling the
warning if it is a false positive.

Notably, refactor warnings (Rxxxx) are not handled. These require
considerably more thought to work out. Since VS code doesn't show them
as errors, we're in no rush.

Unrelated to Pylint warnings: In sample scripts, remove the statements
that directly added the project root to sys.path. This was a hack used
to make d2txt.py importable in sample scripts. Now that we have
`pip install -e .`, there's no need to tamper with sys.path.

Note: As of Pylint 2.4.4, it incorrectly believes that all module-level
variables are constants, and emits invalid-name on them. This check had
to be disabled via comments sprinkled throughout the codebase.
Looks like this will be fixed in Pylint 2.5.0, though. It's already been
integrated into the main branch:
pylint-dev/pylint@3422e4a

Also see relevant issues that motivated the change:
- pylint-dev/pylint#3111
- pylint-dev/pylint#3132

The latest changelog with the fix mentioned under 2.5.0 changes:
https://github.com/PyCQA/pylint/blob/f2f4e6f42416644471ab003d4df7ecf052c3c411/ChangeLog
pastelmind added a commit to pastelmind/d2txt that referenced this issue Jan 22, 2020
Move all CLI script code under `if __name__ == "__main__"` to separate
module-level `main()` methods that serve as the entrypoint. This fixes
many invalid-name errors and all redefined-outer-name errors.

Sort imports alphabetically, and group them in the order:
builtins, 3rd-parties, local stuff. This eliminates import-related
warnings.

Other minor warnings are dealt with by fixing the code, or disabling the
warning if it is a false positive.

Notably, refactor warnings (Rxxxx) are not handled. These require
considerably more thought to work out. Since VS code doesn't show them
as errors, we're in no rush.

Unrelated to Pylint warnings: In sample scripts, remove the statements
that directly added the project root to sys.path. This was a hack used
to make d2txt.py importable in sample scripts. Now that we have
`pip install -e .`, there's no need to tamper with sys.path.

Note: As of Pylint 2.4.4, it incorrectly believes that all module-level
variables are constants, and emits invalid-name on them. This check had
to be disabled via comments sprinkled throughout the codebase.
Looks like this will be fixed in Pylint 2.5.0, though. It's already been
integrated into the main branch:
pylint-dev/pylint@3422e4a

Also see relevant issues that motivated the change:
- pylint-dev/pylint#3111
- pylint-dev/pylint#3132

The latest changelog with the fix mentioned under 2.5.0 changes:
https://github.com/PyCQA/pylint/blob/f2f4e6f42416644471ab003d4df7ecf052c3c411/ChangeLog
pastelmind added a commit to pastelmind/d2txt that referenced this issue Jan 22, 2020
Move all CLI script code under `if __name__ == "__main__"` to separate
module-level `main()` methods that serve as the entrypoint. This fixes
many invalid-name errors and all redefined-outer-name errors.

Sort imports alphabetically, and group them in the order:
builtins, 3rd-parties, local stuff. This eliminates import-related
warnings.

Other minor warnings are dealt with by fixing the code, or disabling the
warning if it is a false positive.

Notably, refactor warnings (Rxxxx) are not handled. These require
considerably more thought to work out. Since VS code doesn't show them
as errors, we're in no rush.

Unrelated to Pylint warnings: In sample scripts, remove the statements
that directly added the project root to sys.path. This was a hack used
to make d2txt.py importable in sample scripts. Now that we have
`pip install -e .`, there's no need to tamper with sys.path.

Note: As of Pylint 2.4.4, it incorrectly believes that all module-level
variables are constants, and emits invalid-name on them. This check had
to be disabled via comments sprinkled throughout the codebase.
Looks like this will be fixed in Pylint 2.5.0, though. It's already been
integrated into the main branch:
pylint-dev/pylint@3422e4a

Also see relevant issues that motivated the change:
- pylint-dev/pylint#3111
- pylint-dev/pylint#3132

The latest changelog with the fix mentioned under 2.5.0 changes:
https://github.com/PyCQA/pylint/blob/f2f4e6f42416644471ab003d4df7ecf052c3c411/ChangeLog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Usability Issues related to the usability/UX of pylint
Projects
None yet
Development

No branches or pull requests

2 participants