A visitor that checks for a yield and then walks the expression can
dispatch on its kind twice. Inlining walk_expr lets the compiler combine
the checks and removes the stack copy of the classified expression on
recursive calls.
Leave inlining to the compiler with debug assertions enabled. At
opt-level=0, forcing the walker into the visitor enlarges its stack
frame enough to overflow the ty server thread on 2,000 nested additions.
Resolving a child ID loads the row pointer from NodeStorage. Preserve
the field order of Ast to keep NodeStorage at offset zero and the row
pointer close to the start of the header.
In the x86-64 profiling build, the load in
Arguments::as_single_positional moved from offset 168 to offset zero.
That shrank the instruction from seven bytes to three. The same load
occurs throughout the generated child accessors.
Every variant of a classified expression carries the same row and arena
pointers. Expose the generated conversions across crate boundaries so
the compiler can return those pointers directly in ExprKind::view.
Build expressions and statements for fixes in a destination arena.
Copy the children each fix retains into that arena before printing
the replacement syntax.
Forward the classified expression or statement to the walker and
pass the borrowed node through rule dispatch. Visit call arguments in
evaluation order when processing type definitions.
Start with a bounded reservation based on source length. When the row
buffer fills, use the observed node density to estimate how much space
the rest of the source will need.
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.
Store child IDs in the arena. Expose borrowed typed nodes, fields and
source-order lists through generated accessors. The walkers accept an
already classified expression or statement. Resolve child IDs with
checked indexing.
Check the stored number of arguments before matching a positional
argument list. Inspect the sole item when checking an ABC base or
metaclass, and count positionals only after identifying a setter.
Store positional and keyword arguments in one list for calls and class headers. Source-order visitors can walk the list directly, and evaluation-order visitors visit starred arguments before keywords.
Box keyword values so a positional argument does not carry the full size of a keyword entry.
Traverse source order when each parameter is checked independently,
and inspect roles directly when locating the positional-only slash.
Expression searches can check annotations and defaults in the same
written order, including for recovered signatures.
In `f(**{**(options := defaults)})`, the inner entry spans
`**(options := defaults)`. PIE804 can copy that slice into the call
directly, preserving the parentheses covered by #30629.
For a repeated key, the ends of adjacent entries give both boundaries
of the deletion, including any parentheses around the values.
## Summary
Today, ty panics on `class C(*(int, Generic[T, U]))` when `T` has a
default and `U` is required. To locate the error, ty looks for the
second base in the class header. The header contains a single starred
expression that expands to two bases.
This PR changes the diagnostic to use the source expression ty recorded
for each base during expansion, following #24695 and #24699. The
diagnostic now underlines `T, U` and reports that a required type
parameter cannot follow one with a default.
Added an mdtest that checks the diagnostic on the unpacked generic base.
## Summary
Today, PIE804 suggests rewriting `foo(**{**(options := defaults)})` as
`foo(**options := defaults)`. The edit drops the parentheses required
around the assignment expression, so `ruff check --fix` rejects it as
invalid syntax.
This PR changes PIE804 to preserve parentheses when copying the unpacked
value, using the same lookup as #11372. Ruff can then simplify the call
to `foo(**(options := defaults))`.
Added a regression fixture that failed before the change and now passes.
Summary
--
Fixes#27012 by finding the remaining places where we used rule codes
rather than also
including/preferring rule names.
Most of these places used `RuleSelector`s, so I added
`RuleSelector::name_and_code`, which uses
`Rule::name_and_code` for rule selectors and falls back on the existing
`prefix_and_code` for
non-rule selectors.
Test Plan
--
Updated snapshots
## Summary
This PR splits `mismatched-type-name` into eight independently
configurable warnings for `NewType`, functional enums, `TypeVar`,
`ParamSpec`, `TypeVarTuple`, `namedtuple`/`NamedTuple`, `TypedDict`, and
`TypeAliasType`.
~~This PR is actually quite a bit less breaking for users than other
rule splits we've done over the last few days. Because
`mismatched-type-name` is no longer the canonical name for _any_ rule on
this branch, we're able to make it a deprecated alias for _all_ the new
rules, meaning that it will continue to work on the CLI, in ty
configuration files, and ty suppression comments (we'll just emit a
warning diagnostic telling users to upgrade their configuration
file/suppression comment).~~
~~A more specific rule takes precedence over the deprecated name if both
are given within the same configuration table. On the command line, the
last matching flag wins (e.g. `--error=mismatched-enum-name
--ignore=mismatched-type-name` will suppress diagnostics for mismatched
enum names).~~
(After review from @MichaReiser, I backed out these changes. It's now a
somewhat breaking change, because the rule is now marked as "removed"
rather than "deprecated".)
As well as splitting the rule into eight separate rules, it also cleans
up our error recovery for several of these type factories. For example,
in the following snippet, we fail to recognize `T` as a `TypeVar` due to
bad error recovery from the non-literal name. But we don't really need a
nonliteral name in this case to determine that it's a validly
constructed TypeVar. It's probably a mistake, but there's no _soundness_
issue here:
```py
from typing import TypeVar
def f(x: str):
# main emits an error: `invalid-typevar`
# this PR emits a warning: `mismatched-typevar-name`
T = TypeVar(x)
def g(x: T) -> T:
return x
reveal_type(g(42)) # main: Todo. This PR: `Literal[42]`.
```
## Motivation
The main motivation here is that having a namedtuple or enum with the
"wrong name", for example, just seems much less "obviously wrong" than
having a typevar or paramspec with the "wrong name". The [ecosystem
report](https://github.com/astral-sh/ruff/pull/29957#issuecomment-6603964612)
is evidence of this. namedtuples and enums define runtime classes that
can be used in runtime contexts and instantiated; these constructs
aren't _just_ for type checkers.
TypeVars/ParamSpecs/TypeVarTuples/NewTypes/TypedDicts are all _only_ for
type checkers and there's no plausible reason why you'd want to give the
type a different name to the variable it's assigned to.
`mismatched-namedtuple-name` and `mismatched-enum-name` are therefore
now disabled by default, matching the default severity we already
settled on for `mistmatched-sentinel-name` in
https://github.com/astral-sh/ruff/pull/27206. `mismatched-typevar-name`,
`mismatched-paramspec-name`, `mismatched-typevartuple-name`,
`mismatched-enum-name`, `mismatched-namedtuple-name` and
`mismatched-newtype-name` all remain enabled by default with `warning`
severity.
When `**kwargs` is annotated with `P.kwargs`, the matching `*args`
annotation must be `P.args`. We emit a diagnostic on `P.args` as a
result -- but if you add a suppression comment to that line... a new
diagnostic pops up on `P.kwargs`! That's not how suppression comments
are really meant to work. This PR fixes that.
## Test plan
### Before (`aa46aae60`)
Adding `# ty: ignore[invalid-paramspec]` on the `*args` line moves the
diagnostic to `**kwargs`.
https://github.com/user-attachments/assets/50ee1858-8126-45bb-896f-2517b98950fe
### After (`684c14273`)
Adding the same ignore clears the diagnostic.
https://github.com/user-attachments/assets/0a7fc2b0-ffd0-49b4-87ae-714835b7421f