Summary
--
This is the first in a series of rule rename PRs. I started here because
the renames are fairly straightforward and are within a single linter
group. Most of these rules were missing some mention of docstrings in
their names or were missing or misusing something like `missing` or
`incorrect`. For example, many of the rules that start with `missing-`
actually enforce a single line somewhere, so they flag 2+ lines as well
as zero.
The first commit contains the changes in `codes.rs`, which I thought was
probably easiest to review
and comment on if any of the names need further refinement. The second
commit contains the rest of
the downstream changes.
| Code | Old name | New name |
|---------------------------------------------------|-----------------------------------------|---------------------------------------------------|
| [`D201`](https://docs.astral.sh/ruff/rules/D201/) |
`blank-line-before-function` | `blank-line-before-function-docstring` |
| [`D202`](https://docs.astral.sh/ruff/rules/D202/) |
`blank-line-after-function` | `blank-line-after-function-docstring` |
| [`D203`](https://docs.astral.sh/ruff/rules/D203/) |
`incorrect-blank-line-before-class` |
`incorrect-blank-line-before-class-docstring` |
| [`D204`](https://docs.astral.sh/ruff/rules/D204/) |
`incorrect-blank-line-after-class` |
`incorrect-blank-line-after-class-docstring` |
| [`D205`](https://docs.astral.sh/ruff/rules/D205/) |
`missing-blank-line-after-summary` |
`incorrect-blank-line-after-docstring-summary` |
| [`D207`](https://docs.astral.sh/ruff/rules/D207/) |
`under-indentation` | `under-indented-docstring-line` |
| [`D208`](https://docs.astral.sh/ruff/rules/D208/) | `over-indentation`
| `over-indented-docstring-line` |
| [`D209`](https://docs.astral.sh/ruff/rules/D209/) |
`new-line-after-last-paragraph` |
`missing-new-line-after-last-docstring-paragraph` |
| [`D210`](https://docs.astral.sh/ruff/rules/D210/) |
`surrounding-whitespace` | `whitespace-around-docstring-text` |
| [`D211`](https://docs.astral.sh/ruff/rules/D211/) |
`blank-line-before-class` | `blank-line-before-class-docstring` |
| [`D212`](https://docs.astral.sh/ruff/rules/D212/) |
`multi-line-summary-first-line` |
`multiline-docstring-summary-on-separate-line` |
| [`D213`](https://docs.astral.sh/ruff/rules/D213/) |
`multi-line-summary-second-line` |
`multiline-docstring-summary-on-first-line` |
| [`D214`](https://docs.astral.sh/ruff/rules/D214/) |
`overindented-section` | `over-indented-docstring-section` |
| [`D215`](https://docs.astral.sh/ruff/rules/D215/) |
`overindented-section-underline` |
`over-indented-docstring-section-underline` |
| [`D300`](https://docs.astral.sh/ruff/rules/D300/) |
`triple-single-quotes` | `unconventional-docstring-quotes` |
| [`D408`](https://docs.astral.sh/ruff/rules/D408/) |
`missing-section-underline-after-name` |
`blank-line-before-docstring-section-underline` |
| [`D410`](https://docs.astral.sh/ruff/rules/D410/) |
`no-blank-line-after-section` |
`missing-blank-line-after-docstring-section` |
| [`D411`](https://docs.astral.sh/ruff/rules/D411/) |
`no-blank-line-before-section` |
`missing-blank-line-before-docstring-section` |
| [`D413`](https://docs.astral.sh/ruff/rules/D413/) |
`missing-blank-line-after-last-section` |
`missing-blank-line-after-last-docstring-section` |
| [`D417`](https://docs.astral.sh/ruff/rules/D417/) |
`undocumented-param` | `undocumented-parameter` |
## Summary
Use a shared ty override for Ruff’s standalone scripts now that PEP 723
scripts can inherit project settings. Fix the diagnostics in the two PR
automation scripts so they can use the same checks.
## Notes to reviewers
All 28 standalone scripts passed ty with locked dependencies, and the
changed-file hooks and mocked workflow checks passed.
---------
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
Enable `require-build-hashes` for the Python projects and inline scripts
so isolated builds require declared hashes for their build dependencies.
Pin the build backends and their dependencies with hashes.
---------
Co-authored-by: zaniebot <242828183+zaniebot@users.noreply.github.com>
## Summary
Enable `truthiness-test-of-none-union` in the standalone scripts' inline
configuration and in the `py-fuzzer` and `ty_benchmark` projects,
following #28889.
Summary
--
This PR updates our ruff-pre-commit dependency to 0.16.5, which supports
the new category selectors,
and then enables `lint.preview` and the new default categories so that
we can dogfood them on our
code. There were 66 diagnostics initially, which I also fixed or
ignored:
```
28 FURB113 [ ] repeated-append
12 ASYNC240 [ ] blocking-path-method-in-async-function
6 RUF105 [*] noqa-comments
5 PLW1510 [ ] subprocess-run-without-check
3 PLW0108 [ ] unnecessary-lambda
2 EXE001 [ ] shebang-not-executable
2 RUF039 [*] unraw-re-pattern
1 B024 [ ] abstract-base-class-without-abstract-method
1 FURB142 [*] for-loop-set-mutations
1 PERF102 [ ] incorrect-dict-iterator
1 PLC0207 [*] missing-maxsplit-arg
1 PLW0127 [ ] self-assigning-variable
1 TRY004 [ ] type-check-without-type-error
1 PLW0120 [*] useless-else-on-loop
1 TRY201 [ ] verbose-raise
```
Of these, I ignored `repeated-append` and `blocking-path-method-in-async-function`. I think they are
still reasonable defaults, just less useful/important in our uses.
0.16.5 also includes https://github.com/astral-sh/ruff-pre-commit/pull/159, so I removed `markdown` from our main `ruff-format` hook, leaving it for the later mdtest formatting stage.
I also had to make a few changes related to config discovery in tests:
- move our cached ecosystem checkouts to `$RUNNER_TEMP` instead of inside of our checkout
- convert some CLI tests to use the `CliTest` helpers
- add `configurationPreference: editorOnly` to some notebook tests
In these cases, our own Ruff settings were being inherited by test code. This likely affects many other tests, but I only updated the obviously failing ones for now. I'm least confident about the notebook tests, but as far as I can tell from the history (https://github.com/astral-sh/ruff/pull/11944, https://github.com/astral-sh/ruff/pull/22810), the specific diagnostics are not the important parts being asserted.
Test Plan
--
CI on this PR
Python tooling can currently build third-party source distributions. Set
`no-build` in the maintained Python projects and PEP 723 scripts,
retaining named exceptions for Git-pinned tooling and the local
ecosystem runner. Pin the isolated build dependencies for those
exceptions, including their transitive requirements.
Follows merged astral-sh/ruff#27856, which gives the legacy `ruff-lsp`
suite a dedicated, locked Python 3.12 test environment.
Follow-up to astral-sh/ruff#27843. Replaces astral-sh/ruff#27840, which
closed when the merged docs branch was deleted.
Co-authored-by: zaniebot <242828183+zaniebot@users.noreply.github.com>
## Summary
This PR adds a new rule (`TID255`) to flag PEP 810 lazy imports that are
resolved immediately during module import execution.
For example:
```python
lazy import foo
class Bar(foo.Foo): ...
```
Here, `foo` is evaluated while the module is being loaded, so the import
is not meaningfully lazy.
We're also offering an unsafe fix to remove `lazy` when the import
statement imports a single member.
Closes https://github.com/astral-sh/ruff/issues/25014.
---------
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
## Summary
This PR renames TID254 to `lazy-import-mismatch` and expands it into a
single rule with two complementary settings: `require-lazy` and
`ban-lazy`. Both settings accept `"all"`, `list[str]`, or `{ include =
..., exclude = [...] }`, and we validate that the two selectors don’t
overlap.
This covers the two main use-cases users asked for:
- Require lazy imports by default, except for known side-effectful
modules. Example:
```toml
require-lazy = { include = "all", exclude = ["sitecustomize"] }
```
This enforces lazy imports everywhere Ruff can rewrite them, while
leaving sitecustomize eager.
- Forbid lazy imports for modules that must stay eager, or even forbid
lazy imports by default with a small allowlist.
```toml
# Require `sitecustomize` to stay eager.
ban-lazy = ["sitecustomize"]
# Ban lazy imports everywhere except `typing`.
ban-lazy = { include = "all", exclude = ["typing"] }
```
## Summary
`TID254` enforces the use of `lazy` imports. You can specify a set of
modules, similar to `banned-module-level-imports`, or `"all"`:
```toml
# Require every module-level import to be lazy.
banned-eager-imports = "all"
# Require lazy imports for specific modules.
banned-eager-imports = [
"boto3",
"botocore",
]
```
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:
- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
requests.)
- Does this pull request include references to any relevant issues?
-->
## Summary
<!-- What's the purpose of the change? What does it do, and why? -->
Part of #18972
This PR makes [indentation-with-invalid-multiple-comment
(E114)](https://docs.astral.sh/ruff/rules/indentation-with-invalid-multiple-comment/#indentation-with-invalid-multiple-comment-e114)'s
example not raise a syntax error by adding a 4 space indented `...`. The
example still gave `E114` without this, but adding the `...` both makes
the change in indentation of the comment clearer, and makes it not give
a `SyntaxError`.
## Test Plan
<!-- How was it tested? -->
N/A, no functionality/tests affected
## Summary
Follow-up to #16035.
`check_docs_formatted.py` will now report backtick-quoted shortcut links
in rule documentation. It uses a regular expression to find them. Such a
link:
* Starts with `[`, followed by <code>\`</code>, then a "name" sequence
of at least one non-backtick non-newline character, followed by another
<code>\`</code>, then ends with `]`.
* Is not followed by either a `[` or a `(`.
* Is not placed within a code block.
If the name is a known Ruff option name, that link is not considered a
violation.
## Test Plan
Manual.
## Summary
Resolves#12321.
The physical-line-based `RUF054` checks for form feed characters that
are preceded by only tabs and spaces, but not any other characters,
including form feeds.
## Test Plan
`cargo nextest run` and `cargo insta test`.
## Summary
Now that Ruff provides a formatter, there is no need to rely on Black to
check that the docs are formatted correctly in
`check_docs_formatted.py`. This PR swaps out Black for the Ruff
formatter and updates inconsistencies between the two.
This PR will be a precursor to another PR
([branch](https://github.com/calumy/ruff/tree/format-pyi-in-docs)),
updating the `check_docs_formatted.py` script to check for pyi files,
fixing #11568.
## Test Plan
- CI to check that the docs are formatted correctly using the updated
script.
## Summary
<!-- What's the purpose of the change? What does it do, and why? -->
This is the implementation for the new rule of `pycodestyle (E204)`. It
follows the guidlines described in the contributing site, and as such it
has a new file named `whitespace_after_decorator.rs`, a new test file
called `E204.py`, and as such invokes the `function` in the `AST
statement checker` for functions and functions in classes. Linking #2402
because it has all the pycodestyle rules.
## Test Plan
<!-- How was it tested? -->
The file E204.py, has a `decorator` defined called wrapper, and this
decorator is used for 2 cases. The first one is when a `function` which
has a `decorator` is called in the file, and the second one is when
there is a `class` and 2 `methods` are defined for the `class` with a
`decorator` attached it.
Test file:
``` python
def foo(fun):
def wrapper():
print('before')
fun()
print('after')
return wrapper
# No error
@foo
def bar():
print('bar')
# E204
@ foo
def baz():
print('baz')
class Test:
# No error
@foo
def bar(self):
print('bar')
# E204
@ foo
def baz(self):
print('baz')
```
I am still new to rust and any suggestion is appreciated. Specially with
the way im using native ruff utilities.
---------
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
When using the autofixer for `Q000` it does not remove the backslashes
from quotes that no longer need escaping.
This new rule checks for such backslashes (regardless whether they come
from the autofixer or not) and can remove them.
fixes#8617
## Summary
The markdown documentation was present, but in the wrong place, so was
not displaying on the website. I moved it and added some references.
Related to #2646.
## Test Plan
`python scripts/check_docs_formatted.py`
## Summary
Fix CI (broken in #7496).
The code snippet was formatted as Black would format a stub file, but
the CI script doesn't know that (it assumes all code snippets are
non-stub files). Easier to ignore.
Sorry for breaking CI!
## Test Plan
`python scripts/check_docs_formatted.py`
Adds rule to convert type aliases defined with annotations i.e. `x:
TypeAlias = int` to the new PEP-695 syntax e.g. `type x = int`.
Does not support using new generic syntax for type variables, will be
addressed in a follow-up.
Added as part of pyupgrade — ~the code 100 as chosen to avoid collision
with real pyupgrade codes~.
Part of #4617
Builds on #5062
## Summary
This PR implements pycodestyle's E241 (tab after comma) and E242
(multiple whitespace after comma) lints.
These are marked as nursery rules like many other pycodestyle rules.
Refs #2402
## Test Plan
E24.py copied from pycodestyle.
## Summary
This is an error message only change to lead an implementor of a new
rule that has an unformatted or invalid bad example to the
right code.
## Test Plan
n/a
## Summary
Adds `PERF401` and `PERF402` mirroring `W8401` and `W8402` from
https://github.com/tonybaloney/perflint
Implementation is not super smart but should be at parity with upstream
implementation judging by:
https://github.com/tonybaloney/perflint/blob/c07391c17671c3c9d5a7fd69120d1f570e268d58/perflint/comprehension_checker.py#L42-L73
It essentially checks:
- If the body of a for-loop is just one statement
- If that statement is an `if` and the if-statement contains a call to
`append()` we flag `PERF401` and suggest a list comprehension
- If that statement is a plain call to `append()` or `insert()` we flag
`PERF402` and suggest `list()` or `list.copy()`
I've set the violation to only flag the first append call in a long
`if-else` statement for `PERF401`. Happy to change this to some other
location or make it multiple violations if that makes more sense.
## Test Plan
Fixtures were added with the relevant scenarios for both rules
## Issue Links
Refers: https://github.com/astral-sh/ruff/issues/4789
## Summary
Add documentation to the `D3XX` rules that check for issues with
docstring quotes. Related to #2646.
## Test Plan
`python scripts/check_docs_formatted.py`