Carry the typed IDs returned by node constructors through parsing and
recovery. Parse source-ordered argument and parameter lists directly.
Keep ownership of the tree with the parsed module.
## Summary
Today, Ruff represents root and nested configurations differently,
duplicates Python-version fallback logic, and applies overrides while
loading configurations.
This PR changes configuration resolution to share `ResolvedSettings`,
centralize fallbacks, and apply overrides when resolving settings. This
lets callers combine configurations before applying CLI or editor
overrides.
Testing: Resolver and CLI tests pass.
## Summary
Give the resolver's nested settings entries named fields, and rename
`PyprojectConfig` to `RootSettings` to reflect that it holds the
invocation's root settings and discovery strategy. Rename its
configuration-file path to `config_path` and document how the settings
apply to inputs. Configuration discovery and resolution behavior remain
unchanged.
Testing: Resolver tests pass and affected crates compile.
Summary
--
Seems like a bit of an oversight on my part, or maybe we opted to leave
this out until the feature
stabilized? But I've found myself repeatedly clicking into rule
implementations to retrieve this
information, and it doesn't seem too intrusive to include it in the
header.
Test Plan
--
The header now looks like this:
<img width="442" height="322" alt="image"
src="https://github.com/user-attachments/assets/0b77adfb-9335-4ca0-acdd-2555b4e326a8"
/>
and the category links to a filtered view of the rules table:
https://docs.astral.sh/ruff/rules/?category=suspicious
Summary
--
This PR extends our infrastructure for redirecting rule codes to
redirect rule names too, in
preparation for the many renames we have planned before stabilizing
human-readable names. As a proof
of concept, I also renamed
[`missing-return-type-undocumented-public-function` (`ANN201`)], which
seemed like a very clear rename target, having been accidentally renamed
in #3283.
The first commit contains the redirect changes, and the second commit
contains the `ANN201` rename,
along with the tests it allows us to exercise.
[`missing-return-type-undocumented-public-function` (`ANN201`)]:
https://docs.astral.sh/ruff/rules/missing-return-type-undocumented-public-function/
Test Plan
--
New CLI tests
## Summary
ty currently reports `unresolved-import` for both a missing module and a
missing member in a `from` import. But the word "unresolved" is heavily
overloaded in Python, and has been reported by users as being confusing
(see https://github.com/astral-sh/ty/issues/2471). This PR renames the
rule to `import-not-found`, which describes the lookup failure in either
case.
Configuration, command-line flags, and ignore comments accept either
spelling. If several flags set this rule’s severity using different
spellings, the last flag wins. Existing baseline entries still match,
and rewriting a baseline uses the new name. Links to the old rule
heading still work.
## Summary
The generated `overrides.analysis` examples omit the `[[overrides]]`
declaration, so copying them into a configuration file produces a TOML
parsing error. We now include the array declaration and `include =
["src"]` in both the `pyproject.toml` and `ty.toml` examples, matching
the existing rule override examples.
Closes https://github.com/astral-sh/ty/issues/3994.
This PR replaces #25027. It adds an opt-in check for imports of
installed distributions that the current project does not declare
directly in `pyproject.toml`.
## Overview
The check uses ty's existing uv workspace integration (`TY_UV=1`) to
call `uv workspace metadata --frozen --active`. uv now provides module
ownership without synchronizing the environment, so ty can read the
metadata without installing packages or updating `uv.lock`.
The metadata provides:
1. Each workspace project's runtime and optional dependencies.
2. Dependencies declared in dependency groups.
3. Importable modules and their owning distributions, including source
paths for editable installs.
During import inference, ty maps each resolved import to its
distribution and compares it against the importing project's direct
declarations. If the declaration is missing, ty emits
`missing-direct-dependency`, naming the distribution to add. Each
distribution is reported once per file, at its first unsuppressed
import.
The rule is disabled by default and requires an existing, synchronized
environment. Unavailable or incompatible dependency metadata disables
the check; rejected dependency metadata produces a debug message.
## Nuances
- Editable installs may lack entries in uv's module-owner map. ty uses
their source paths to attribute resolved imports.
- Files identified as package code require runtime or optional
dependencies. Non-package files, such as tests and scripts, can also use
dependency groups, but those groups' transitive dependencies still need
direct declarations.
- For `from package import child`, a resolved child module is checked
against its own distribution rather than the parent's. Imports with
unknown or ambiguous ownership are skipped.
- Ownership metadata is used only when the explicitly selected Python
environment matches uv's environment.
- The current integration covers directory checks. Explicit file
arguments, `--config-file`, and standalone PEP 723 scripts do not use
the workspace's dependency declarations.
The schema and rule documentation are regenerated. Most of the
rule-documentation diff consists of updated source-line links.
Summary
--
This PR allows adding rules that only belong to categories and only have
names rather than requiring
a linter group and code.
The first non-empty commit uses `Rule::name` instead of
`Rule::noqa_code` throughout the linter tests, causing a huge number of
file renames without changes.
The second commit updates `FixTable` from a mapping of `SecondaryCode ->
(name, fix_count)` to
`DiagnosticId -> (Option<SecondaryCode>, fix_count)`. This doesn't
really change its usage but
involves minor diffs to a substantial number of lines. It also allows us
to drop some of the special
`hashbrown` handling and the `hashbrown` dependency in `ruff_linter`,
which is nice.
The third commit contains the bulk of the change, including the macro
changes needed to support
`map_codes` entries like that for the `pytest-fixture-autouse` rule in
the fourth commit:
```rust
() => rules::ruff::rules::PytestFixtureAutouse,
```
These changes are pretty mechanical overall, just handling cases where
the linter or noqa code could
be missing, which fortunately seemed pretty straightforward in every
case.
The fourth commit takes advantage of these changes to reinstate `RUF076`
as our first purely
`pedantic` rule. I'm happy to drop that if it's too controversial or
opens too many questions about
the previously-removed rule and its code, but I thought it was nice to
have a motivating example. We
moved this rule from `preview` to `removed` because it was too pedantic
for the `RUF` category, even
in preview, so it's exactly the kind of rule we can support now with the
categories.
Finally, the fifth commit updates the contributing docs to reflect that
linters and codes are
optional. This may need to be adjusted if we back out the `RUF076`
change.
Test Plan
--
Existing tests, plus a few new ones for `pytest-fixture-autouse`
## Summary
- Use Rust 1.96's stabilized `assert_matches!` and
`debug_assert_matches!` macros throughout Ruff and ty.
- Include mismatched values in assertion failures and remove redundant
manual debug formatting.
- Preserve existing assertions for values that do not implement `Debug`.
Summary
--
Addresses
https://github.com/astral-sh/ruff/pull/27666#discussion_r3801704517,
where we want to
start using "group" to refer to secondary categories. This type is
already referred to as `status`
in the user-facing JSON output, so this change aligns the internal type
name with that usage.
Test Plan
--
Existing tests
Summary
--
I saw this suggestion on [Discord]:
> Would it make sense to put a mark in the full rule page for rules that
are selected by default? I
feel like that would be very useful in seeing for example what rules are
not selected from a group
like UP by default. The extra "default rules" page doesn't help with
that, for example.
> Like a blue dot, maybe in front of the rules in the table.
This PR implements this suggestion with a couple of modifications. I
included a check mark instead of a blue dot and as an additional
indicator at the end of each row, near the preview, fix, and other
indicators, rather than at the start.
Test Plan
--
Local build:
<img width="569" height="237" alt="image"
src="https://github.com/user-attachments/assets/1a6db243-ac2c-4b53-a378-541467a6018d"
/>
[Discord]:
https://discord.com/channels/1039017663004942429/1070132471699607623/1536942546972839957
This PR stabilizes the expanded default rule set currently in preview
and adds a new generated `Default Rules` page to the docs to avoid
needing to include a lengthy selector block in multiple places.
Summary
--
This PR adds a new lint rule for TOML config files that checks for the
use of rule codes as
selectors and offers a fix to replace them with human-readable names. At
a high-level, the
implementation just deserializes a TOML config into a stripped down
version of our normal
configuration types and then checks each of its `Spanned` selector
entries. Unless I oversimplified
this too much, it didn't end up being as tricky as I thought. The main
tricky part was adding basic
support for fixes in TOML files, but even this was pretty
straightforward.
Codex was concerned about calling this a safe fix because applying the
fix with `--preview`, or
another temporary way of enabling preview, would break the config if
preview isn't always enabled.
That seemed to go beyond our usual fix safety policy to me, but I'm also
happy to mark the fix as
unsafe if that's preferable.
Test Plan
--
New tests covering all of the expected selectors and new CLI tests to
make sure that `ruff.toml`
files get linted and respect various ways of suppressing fixes.
Previously we only linted
`pyproject.toml` files for `RUF200` and bypassed some of the checks in
`LintContext`. I left the
change to use `LintContext` as a separate commit since it also affects
`RUF200` and I briefly
considered splitting it off into a separate PR.
<!--
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?
- Does this PR follow our AI policy
(https://github.com/astral-sh/.github/blob/main/AI_POLICY.md)?
-->
## Summary
<!-- What's the purpose of the change? What does it do, and why? -->
This removes the `Preview` rule status from ty documentation, and
"promotes" the three rules that were previously marked as preview to
stable. Unlike ruff, ty does not actually have a preview mode: all rules
are always available[^1]. As such the preview status was misleading.
[^1]: Although of course you can configure their
[level](https://docs.astral.sh/ty/rules/#rule-levels)).
## Test Plan
This change only affects our doc generation tooling (not the observable
behavior of the type checker); I have modified our tests for that
tooling accordingly.
<!-- How was it tested? -->
Adds `*.md` to the list of default globs in preview mode.
Adds a simple filter to the `check` CLI command to exclude file types
that aren't
supported for linting before checking if the set of resolved paths is
empty, preserving
existing behavior of warning "No Python files found under the given
path(s)" even if
there are markdown files present and preview mode is enabled.
Fixes#3792
Adds initial support for formatting Python code blocks inside Markdown
files.
- Adds `Markdown` source types/kinds
- Maps `.md` file extension to `Markdown` by default
- Uses simple regex adapted from blacken-docs to find and format fenced
python code blocks
- Dedents contents before formatting, and reapplies indent from fenced
<code>```py</code> header
- Selects `Python` vs `Stub` options based on language label on code
block
- Silently skips formatting for any code block with syntax errors or
that produce formatting errors.
- CLI tests formatting via both stdin and from filesystem
- Requires running with `--preview`, and otherwise emits formatting
error when given a markdown file
- Requires a user to `extend-include = ["**/*.md"]` if they want to
format markdown files by default
Limitations:
- Returns a formatting error if run with a range of any sort
- Ignores implicit code blocks (no code fence)
- Doesn't yet support `~~~` fences, arbitrary fence lengths, or code
blocks inside blockquotes
Issue #3792
## Summary
Fixes https://github.com/astral-sh/ty/issues/1885.
It wasn't obvious to me that there was a deliberate order to the way
these rules were listed in our reference docs -- it looked like it was
_nearly_ alphabetical, but not quite. I think it's simpler if we just
list them in alphabetical order.
## Test Plan
The output from running `cargo dev generate-all` (committed as part of
this PR) looks correct!
Summary
--
Inspired by #20859, this PR adds the version a rule was added, and the
file and line where it was defined, to `ViolationMetadata`. The file and
line just use the standard `file!` and `line!` macros, while the more
interesting version field uses a new `violation_metadata` attribute
parsed by our `ViolationMetadata` derive macro.
I moved the commit modifying all of the rule files to the end, so it
should be a lot easier to review by omitting that one.
As a curiosity and a bit of a sanity check, I also plotted the rule
numbers over time:
<img width="640" height="480" alt="image"
src="https://github.com/user-attachments/assets/75b0b5cc-3521-4d40-a395-8807e6f4925f"
/>
I think this looks pretty reasonable and avoids some of the artifacts
the earlier versions of the script ran into, such as the `rule`
sub-command not being available or `--explain` requiring a file
argument.
<details><summary>Script and summary data</summary>
```shell
gawk --csv '
NR > 1 {
split($2, a, ".")
major = a[1]; minor = a[2]; micro = a[3]
# sum the number of rules added per minor version
versions[minor] += 1
}
END {
tot = 0
for (i = 0; i <= 14; i++) {
tot += versions[i]
print i, tot
}
}
' ruff_rules_metadata.csv > summary.dat
```
```
0 696
1 768
2 778
3 803
4 822
5 848
6 855
7 865
8 893
9 915
10 916
11 924
12 929
13 932
14 933
```
</details>
Test Plan
--
I built and viewed the documentation locally, and it looks pretty good!
<img width="1466" height="676" alt="image"
src="https://github.com/user-attachments/assets/5e227df4-7294-4d12-bdaa-31cac4e9ad5c"
/>
The spacing seems a bit awkward following the `h1` at the top, so I'm
wondering if this might look nicer as a footer in Ruff. The links work
well too:
- [v0.0.271](https://github.com/astral-sh/ruff/releases/tag/v0.0.271)
- [Related
issues](https://github.com/astral-sh/ruff/issues?q=sort%3Aupdated-desc%20is%3Aissue%20is%3Aopen%20airflow-variable-name-task-id-mismatch)
- [View
source](https://github.com/astral-sh/ruff/blob/main/crates%2Fruff_linter%2Fsrc%2Frules%2Fairflow%2Frules%2Ftask_variable_name.rs#L34)
The last one even works on `main` now since it points to the
`derive(ViolationMetadata)` line.
In terms of binary size, this branch is a bit bigger than main with
38,654,520 bytes compared to 38,635,728 (+20 KB). I guess that's not
_too_ much of an increase, but I wanted to check since we're generating
a lot more code with macros.
---------
Co-authored-by: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com>
## Summary
Looks like an oversight at some point that led to two identical globals,
the one in `ty_project` just calls `ty_python_semantic::register_lints`.