Commit Graph
30 Commits
Author SHA1 Message Date
Carl Meyer 367a5639d2 [ty] Fix conditional type and tuple annotations (#29987)
When `typing.Type` is imported with a fallback to the builtin `type`, ty
can reject a valid `Type[T]` annotation and infer `Unknown` for a
generic call that should return `int`. A `typing.Tuple` import with a
`tuple` fallback has the same problem.

Use the same annotation handling for each pair of spellings, including
when ty sees multiple possible bindings for the subscripted name.

Fixes astral-sh/ty#4907.

## Test plan

Checked the issue reproducer and a dictionary constructor call against
the `frozendict` 2.4.7 stub with Python 3.12 selected. The generic call
reveals `int`, and `frozendict(x)` reveals `frozendict[str, int]`.
2026-11-05 15:52:11 +01:00
Charlie Marsh 775c19b5c5 [ty] Preserve tuple shapes when slicing NewTypes (#29091)
## Summary

We now infer `tuple[int, str]` for slices of tuple-based `NewType`s like
this one:

```python
from typing import NewType

MyTuple = NewType("MyTuple", tuple[int, str, None])

def example(value: MyTuple) -> tuple[int, str]:
    return value[:2]
```

Previously, we fell back to the general `__getitem__` signature and
inferred `tuple[int | str | None, ...]`. We now apply tuple slicing to
the `NewType`'s concrete base, returning a plain tuple with the sliced
element types.

Closes https://github.com/astral-sh/ty/issues/4632.
2026-10-05 09:00:12 +02:00
Shunsuke Shibayama 11dc3e08bc [ty] Preserve recursive metadata in union transformations (#28497) 2026-09-12 13:30:53 +09: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
David Peter 7b3b6a0b8c [ty] Split ty_extensions into public and internal parts (#26586)
## Summary

Split `ty_extensions` into a public part (`ty_extensions`), and an
internal part (`ty_extensions._internal`). For now, move everything to
`_internal`, except for:

- `static_assert`
- `Unknown`
- `AlwaysTruthy`, `AlwaysFalsy`
- `Intersection`, `Not`
- `Top`, `Bottom`
- `JustFloat`, `JustComplex`
- `NamedTupleLike`
2026-07-07 02:32:21 -07:00
Shunsuke Shibayama beb85f3b5c [ty] improve variable-length tuple slicing (#26151)
## Summary

This PR implements variable-length tuple (VLT) slicing (previously not
implemented and fell back to `@Todo`).

What makes this more complicated than fixed-length tuple slicing is that
VLT is divided into a prefix part, variable part, and suffix part. While
the length of the prefix/suffix part is fixed, the length of the
variable part is not, so element type unioning is required depending on
the slice. Also, the tuple after slicing may be of fixed-length.

Consider the following VLT:

````
t: tuple[P0, P1, P2, *tuple[V, ...], S0, S1, S2]
````

Tuples after slicing can be classified by the sign of the step, whether
it is a non-empty range, and whether it can be represented as a fixed
length or not. The decision tree is:

```mermaid
flowchart TD
    A["VLT slicing"] --> B{"step == 0?"}
    B -->|Yes| Z["error"]
    B -->|No| C{"backward slice <br> (step < 0)?"}

    C -->|Yes| R["reverse tuple and bounds"]
    C -->|No| F["use tuple as-is"]
    R --> F

    F --> E{"always empty?"}
    E -->|Yes| Empty["Empty"]

    E -->|No| BP{"result length is fixed<br/>from front/back positions?"}
    BP -->|Yes| FixedFront["Fixed"]

    BP -->|No| M{"representable as one<br/>variable-length tuple?"}
    M -->|Yes| Mixed["Mixed"]

    M -->|No| H["Homogeneous"]
```

### Empty

```
t[5:2]: tuple[()]
```

### Fixed

The slice result can be expressed as a fixed length.

#### Exact

Since the range required by the slice completely fits within the
prefix/suffix, it can be represented as a fixed-length tuple.

```
t[:2]:  tuple[P0, P1]
t[-2:]: tuple[S1, S2]
```

#### Approximation

Since the total length required by the slice is shorter than the fixed
length (prefix length + suffix length), it can be represented as a fixed
length tuple, although it may contain elements of the variable part.

```
t[:4]: 
    if variable length == 0: tuple[P0, P1, P2, S0] 
    if variable length > 0:  tuple[P0, P1, P2, V]
=> tuple[P0, P1, P2, V | S0]
```

### Mixed

The slice result cannot be expressed as a fixed length.

#### Exact

It is possible to separate the fixed length part and variable length
part, and it can be expressed by cutting a part of the original VLT.

```
t[:]:   tuple[P0, P1, P2, *tuple[V, ...], S0, S1, S2]
t[1:]:  tuple[P1, P2, *tuple[V, ...], S0, S1, S2]
t[:-1]: tuple[P0, P1, P2, *tuple[V, ...], S0, S1]
```

#### Approximation

The prefix/suffix part and the variable part cannot be separated, and
some parts are fused.

```
t[4:]: 
    if variable length == 0: tuple[S1, S2] 
    if variable length == 1: tuple[S0, S1, S2] 
    if variable length == 2: tuple[V, S0, S1, S2] 
    ...
=> tuple[*tuple[V | S0, ...], S1, S2]
t[::2]: 
    if variable length == 0: tuple[P0, P2, S1] 
    if variable length == 1: tuple[P0, P2, S0, S2] 
    if variable length == 2: tuple[P0, P2, V, S1] 
    ...
=> tuple[P0, P2, *tuple[V | S0 | S1 | S2, ...]]
```

### Homogenous

Since none of the above patterns apply, it falls back to a homogenous
tuple that is the union of all elements.

```
t[2147483647::-1]: tuple[S2 | S1 | S0 | V | P2 | P1 | P0, ...]
```

## Caveat

This PR assumes that the type of the sliced ​​VLT will be a single tuple
type. However, sometimes a union of tuples is more precise than a single
tuple type:

```
t[:4]: tuple[P0, P1, P2, V | S0]
t[:5]: tuple[P0, P1, P2, V | S0, V | S0 | S1]
```

There is no problem with a single tuple type up to `t[:4]`. But in the
case of `t[:5]`, it is more precise to divide it into a union.

```
t[:5]: 
    if variable length == 0: tuple[P0, P1, P2, S0, S1] 
    if variable length == 1: tuple[P0, P1, P2, V, S0] 
    if variable length > 2: tuple[P0, P1, P2, V, V]
=> tuple[P0, P1, P2, S0, S1] | tuple[P0, P1, P2, V, S0 | V]
```

However, I think that such a union division should not be introduced at
this time. This is because ty does not yet have an implementation for
strict normalization, subtyping, and equality of tuples when they
contain unions (see astral-sh/ty#493). Until it is implemented, it is
safe to assume that a sliced ​​VLT type is still a single tuple type.

## Test Plan

mdtest updated
2026-06-25 19:23:06 -04:00
ReginaldandJelle Zijlstra dc79edf868 [ty] Add missing-type-argument lint rule (#25617)
## Summary

Add a new lint that detects generic types used without explicit type
parameters in annotations (e.g. `list` instead of `list[int]`).
This is equivalent to mypy's `--disallow-any-generics` and pyright's
`reportMissingTypeArgument`.

Can be enabled via:
```
    [rules]
    missing-type-argument = "warn"
```
It skips classes where all type parameters have PEP 696 defaults.

This should fix the following issue in ty:
- https://github.com/astral-sh/ty/issues/2442

## Test Plan

Added tests covering stdlib generics, user-defined generics, PEP 696
defaults, dotted names, function signatures, base classes, and variable annotations.

---------

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2026-06-05 13:07:10 -07:00
Carl Meyer 85207d3b76 [ty] sys.implementation.version is not sys.version_info (#25608)
## Summary

The way we special-cased `sys.version_info` accidentally also treated
`sys.implementation.version` as always being the Python version, but
this is only true for CPython. This was because both are instances of
`sys._version_info` class in typeshed, and our special-casing operated
at the level of that class.

Adjust things so that class is no longer treated as a singleton, and our
special-casing operates at the instance layer rather than the class
layer.

This also allows removing the now-unused
`ExpandedClassBaseEntry::Synthetic` variant, which allows simplifying
some diagnostic code.

Closes https://github.com/astral-sh/ty/issues/3640

## Testing

Added mdtest.
2026-06-03 20:54:38 -07:00
Charlie Marsh d7f1a5b72b [ty] Preserve slice-bound types in subscript inference (#25446)
## Summary

Prior to this change, when a slice bound was not an integer literal,
boolean literal, or `None`, we inferred a bare `slice[Any, Any, Any]`.
This erased enough information that subscription checking accepted
invalid slices even when `__getitem__` requires `SupportsIndex | None`
bounds:

```python
def get(values: list[int], start: float) -> list[int]:
    return values[start:]  # error: [invalid-argument-type]
```

(This may have been waiting on
https://github.com/astral-sh/ruff/commit/cb58704e085fe7e70d72842294575de47e47bead?)

We now preserve the inferred type of each provided slice bound in
`slice[...]`, so ordinary argument checking diagnoses invalid bounds for
built-in sequences and user-defined `__getitem__` methods with typed
slice parameters.

Closes https://github.com/astral-sh/ty/issues/3547.
2026-06-02 21:56:10 +02:00
cb58704e08 [ty] Sync vendored typeshed stubs (#23006)
Close and reopen this PR to trigger CI

---------

Co-authored-by: typeshedbot <>
Co-authored-by: David Peter <mail@david-peter.de>
2026-02-03 13:20:44 +01:00
Charlie Marsh ceabccb9a7 [ty] Add support for subscripts on intersections (#22654)
## Summary

Closes https://github.com/astral-sh/ty/issues/2072.
2026-01-24 14:59:35 +00:00
Charlie Marsh 8f040a06e6 [ty] Make infer_subscript_expression_types a method on Type (#22731)
## Summary

A refactor in anticipation of
https://github.com/astral-sh/ruff/pull/22654.
2026-01-23 16:47:29 +00:00
Alex Waygood 5a18e93d65 [ty] Make special cases for subscript inference exhaustive (#22035)
## Summary

Fixes https://github.com/astral-sh/ty/issues/2015. We weren't recursing
into the value of a type alias when we should have been.

There are situations where we should also be recursing into the
bounds/constraints of a typevar. I initially tried to do that as well in
this PR, but that seems... trickier. For now I'm cutting scope; this PR
does, however, add several failing tests for those cases.

## Test Plan

added mdtests
2026-01-15 00:18:54 +00:00
Simon Lamon c032e27566 [ty] Rename non-subscriptable error code to not-subscriptable (#22193) 2025-12-27 11:44:35 +00:00
Carl Meyer c2773b4c6f [ty] support type[tuple[...]] (#21652)
Fixes https://github.com/astral-sh/ty/issues/1649

## Summary

We missed this when adding support for `type[]` of a specialized
generic.

## Test Plan

Added mdtests.
2025-12-01 11:49:26 +01:00
Alex Waygood db0e921db1 [ty] Fix bug where ty would think all types had an __mro__ attribute (#20995) 2025-10-27 11:19:12 +00:00
Alex Waygood 2f3c7ad1fc [ty] Improve sys.version_info special casing (#19894) 2025-08-13 14:39:13 +01:00
Alex Waygood d2fbf2af8f [ty] Remove Type::Tuple (#19669) 2025-08-11 22:03:32 +01:00
Alex Waygood c401a6d86e [ty] Add failing tests for tuple subclasses (#19803) 2025-08-07 13:11:15 +00:00
Brent Westbrook a71513bae1 Fix tests on 32-bit architectures (#19652)
Summary
--

Fixes #19640. I'm not sure these are the exact fixes we really want, but
I
reproduced the issue in a 32-bit Docker container and tracked down the
causes,
so I figured I'd open a PR.

As I commented on the issue, the `goto_references` test depends on the
iteration
order of the files in an `FxHashSet` in `Indexed`. In this case, we can
just
sort the output in test code.

Similarly, the tuple case depended on the order of overloads inserted in
an
`FxHashMap`. `FxIndexMap` seemed like a convenient drop-in replacement,
but I
don't know if that will have other detrimental effects. I did have to
change the
assertion for the tuple test, but I think it should now be stable across
architectures.

Test Plan
--

Running the tests in the aforementioned Docker container
2025-07-31 08:52:19 -04:00
Alex Waygood 7b4103bcb6 [ty] Remove special casing for tuple addition (#19636) 2025-07-30 16:25:42 +00:00
Alex Waygood feaedb1812 [ty] Synthesize precise __getitem__ overloads for tuple subclasses (#19493) 2025-07-30 11:25:44 +00:00
Alex Waygood e44c489273 [ty] Fix false positives when subscripting an object inferred as having an Intersection type (#18920) 2025-06-24 18:39:02 +00:00
Suneet TipirneniandCarl Meyer ef8281b695 [ty] add support for mapped union and intersection subscript loads (#18846)
## Summary

Note this modifies the diagnostics a bit. Previously performing
subscript access on something like `NotSubscriptable1 |
NotSubscriptable2` would report the full type as not being
subscriptable:

```
[non-subscriptable] "Cannot subscript object of type `NotSubscriptable1 | NotSubscriptable2` with no `__getitem__` method"
```

Now each erroneous constituent has a separate error:

```
[non-subscriptable] "Cannot subscript object of type `NotSubscriptable2` with no `__getitem__` method"
[non-subscriptable] "Cannot subscript object of type `NotSubscriptable1` with no `__getitem__` method"
```

Closes https://github.com/astral-sh/ty/issues/625

## Test Plan

 mdtest

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
2025-06-23 16:38:01 +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
Alex Waygood d02c9ada5d [ty] Do not carry the generic context of Protocol or Generic in the ClassBase enum (#17989)
## Summary

It doesn't seem to be necessary for our generics implementation to carry
the `GenericContext` in the `ClassBase` variants. Removing it simplifies
the code, fixes many TODOs about `Generic` or `Protocol` appearing
multiple times in MROs when each should only appear at most once, and
allows us to more accurately detect runtime errors that occur due to
`Generic` or `Protocol` appearing multiple times in a class's bases.

In order to remove the `GenericContext` from the `ClassBase` variant, it
turns out to be necessary to emulate
`typing._GenericAlias.__mro_entries__`, or we end up with a large number
of false-positive `inconsistent-mro` errors. This PR therefore also does
that.

Lastly, this PR fixes the inferred MROs of PEP-695 generic classes,
which implicitly inherit from `Generic` even if they have no explicit
bases.

## Test Plan

mdtests
2025-05-22 21:37:03 -04:00
Alex Waygood 55df9271ba [ty] Understand homogeneous tuple annotations (#17998) 2025-05-12 22:02:25 -04:00
Alex Waygood d1bb10a66b [ty] Understand classes that inherit from subscripted Protocol[] as generic (#17832) 2025-05-09 17:39:15 +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