Skip to content

Commit 222574a

Browse files
authored
Expand the default rule set (#23385)
## Summary This PR adds the new default rule set in preview. This ended up being pretty non-invasive because the `DEFAULT_SELECTORS` are only used in one place where `preview` isn't set to the default value of `false`. I've currently listed each rule with a separate `RuleSelector`, which I generated with the script below. I thought about trying to be more clever by finding the smallest set of prefix selectors that yield the same rule set, but I figured this would be the easiest way to add and remove rules in the future anyway. <details><summary>Script</summary> <p> ```py import json import subprocess import tomllib from pathlib import Path from string import ascii_uppercase RULES = { rule["code"]: rule["linter"] for rule in json.loads( subprocess.run( ["ruff", "rule", "--all", "--output-format=json"], check=True, text=True, capture_output=True, ).stdout ) } for code, linter in RULES.items(): if linter == "Ruff-specific rules": RULES[code] = "Ruff" def kebab_to_pascal(s: str) -> str: return "".join(part.title() for part in s.split("-")) rules = tomllib.loads(Path("proposal.toml").read_text())["lint"]["select"] for rule in rules: linter = kebab_to_pascal(RULES[rule]) suffix = rule.lstrip(ascii_uppercase) prefix = "_" match linter: case "Flake8Comprehensions": suffix = suffix.removeprefix("4") case "Pycodestyle": prefix = "" suffix = rule case "Flake8Gettext": linter = "Flake8GetText" case "Pep8Naming": linter = "PEP8Naming" case "Pylint": prefix = "" suffix = rule.removeprefix("PL") case "Flake8Debugger": suffix = suffix.removeprefix("10") print( " " * 4, f"RuleSelector::rule(RuleCodePrefix::{linter}(" f"codes::{linter}::{prefix}{suffix}))," f" // {rule}", sep="", ) ``` </p> </details> ## Test Plan A new CLI test showing the preview default rules. I filtered down the snapshot to just the `linter.rules.enabled` section, which isn't strictly necessary but was a lot shorter. I also tested manually in VS Code to make sure I didn't miss wiring up the preview defaults in the server: <img width="681" height="340" alt="image" src="https://github.com/user-attachments/assets/9f7a0cd4-968e-40d6-abf0-dfbfa496531d" /> I also tested in the playground.
1 parent 1465b5d commit 222574a

File tree

7 files changed

+878
-7
lines changed

7 files changed

+878
-7
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ ruff check --config "lint.per-file-ignores = {'some_file.py' = ['F841']}"
300300
```
301301

302302
To opt in to the latest lint rules, formatter style changes, interface updates, and more, enable
303-
[preview mode](https://docs.astral.sh/ruff/rules/) by setting `preview = true` in your configuration
303+
[preview mode](https://docs.astral.sh/ruff/preview/) by setting `preview = true` in your configuration
304304
file or passing `--preview` on the command line. Preview mode enables a collection of unstable
305305
features that may change prior to stabilization.
306306

@@ -311,7 +311,7 @@ for more on the linting and formatting commands, respectively.
311311

312312
<!-- Begin section: Rules -->
313313

314-
**Ruff supports over 800 lint rules**, many of which are inspired by popular tools like Flake8,
314+
**Ruff supports over 900 lint rules**, many of which are inspired by popular tools like Flake8,
315315
isort, pyupgrade, and others. Regardless of the rule's origin, Ruff re-implements every rule in
316316
Rust as a first-party feature.
317317

@@ -322,6 +322,12 @@ stylistic rules that overlap with the use of a formatter, like `ruff format` or
322322
If you're just getting started with Ruff, **the default rule set is a great place to start**: it
323323
catches a wide variety of common errors (like unused imports) with zero configuration.
324324

325+
In [preview](https://docs.astral.sh/ruff/preview/), Ruff enables an expanded set of default rules
326+
that includes rules from the `B`, `UP`, and `RUF` categories, as well as many more. If you give the
327+
new defaults a try, feel free to leave feedback in the [GitHub
328+
discussion](https://github.com/astral-sh/ruff/discussions/23203), where you can also find the new
329+
rule set listed in full.
330+
325331
<!-- End section: Rules -->
326332

327333
Beyond the defaults, Ruff re-implements some of the most popular Flake8 plugins and related code

0 commit comments

Comments
 (0)