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.
Fixesastral-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]`.
## 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.
## 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
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`
## 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
## 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>
## 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.
## 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.
## 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
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
## 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>
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>
## 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