Skip to content

Commit 1a942aa

Browse files
AvasamAlexWaygood
andauthored
Replace Flake8 checks with Ruff (except for flake8-pyi) (#11496)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 55205a4 commit 1a942aa

File tree

8 files changed

+63
-50
lines changed

8 files changed

+63
-50
lines changed

.flake8

+14-39
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,19 @@
1-
# The following rules come from plugins that are not used by typeshed.
2-
# Since typeshed stubs them, they can still be expected to be found in a
3-
# developer's venv for intellisense and reference reasons
4-
# A flake8-builtins
5-
# B flake8-bugbear
6-
# D flake8-docstrings
7-
# N8 pep8-naming
8-
# SIM flake8-simplify
9-
# RST flake8-rst-docstrings
10-
# TYP flake8-typing-imports
11-
12-
# The following rules are incompatible with or enforced by Black:
13-
# E203 whitespace before ':' -- scripts only
14-
# E301 expected 1 blank line
15-
# E302 expected 2 blank lines
16-
# E305 expected 2 blank lines
17-
# E501 line too long
18-
# E701 Multiple statements on one line (colon) -- disallows "..." on the same line
19-
20-
# Some rules are considered irrelevant to stub files:
21-
# F401 imported but unused -- does not recognize re-exports
22-
# https://github.com/PyCQA/pyflakes/issues/474
23-
24-
# Rules that are out of the control of stub authors:
25-
# E741 ambiguous variable name
26-
# F403 import *' used; unable to detect undefined names
27-
# F405 defined from star imports
28-
291
[flake8]
30-
extend-ignore = A, B, D, N8, SIM, RST, TYP, E301, E302, E305, E501, E701
2+
# NQA: Ruff won't warn about redundant `# noqa: Y`
3+
# Y: Flake8 is only used to run flake8-pyi, everything else is in Ruff
4+
# F821: Until https://github.com/astral-sh/ruff/issues/3011 is fixed, we need flake8-pyi's monkeypatching
5+
select = NQA, Y, F821
6+
# Ignore rules normally excluded by default
7+
extend-ignore = Y090
318
per-file-ignores =
32-
*.py: E203
33-
*.pyi: E741, F401, F403, F405
34-
# Since typing.pyi defines "overload" this is not recognized by Flake8 as typing.overload.
35-
# Unfortunately, Flake8 does not allow to "noqa" just a specific error inside the file itself.
36-
# https://github.com/PyCQA/flake8/issues/1079
37-
# F811 redefinition of unused '...'
38-
stdlib/typing.pyi: E741, F401, F403, F405, F811
39-
# Generated protobuf files include docstrings,
40-
# and import some things from typing_extensions that could be imported from typing
41-
*_pb2.pyi: E741, F401, F403, F405, Y021, Y023, Y026, Y053, Y054
9+
# We should only need to noqa Y and F821 codes in .pyi files
10+
*.py: NQA
11+
# Generated protobuf files:
12+
# Y021: Include docstrings
13+
# Y023: Alias typing as typing_extensions
14+
# Y026: Have implicit type aliases
15+
# Y053: have literals >50 characters long
16+
*_pb2.pyi: Y021, Y023, Y026, Y053
4217

4318
exclude = .venv*,.git
4419
noqa_require_code = true

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ repos:
2525
# Very few rules are useful to run on our test cases;
2626
# we explicitly enumerate them here:
2727
name: Run ruff on the test cases
28-
args: ["--exit-non-zero-on-fix", "--select=FA,I", "--no-force-exclude", "--unsafe-fixes"]
28+
args: ["--exit-non-zero-on-fix", "--select=FA,I,RUF100", "--no-force-exclude", "--unsafe-fixes"]
2929
files: '.*test_cases/.+\.py$'
3030
- repo: https://github.com/psf/black-pre-commit-mirror
3131
rev: 24.1.1 # must match requirements-tests.txt

pyproject.toml

+38-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@ exclude = [
3232
# are invoked via separate runs of ruff in pre-commit:
3333
# see our .pre-commit-config.yaml file for details
3434
exclude = ["**/test_cases/**/*.py"]
35+
# We still use flake8-pyi and flake8-noqa to check these (see .flake8 config file);
36+
# tell ruff not to flag these as e.g. "unused noqa comments"
37+
external = ["F821", "NQA", "Y"]
3538
select = [
3639
"B", # flake8-bugbear
3740
"FA", # flake8-future-annotations
3841
"I", # isort
42+
"RUF", # Ruff-specific and unused-noqa
3943
"UP", # pyupgrade
40-
# Only enable rules that have safe autofixes:
41-
"F401", # Remove unused imports
44+
# Flake8 base rules
45+
"E", # pycodestyle Error
46+
"F", # Pyflakes
47+
"W", # pycodestyle Warning
48+
# PYI: only enable rules that always autofix, avoids duplicate # noqa with flake8-pyi
49+
# See https://github.com/plinss/flake8-noqa/issues/22
4250
"PYI009", # use `...`, not `pass`, in empty class bodies
4351
"PYI010", # function bodies must be empty
4452
"PYI012", # class bodies must not contain `pass`
@@ -51,15 +59,43 @@ select = [
5159
"PYI058", # use `Iterator` as the return type for `__iter__` methods
5260
]
5361
ignore = [
62+
###
63+
# Rules that can conflict with the formatter (Black)
64+
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
65+
###
66+
"E111", # indentation-with-invalid-multiple
67+
"E114", # indentation-with-invalid-multiple-comment
68+
"E117", # over-indented
69+
"W191", # tab-indentation
70+
###
71+
# Rules we don't want or don't agree with
72+
###
5473
# Slower and more verbose https://github.com/astral-sh/ruff/issues/7871
5574
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
75+
# Used for direct, non-subclass type comparison, for example: `type(val) is str`
76+
# see https://github.com/astral-sh/ruff/issues/6465
77+
"E721", # Do not compare types, use `isinstance()`
78+
###
79+
# False-positives, but already checked by type-checkers
80+
###
81+
# Ruff doesn't support multi-file analysis yet: https://github.com/astral-sh/ruff/issues/5295
82+
"RUF013", # PEP 484 prohibits implicit `Optional`
5683
]
5784

5885
[tool.ruff.lint.per-file-ignores]
5986
"*.pyi" = [
6087
# Most flake8-bugbear rules don't apply for third-party stubs like typeshed.
6188
# B033 could be slightly useful but Ruff doesn't have per-file select
6289
"B", # flake8-bugbear
90+
# Rules that are out of the control of stub authors:
91+
"E501", # Line too long
92+
"E741", # ambiguous variable name
93+
"F403", # `from . import *` used; unable to detect undefined names
94+
# False positives in stubs
95+
"F821", # Undefined name: https://github.com/astral-sh/ruff/issues/3011
96+
# Stubs can sometimes re-export entire modules.
97+
# Issues with using a star-imported name will be caught by type-checkers.
98+
"F405", # may be undefined, or defined from star imports
6399
]
64100

65101
[tool.ruff.lint.isort]

stdlib/builtins.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class object:
9999
@property
100100
def __class__(self) -> type[Self]: ...
101101
@__class__.setter
102-
def __class__(self, type: type[object], /) -> None: ... # noqa: F811
102+
def __class__(self, type: type[object], /) -> None: ...
103103
def __init__(self) -> None: ...
104104
def __new__(cls) -> Self: ...
105105
# N.B. `object.__setattr__` and `object.__delattr__` are heavily special-cased by type checkers.

stdlib/socket.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -803,15 +803,15 @@ def getfqdn(name: str = "") -> str: ...
803803
if sys.version_info >= (3, 11):
804804
def create_connection(
805805
address: tuple[str | None, int],
806-
timeout: float | None = ..., # noqa: F811
806+
timeout: float | None = ...,
807807
source_address: _Address | None = None,
808808
*,
809809
all_errors: bool = False,
810810
) -> socket: ...
811811

812812
else:
813813
def create_connection(
814-
address: tuple[str | None, int], timeout: float | None = ..., source_address: _Address | None = None # noqa: F811
814+
address: tuple[str | None, int], timeout: float | None = ..., source_address: _Address | None = None
815815
) -> socket: ...
816816

817817
def has_dualstack_ipv6() -> bool: ...

stdlib/typing.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Since this module defines "overload" it is not recognized by Ruff as typing.overload
2+
# ruff: noqa: F811
13
# TODO: The collections import is required, otherwise mypy crashes.
24
# https://github.com/python/mypy/issues/16744
35
import collections # noqa: F401 # pyright: ignore

stubs/WebOb/@tests/test_cases/check_wsgify.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from _typeshed.wsgi import StartResponse, WSGIApplication, WSGIEnvironment
4-
from collections.abc import Iterable # noqa: F401
4+
from collections.abc import Iterable
55
from typing_extensions import assert_type
66

77
from webob.dec import _AnyResponse, wsgify

test_cases/stdlib/typing/check_all.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from __future__ import annotations
66

77
import sys
8-
from typing import * # noqa: F403
9-
from zipfile import * # noqa: F403
8+
from typing import *
9+
from zipfile import *
1010

1111
if sys.version_info >= (3, 9):
12-
x: Annotated[int, 42] # noqa: F405
12+
x: Annotated[int, 42]
1313

14-
p: Path # noqa: F405
14+
p: Path

0 commit comments

Comments
 (0)