Commit Graph
36 Commits
Author SHA1 Message Date
Alex Waygood 0471c14d1f [ty] Rename unresolved-import to import-not-found (#29960)
## 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.
2026-11-03 19:53:26 +00:00
Carl Meyer 983a0aeece [ty] Preserve non-boolean comparison results for intersections (#28045)
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`.

Fixes astral-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.
2026-10-07 10:35:46 -07:00
Carl Meyer 7ae3af9e7d [ty] Preserve tuple shapes during concatenation (#28617)
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.
2026-09-15 13:24:13 -07:00
Charlie Marsh 1edf9fe2ca [ty] Report deprecations for overloads, operators, and properties (#28134)
## 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>
2026-09-02 13:33:49 +00:00
Charlie Marsh 9501d97402 [ty] Simplify numeric tower displays (e.g., float over int | float) (#27406)
## 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.
2026-08-05 21:29:29 +00:00
Ed Page 958efb1ff4 Vendor latest annotate-snippets (#27033)
## 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"
/>
2026-07-28 11:57:09 -05:00
Carl Meyer c48e681bb5 [ty] Fix reflected binary dispatch for runtime classes (#26623) 2026-07-08 08:27:53 -07:00
Charlie Marsh b31f66e9f8 [ty] Preserve uncertain reflected operator dispatch (#26474)
## 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
```
2026-07-01 13:08:46 +00:00
Charlie Marsh 1a7e4c8da1 [ty] Prefer reflected operators by runtime class (#26434)
## 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
```
2026-07-01 09:02:08 -04:00
eab0666c68 [ty] Sync vendored typeshed stubs (#25172)
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>
2026-05-20 10:11:49 +00:00
Charlie Marsh 01f55e1e18 [ty] Fix panic from recursive binary inference (#25189) 2026-05-15 23:14:42 +01:00
David Peter 08629b4954 [ty] Migrate more mdtests to inline snapshots (#24687)
## Summary

Migrate another ~20 test suites to inline snapshots (Friday batch).
2026-04-17 11:49:09 +02:00
David Peter 3e88931982 [ty] Migrate more mdtests to inline snapshots (#24681)
## Summary

Migrate another 20 test suites to inline snapshots.
2026-04-17 09:47:15 +02:00
Carl Meyer 590aab49fc [ty] stop unioning Unknown into types of un-annotated attributes (#24531) 2026-04-10 10:34:10 -07:00
Alex WaygoodandBrandt Bucher a2f546b254 [ty] Infer precise types for bit-shift operations on integer literals (#23301)
## 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>
2026-02-16 03:42:36 +00:00
Aria Desires 83168a1bb1 [ty] highlight special type syntax in hovers as xml (#22005)
## Summary

These types look better rendered as XML than python

## Test Plan

<img width="532" height="299" alt="Screenshot 2025-12-16 at 8 40 56 AM"
src="https://github.com/user-attachments/assets/42d9abfa-3f4a-44ba-b6b4-6700ab06832d"
/>
2025-12-16 14:20:35 +00:00
Alex Waygood 5a2aba237b [ty] Improve diagnostics for unsupported binary operations and unsupported augmented assignments (#21947)
## 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
2025-12-12 21:53:29 +00:00
Luca Chiodini 5a9d6a91ea [ty] Uniformly use "not supported" in diagnostics (#21916) 2025-12-11 15:03:55 +00:00
Alex Waygood 14fce0d440 [ty] Improve the display of various special-form types (#21775) 2025-12-03 21:19:59 +00:00
Alex Waygood 392a8e4e50 [ty] Improve diagnostics for unsupported comparison operations (#21737) 2025-12-02 19:58:45 +00:00
Carl Meyer 6b7adb0537 [ty] support PEP 613 type aliases (#21394)
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.
2025-11-20 17:59:35 -08:00
David Peter d2fe6347fb [ty] Rename UnionType to types.UnionType (#21262) 2025-11-03 22:06:56 +01:00
cafb96aa7a [ty] Sync vendored typeshed stubs (#20876)
Close and reopen this PR to trigger CI

---------

Co-authored-by: typeshedbot <>
Co-authored-by: David Peter <mail@david-peter.de>
2025-10-15 11:13:32 +02:00
Brent Westbrook 88c0ce3e38 Update default and latest Python versions for 3.14 (#20725)
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.
2025-10-07 12:23:11 -04:00
Alex Waygood 27b03a9d7b [ty] Remove special casing for string-literal-in-tuple __contains__ (#19642) 2025-07-31 11:28:03 +01:00
Alex Waygood 7b4103bcb6 [ty] Remove special casing for tuple addition (#19636) 2025-07-30 16:25:42 +00:00
Douglas CreagerandAlex Waygood ea812d0813 [ty] Homogeneous and mixed tuples (#18600)
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>
2025-06-20 18:23:54 -04:00
Alperen Keleş 932f941d15 [ty] fix binary expression inference between boolean literals and bool instances (#18663) 2025-06-17 18:02:40 +01:00
Andrew Gallant fc549bda94 [ty] Minor tweaks to "list all members" docs and tests (#18388)
Ref https://github.com/astral-sh/ruff/pull/18251#pullrequestreview-2881810681
2025-05-30 13:36:57 -04:00
David Peter e730f27f80 [ty] List available members for a given type (#18251)
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`.
2025-05-30 11:24:20 -04:00
Brandt Bucher 91b7a570c2 [ty] Implement Python's floor division semantics for Literal ints (#18249)
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`.

Fixes astral-sh/ty#481.
2025-05-22 10:42:29 -04:00
Douglas Creager 0fb94c052e [ty] Infer parameter specializations of generic aliases (#18021)
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
```
2025-05-12 22:12:44 -04:00
Alex Waygood 55df9271ba [ty] Understand homogeneous tuple annotations (#17998) 2025-05-12 22:02:25 -04:00
Alex Waygood 67cd94ed64 [ty] Add missing bitwise-operator branches for boolean and integer arithmetic (#17949) 2025-05-08 14:10:35 +01:00
Charlie Marsh a2e9a7732a Update class literal display to use <class 'Foo'> style (#17889)
## Summary

Closes https://github.com/astral-sh/ruff/issues/17238.
2025-05-06 20:11:25 -04:00
Micha Reiser b51c4f82ea Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00