Skip to content

Commit 3b63891

Browse files
ruff: enable pep8-naming rules (#18144)
The vast majority of mypy's code follows PEP 8 naming conventions. However, we currently don't enforce this in the linter config. I noticed this in a recent PR which included a `camelCase` name: #18132 (comment). Ruff has some rules to enforce PEP 8 naming style ([pep8-naming](https://docs.astral.sh/ruff/rules/#pep8-naming-n)). I think it would be a good idea to enable some of these to help contributors catch naming discrepancies before PR review. We have a few notable exceptions to PEP 8 naming (e.g. functions named to match ast node names), but these are easily accounted for with some config file ignores and handful of `# noqa`'s.
1 parent 0ee6dc9 commit 3b63891

File tree

5 files changed

+11
-4
lines changed

5 files changed

+11
-4
lines changed

mypy/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ def accept(self, visitor: ExpressionVisitor[T]) -> T:
17271727
return visitor.visit_str_expr(self)
17281728

17291729

1730-
def is_StrExpr_list(seq: list[Expression]) -> TypeGuard[list[StrExpr]]:
1730+
def is_StrExpr_list(seq: list[Expression]) -> TypeGuard[list[StrExpr]]: # noqa: N802
17311731
return all(isinstance(item, StrExpr) for item in seq)
17321732

17331733

mypy/test/teststubgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ def test(cls, arg0: str) -> None:
987987
def test_generate_c_type_classmethod_with_overloads(self) -> None:
988988
class TestClass:
989989
@classmethod
990-
def test(self, arg0: str) -> None:
990+
def test(cls, arg0: str) -> None:
991991
"""
992992
test(cls, arg0: str)
993993
test(cls, arg0: int)

mypyc/lib-rt/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
compile_args = ["--std=c++11"]
2222

2323

24-
class build_ext_custom(build_ext):
24+
class build_ext_custom(build_ext): # noqa: N801
2525
def get_library_names(self):
2626
return ["gtest"]
2727

mypyc/test/test_emitfunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_integer(self) -> None:
134134
def test_tuple_get(self) -> None:
135135
self.assert_emit(TupleGet(self.t, 1, 0), "cpy_r_r0 = cpy_r_t.f1;")
136136

137-
def test_load_None(self) -> None:
137+
def test_load_None(self) -> None: # noqa: N802
138138
self.assert_emit(
139139
LoadAddress(none_object_op.type, none_object_op.src, 0),
140140
"cpy_r_r0 = (PyObject *)&_Py_NoneStruct;",

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ select = [
115115
"W", # pycodestyle (warning)
116116
"B", # flake8-bugbear
117117
"I", # isort
118+
"N", # pep8-naming
118119
"RUF100", # Unused noqa comments
119120
"PGH004", # blanket noqa comments
120121
"UP", # pyupgrade
@@ -134,6 +135,8 @@ ignore = [
134135
"E721", # Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
135136
"E731", # Do not assign a `lambda` expression, use a `def`
136137
"E741", # Ambiguous variable name
138+
"N818", # Exception should be named with an Error suffix
139+
"N806", # UPPER_CASE used for constant local variables
137140
"UP031", # Use format specifiers instead of percent format
138141
"UP032", # 'f-string always preferable to format' is controversial
139142
"C416", # There are a few cases where it's nice to have names for the dict items
@@ -147,6 +150,10 @@ unfixable = [
147150
"UP036", # sometimes it's better to just noqa this
148151
]
149152

153+
[tool.ruff.lint.per-file-ignores]
154+
# Mixed case variable and function names.
155+
"mypy/fastparse.py" = ["N802", "N816"]
156+
150157
[tool.ruff.lint.isort]
151158
combine-as-imports = true
152159
extra-standard-library = ["typing_extensions"]

0 commit comments

Comments
 (0)