pep8-naming icon indicating copy to clipboard operation
pep8-naming copied to clipboard

Add constants naming rules

Open sobolevn opened this issue 8 years ago • 8 comments

pep8 implies that constants are named in UPPER_CASE: https://www.python.org/dev/peps/pep-0008/#constants

Right now this code is considered valid:

# Module-level variable:
delta = 10

However, it is not valid. This one is:

# Module-level variable:
DELTA = 10

pylint has checks for it: C: 8, 0: Constant name "delta" doesn't conform to UPPER_CASE naming style (invalid-name)

sobolevn avatar Feb 03 '18 11:02 sobolevn

I'm curious to know how pylint detects constants. Note that if a constant literal (10) is assigned to a variable (delta), that's not enough evidence to call that variable a constant. Some other value can be assigned to the same variable later in the code...

I'd say doable, but requires a more in depth introspection.

5j9 avatar May 25 '18 08:05 5j9

@5j9 I would say that constant is a misleading name.

'Module level variable' is better. And by convention we name these variables as UPPER_CASE.

sobolevn avatar May 25 '18 08:05 sobolevn

Ah, in that case we do not agree on what is considered a constant. I've always thought of them having a constant value, i.e. I never use UPPER_CASE for a for-loop variable, even if it is defined at the module level. Same for any global variable that gets reassigned or mutated during the execution of the code in that module.

5j9 avatar May 25 '18 09:05 5j9

I'm looking at the differences between pylint and pep8-naming (through flake8) and came across this page. Has there been any development on this in the past year?

master468 avatar Aug 16 '19 13:08 master468

Any update on this? Is anyone using any other plugin other than pylint for this?

noorul avatar Nov 14 '21 04:11 noorul

I use wemake-python-styleguide that has this rule: https://wemake-python-stylegui.de/en/latest/pages/usage/violations/naming.html#global-module-level-variables

sobolevn avatar Nov 14 '21 08:11 sobolevn

I tried adding this, but it takes too much time to complete the scan.

noorul avatar Nov 14 '21 09:11 noorul

I made a small plugin for flake8 https://pypi.org/project/flake8-class-constants/ It's dirty, without tests, but it seems to work on small projects

ipadla avatar Nov 30 '21 22:11 ipadla