-
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
Question: why doesn't ruff complain about CapsWords (PascalCase)? #15069
Comments
Read-as-written, I believe attributes defined at the class-level are not instance variables, but rather class variables (unless you're using a dataclass) class MyClass:
class_var = "bar"
def __init__(self):
self.instance_var = "foo" That being said, I have no idea if PEP-8 intended to cover class variables here, or type-annotations-only declaractions. |
There is N815 which says: Bad: class MyClass:
myVariable = "hello"
another_variable = "world" Good: class MyClass:
my_variable = "hello"
another_variable = "world" At first glance, it's surprising that it doesn't exclude PascalCase, but maybe there is a reason? |
Technically, I can't disagree, that it is class attribute (and I am not sure about explicit PEP-8 opinion on that). But N815 would complain on For popular case like |
These are just my own notes from looking into this issue: The difference here doesn't come from whether the variable is attributed. It correctly flags This sounds very familiar to #14905 Ruff's behavior is consistent with pep8-namiang class MyClass:
MyVariable: int
myVariable: int ❯ uv tool run --with pep8-naming flake8 test.py
test.py:3:6: N815 variable 'myVariable' in class scope should not be mixedCase |
I did search the pep8-naming repository for an explanation but couldn't find any. My best guess is that CapNames are allowed because flagging them would be in conflict with the PEP8 recommendation for type variables. |
@MichaReiser Another code pattern that could explain this are nested classes, where the nested class is swapped out using an assignment to a different class by a subclass. I've seen this pattern used in a few places over the years. E.g. I think we could come up with some heuristics for the given example with |
I also sometimes use this pattern: from enum import Enum, auto
from typing import TypeAlias
class LoggerOption(Enum):
INFO = auto()
WARN = auto()
class Logger:
Option: TypeAlias = LoggerOption
def __init__(self, option: Option): # using the alias
self.option = option In another file: from logger import Logger # only Logger needs to be imported
logger = Logger(Logger.Option.INFO) |
PEP 8 describes that:
and that for Method Names and Instance Variables
Short
example.py
:ruff check example.py --select ALL --ignore D All checks passed!
The text was updated successfully, but these errors were encountered: