astral-sh / astral-sh/ruff

C0103/N815: Constant names are not checked against UPPER_CASE naming convention

Open
#2,964 7 comments 12 reactions 0 assignees View on GitHub
rule
Dominant language
Rust
Stars
49.6k
Forks
2.4k
Avg merge
2d 1h
Merged PRs (30d)
445

Description

Hi ruff team,

It looks like ruff doesn't check the name of constants like pylint does, despite `invalid-name` / `C0103` (`N815`) being marked as done in the [pylint tracking issue](https://github.com/charliermarsh/ruff/issues/970).

PEP 8 recommends UPPER_CASE for constant names ([ref](https://peps.python.org/pep-0008/#constants)).

### Example

```py
# file.py
from typing import NamedTuple

Other = NamedTuple("Other", "a,b,c")

not_correct = "tata"
CORRECT = ""
ALSO_CORRECT = ""

def foo():
print(not_correct)
```
```sh
$ pylint file.py --disable=all --enable=C0103
************* Module file
file.py:6:0: C0103: Constant name "not_correct" doesn't conform to UPPER_CASE naming style (invalid-name)

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

$ ruff file.py --verbose --no-cache
[2023-02-16][12:57:12][ruff::commands::run][DEBUG] Identified files to lint in: 226.791µs
[2023-02-16][12:57:12][ruff::commands::run][DEBUG] Checked 1 files in: 7.339083ms

$ ruff --version
ruff 0.0.247
```

### Solutions

pep8-naming N816 (mixedCase variable in global scope) already implements a weaker version of this, so I was able to adapt the code to only allow UPPER_CASE variable names in global scope, but the two rules feel a bit redundant together as some constant named `thisIs_incorrect` would trigger both N816 (because it is a mixed case naming) and C0103 (it does not follow UPPER_CASE convention for constants).

I guess there are 2 paths forward to solve this issue:
- Implement C0103 as a separate rule, but accept that some variable naming may trigger 2 separate diagnostics
- Modify N816 to only allow UPPER_CASE names, but this would differ from the original `pep8-naming` package

In any case, I would be glad to submit a PR to help get this fixed !

_PS: Thanks for the awesome work you're all doing !_

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior with the provided file.py example and compare Ruff's handling of N816 with pylint's C0103 output. Read the existing N816 implementation and decide whether constant naming belongs in a separate C0103 rule or should be folded into N816. Done means global constant names are checked consistently and the chosen behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.