## 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.
Comparisons on narrowed operands can return objects other than `bool`,
but intersection comparison inference intersected those results with
`bool`. This could turn a valid comparison into `Never` and hide errors
in code that remains reachable.
Resolve rich-comparison methods with the full intersection receiver so
non-boolean results and `Self` constraints are preserved. Keep boolean
simplifications for boolean results, respect reflected-method priority,
and use inherited `object` comparison methods only as fallbacks.
Preserve union alternatives within each intersection component's return
type, with a conservative union fallback when distributing them would
exceed the complexity limit. Retain definite equality and inequality
results for gradual operands narrowed to `None`.
Fixesastral-sh/ty#4380.
## Ecosystem report
Analyzed head: `9ea38ae03484b349136e8fb102c21fcb1938ef72`. Most changes
are expected off-by-default unsoundness warnings for gradual comparison
results. Preserving non-boolean results also removes false
unreachable-branch diagnostics and exposes existing downstream
annotation mismatches; reflected arithmetic becomes more precise. No
remaining ecosystem regressions were identified. [Full ecosystem
analysis](https://github.com/astral-sh/ruff/pull/28045#issuecomment-5898743526).
The current-head memory report and [CodSpeed
comparison](https://app.codspeed.io/astral-sh/ruff/runs/compare/6abc0e147d32f5eb9f94ffd2..6abc1f087d32f5eb9f95019d)
show no material performance regression.
Tuple concatenation currently discards element positions and tuple
length, so adding two fixed-length tuples produces a variable-length
tuple. Preserve the concatenated shape, including known prefixes and
suffixes around variable-length portions.
As with tuple slicing and iteration, this (unsoundly) assumes values
annotated as `tuple` behave like ordinary tuples, even though a subclass
could override `__add__` and/or `__radd__` incompatibly. Explicitly
known subclasses continue to use their operator methods.
Keep inference bounded by approximating large tuple combinations and
widening tuple lengths when their alternatives change between cycle
iterations. Stable tuple unions, including those supplied by collection
type context, retain their shapes. Some cycles with finite but changing
tuple lengths can still lose precision to ensure convergence.
Fixes https://github.com/astral-sh/ty/issues/3413.
## Ecosystem impact
- Removes tuple-shape false positives in Hydra-zen, Trio, Spark, and
SymPy, and two unsoundness warnings in pandas.
- Most other changes make existing diagnostics more precise. New errors
expose overly narrow tuple attributes and collection element types in
zope.interface and SymPy, including invariant dictionary key types.
- One new xarray false positive exposes existing dictionary-key
inference that incorrectly restricts keys to strings. This limitation is
left for separate work. Cycle widening also loses finite tuple
alternatives in Apprise, consolidating existing errors without changing
which operations are rejected.
[Full ecosystem
analysis](https://github.com/astral-sh/ruff/pull/28617#issuecomment-5687202176).
## Performance
The `many_tuple_assignments` microbenchmark increases from 141.1 ms to
159.7 ms in simulation and from 6.4 MB to 6.9 MB in memory. This is an
expected cost of preserving tuple shapes; the tradeoff is acceptable for
this change. See the [benchmark
report](https://github.com/astral-sh/ruff/pull/28617#issuecomment-5684963921).
## Test plan
- Mdtests cover fixed and empty tuples, variable-length prefixes and
suffixes, aliases, union operands, subclass overrides, and
`TypeVarTuple` concatenation with both typing syntaxes.
- Exercise ordinary and augmented concatenation, large unions with
custom operators, repeated doubling, and recursively growing local
variables and instance attributes.
- Preserve finite alternating tuple shapes and fixed-length tuple unions
supplied by an empty list's return context.
- Verify incompatible augmented assignment to `tuple[Any, Any]` and
instance-layout conflicts detected through concatenated `__slots__`.
- Unit tests cover tuple widening across cycle results, including equal
lengths, changing lengths, discarded alternatives, avoiding relation
queries, and `Never` elements.
## Summary
Previously, we missed deprecations on selected overloads, implicit
operator and `__call__` calls, and property accessors. This PR adds
support for reporting `deprecated` in those contexts, e.g.:
```python
from typing import overload
from typing_extensions import deprecated
@overload
@deprecated("Use a string")
def convert(value: int) -> str: ...
@overload
def convert(value: str) -> str: ...
def convert(value: int | str) -> str:
return str(value)
convert(1) # Before: no diagnostic. After: deprecated.
convert("one") # Before and after: no diagnostic.
```
This moves
[`directives_deprecated`](https://github.com/python/typing/blob/cf943ccbea5596ef969eec6e2260097ff697b7ba/conformance/tests/directives_deprecated.py)
from Partial to Pass.
<details>
<summary>Case analysis</summary>
Mypy 2.1.0, pyright 1.1.410, and pyrefly 1.3.0-dev.1 report all seven
required diagnostics in this conformance file. All comparisons
explicitly enable deprecation diagnostics. The differences below concern
additional cases in the regression tests.
We do not report a deprecated reflected operator when the left operand's
method accepts the operation. Pyright and pyrefly agree; mypy also warns
about the unused reflected method:
```python
from typing_extensions import deprecated
class Left:
def __add__(self, other: object) -> int:
return 0
class Right:
@deprecated("reflected addition")
def __radd__(self, other: object) -> int:
return 0
Left() + Right() # ty, pyright, pyrefly: OK; mypy: deprecated.
1 + Right() # All four: deprecated.
```
A deprecated getter still runs during augmented assignment, even when
the setter is not deprecated. We report that deprecation, as does
pyrefly. Pyright misses the getter during augmented assignment, while
mypy loses the getter's deprecation when a non-deprecated setter or
deleter is subsequently defined. Access through the class returns the
property object without calling the getter; we leave that access quiet,
as do mypy and pyrefly:
```python
from typing_extensions import deprecated
class OldGetter:
@property
@deprecated("old getter")
def value(self) -> int:
return 0
@value.setter
def value(self, value: int) -> None: ...
@value.deleter
def value(self) -> None: ...
value = OldGetter()
value.value # ty, pyright, pyrefly: deprecated; mypy: OK.
value.value = 1 # All four: OK.
value.value += 1 # ty, pyrefly: deprecated; mypy, pyright: OK.
OldGetter.value # ty, mypy, pyrefly: OK; pyright: deprecated.
```
When both an overload and its implementation are deprecated, we emit the
implementation's diagnostic once, as does pyright. Mypy and pyrefly emit
separate diagnostics for the implementation and selected overload.
Union alternatives that select the same deprecated function produce one
diagnostic. If an operation can invoke several distinct deprecated
functions, we combine their messages and annotate each source
declaration. Augmented assignment still reports deprecated getters and
setters separately, since it invokes both.
</details>
## Summary
PEP 484's numeric tower means we represent a `float` annotation as the
semantic union `int | float*` and `complex` as `int | float* |
complex*`. We previously rendered those internal unions directly, which
exposed an implementation detail in user-facing output:
```python
def f() -> float:
return 1
reveal_type(f()) # Before: int | float; after: float
```
This change recognizes complete numeric-tower unions during display and
renders them using their canonical annotation spelling, `float` or
`complex`.
The implementation does _not_ preserve explicit unions, e.g., if the
user writes out `int | float`, we still collapse that rather than trying
to preserve it, mostly for simplicity (as per
https://github.com/astral-sh/ty/issues/2184#issuecomment-4345219164).
Closes https://github.com/astral-sh/ty/issues/2184.
## Summary
This is a first step towards #20411. After this, we would work to either
upstream changes or adjust things within Ruff.
Performance improvements have already been upstreamed
(https://github.com/rust-lang/annotate-snippets-rs/pull/437,
https://github.com/rust-lang/annotate-snippets-rs/pull/440)
Re-implemented the following from the fork:
- Hidden snippets
(https://github.com/rust-lang/annotate-snippets-rs/issues/448)
- Renderer-level disabling of hyperlinks
- Jupyter cell indices
(https://github.com/rust-lang/annotate-snippets-rs/issues/449)
- Fixable mark
(https://github.com/rust-lang/annotate-snippets-rs/issues/450)
- `lineno_offset` (maybe not needed if #20648 is taken care of)
- anonymized lines affects origin
(https://github.com/rust-lang/annotate-snippets-rs/issues/451)
- cut indicator (maybe not needed if we can switch to unicode renderer)
Additional hacks to minimize behavior change include
- Adjust upstream's hidden severity behavior to match Ruff's
(https://github.com/rust-lang/annotate-snippets-rs/issues/452)
- How newlines are annotated
(https://github.com/rust-lang/annotate-snippets-rs/issues/454)
- To not indent for line numbers when none will be present
(https://github.com/rust-lang/annotate-snippets-rs/issues/453)
- Enabling `simd` by default
All of the above represents work that needs to be resolved to address
#20411.
`lib.name` hack is used to simplify maintenance with the assumption that
we'll continue working towards #20411.
Behavior changes that were included
- Tweak when implicit padding lines are present
- Fix Origin's line/column is now derived from the primary annotation
- Make consistent the rendering of primary/context annotations
- Fix snippets to show up for empty source (but may degrade #24528)
- Fix handling of term width
- Fix rendering of annotations between newline and first character
(granted, maybe the caller is wrong)
- Tweak multiline annotation markers
New or upcoming features annotate-snippets features that we will be able
to leverage by moving this forward
- `AnnotationKind::Visible` to improve the quality of built-in
code-folding if Ruff were to switch to it
- `DecorStyle::Unicode`
- *(upcoming)* No-graphiscs mode for screen readers (may also be useful
for AIs)
Deferred:
- Moving related `Group`s into the same `Report`
- Using `secondary_title` for any `Group` after the first in a `Report`
- Using a `Snippet`, instead of titles, for "virtual code" like
https://github.com/astral-sh/ruff/pull/27033#discussion_r3622727236
- Switching hidden snippets to `Origin`
## Test Plan
Within cpython, I ran
```console
$ hyperfine -i "../../ruff/target-baseline/release/ruff check" "../../ruff/target-update/release/ruff check" -N -m20 --output=null
Benchmark 1: ../../ruff/target-baseline/release/ruff check
Time (mean ± σ): 27.5 ms ± 1.1 ms [User: 33.0 ms, System: 108.4 ms]
Range (min … max): 25.0 ms … 31.7 ms 94 runs
Warning: Ignoring non-zero exit code.
Benchmark 2: ../../ruff/target-update/release/ruff check
Time (mean ± σ): 28.1 ms ± 2.0 ms [User: 33.4 ms, System: 105.7 ms]
Range (min … max): 24.8 ms … 38.9 ms 102 runs
Warning: Ignoring non-zero exit code.
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use t
he '--warmup' or '--prepare' options.
Summary
../../ruff/target-baseline/release/ruff check ran
1.03 ± 0.08 times faster than ../../ruff/target-update/release/ruff check
```
Before:
<img width="486" height="583" alt="image"
src="https://github.com/user-attachments/assets/2d94dabf-b28d-48c1-b17c-52aac59fd57e"
/>
After:
<img width="486" height="583" alt="image"
src="https://github.com/user-attachments/assets/b388ada3-e3d7-4ba0-b90b-1330eb81f4be"
/>
## Summary
This is a follow-up to #26434 that preserves both binary-operation
dispatch results when static operand types do not determine their exact
runtime classes.
For `left: Base` and `right: Child`, the runtime operands may be `Base +
Child`, where `Child.__radd__` has priority, or `Child + Child`, where
`Base.__add__` is called. We previously selected only the reflected
result from the static subtype relationship.
This change classifies reflected priority as never, possible, or
definite. Possible priority unions the normal and reflected call
bindings; definite priority remains precise for exact left runtime
classes such as the integer literal in `0 | Permission.READ`. Bounded
TypeVars and NewTypes use the same conservative distinction.
```python
from typing import Literal
class Base:
def __add__(self, other: object) -> Literal["base"]:
return "base"
class Child(Base):
def __radd__(self, other: object) -> Literal["child"]:
return "child"
def add(left: Base, right: Child) -> Literal["base", "child"]:
return left + right
```
## Summary
Python gives the right operand's reflected method priority when its
runtime class is a strict subclass of the left operand's runtime class
and it provides a different implementation.
Prior to #15161, this dispatch path only handled class-instance types,
so checking whether the right instance type was a subtype of the left
effectively modeled the runtime-class relationship. #15161 generalized
the path to literals and other value types but retained the same subtype
predicate. That is not equivalent for singleton types: an enum literal
is not a subtype of a specific integer literal even though its runtime
class can be a strict subclass of `int`.
This PR compares the operands' nominal runtime classes when deciding
reflected-method precedence. This lets a common `IntFlag` accumulator
pattern retain the enum type produced at runtime:
```python
from enum import IntFlag, auto
class Permission(IntFlag):
READ = auto()
WRITE = auto()
def permissions_for(editable: bool) -> Permission:
permissions = 0
permissions |= Permission.READ
if editable:
permissions |= Permission.WRITE
return permissions
```
Close and reopen this PR to trigger CI
---------
Co-authored-by: typeshedbot <>
Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
## Summary
Fixes https://github.com/astral-sh/ty/issues/517.
This PR revives https://github.com/astral-sh/ruff/pull/18329 /
https://github.com/astral-sh/ruff/pull/19281, but without the
controversial bit of those PRs (the new diagnostic). Rather than adding
a new diagnostic if an integer is left-shifted or right-shifted by a
negative number, we just ignore that and infer `int`. We can always add
a new diagnostic later, if we find consensus on it.
Along the way, Claude also discovered that
`reveal_type(-(-9223372036854775807 - 1))` currently reveals
`Literal[-9223372036854775808]` on `main`, which is... not correct. So
this PR fixes that too.
### Test Coverage
mdtests
Co-authored-by: Brandt Bucher <brandt@python.org>
## Summary
This PR takes the improvements we made to unsupported-comparison
diagnostics in https://github.com/astral-sh/ruff/pull/21737, and extends
them to other `unsupported-operator` diagnostics.
## Test Plan
Mdtests and snapshots
Refs https://github.com/astral-sh/ty/issues/544
## Summary
Takes a more incremental approach to PEP 613 type alias support (vs
https://github.com/astral-sh/ruff/pull/20107). Instead of eagerly
inferring the RHS of a PEP 613 type alias as a type expression, infer it
as a value expression, just like we do for implicit type aliases, taking
advantage of the same support for e.g. unions and other type special
forms.
The main reason I'm following this path instead of the one in
https://github.com/astral-sh/ruff/pull/20107 is that we've realized that
people do sometimes use PEP 613 type aliases as values, not just as
types (because they are just a normal runtime assignment, unlike PEP 695
type aliases which create an opaque `TypeAliasType`).
This PR doesn't yet provide full support for recursive type aliases
(they don't panic, but they just fall back to `Unknown` at the recursion
point). This is future work.
## Test Plan
Added mdtests.
Many new ecosystem diagnostics, mostly because we
understand new types in lots of places.
Conformance suite changes are correct.
Performance regression is due to understanding lots of new
types; nothing we do in this PR is inherently expensive.
Summary
--
Closes#19467 and also removes the warning about using Python 3.14
without
preview enabled.
I also bumped `PythonVersion::default` to 3.9 because it reaches EOL
this month,
but we could also defer that for now if we wanted.
The first three commits are related to the `latest` bump to 3.14; the
fourth commit
bumps the default to 3.10.
Note that this PR also bumps the default Python version for ty to 3.10
because
there was a test asserting that it stays in sync with
`ast::PythonVersion`.
Test Plan
--
Existing tests
I spot-checked the ecosystem report, and I believe these are all
expected. Inbits doesn't specify a target Python version, so I guess
we're applying the default. UP007, UP035, and UP045 all use the new
default value to emit new diagnostics.
We already had support for homogeneous tuples (`tuple[int, ...]`). This
PR extends this to also support mixed tuples (`tuple[str, str,
*tuple[int, ...], str str]`).
A mixed tuple consists of a fixed-length (possibly empty) prefix and
suffix, and a variable-length portion in the middle. Every element of
the variable-length portion must be of the same type. A homogeneous
tuple is then just a mixed tuple with an empty prefix and suffix.
The new data representation uses different Rust types for a fixed-length
(aka heterogeneous) tuple. Another option would have been to use the
`VariableLengthTuple` representation for all tuples, and to wrap the
"variable + suffix" portion in an `Option`. I don't think that would
simplify the method implementations much, though, since we would still
have a 2×2 case analysis for most of them.
One wrinkle is that the definition of the `tuple` class in the typeshed
has a single typevar, and canonically represents a homogeneous tuple.
When getting the class of a tuple instance, that means that we have to
summarize our detailed mixed tuple type information into its
"homogeneous supertype". (We were already doing this for heterogeneous
types.)
A similar thing happens when concatenating two mixed tuples: the
variable-length portion and suffix of the LHS, and the prefix and
variable-length portion of the RHS, all get unioned into the
variable-length portion of the result. The LHS prefix and RHS suffix
carry through unchanged.
---------
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This PR adds initial support for listing all attributes of
an object. It is exposed through a new `all_members`
routine in `ty_extensions`, which is in turn used to test
the functionality.
The purpose of listing all members is for code
completion. That is, given a `object.<CURSOR>`, we
would like to list all available attributes on
`object`.
Division works differently in Python than in Rust. If the result is
negative and there is a remainder, the division rounds down (instead of
towards zero). The remainder needs to be adjusted to compensate so that
`(lhs // rhs) * rhs + (lhs % rhs) == lhs`.
Fixesastral-sh/ty#481.
This updates our function specialization inference to infer type
mappings from parameters that are generic aliases, e.g.:
```py
def f[T](x: list[T]) -> T: ...
reveal_type(f(["a", "b"])) # revealed: str
```
Though note that we're still inferring the type of list literals as
`list[Unknown]`, so for now we actually need something like the
following in our tests:
```py
def _(x: list[str]):
reveal_type(f(x)) # revealed: str
```