mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
[ty] Return Option<TupleType>
from infer_tuple_type_expression
(#19735)
## Summary This PR reduces the virality of some of the `Todo` types in `infer_tuple_type_expression`. Rather than inferring `Todo`, we instead infer `tuple[Todo, ...]`. This reflects the fact that whatever the contents of the slice in a `tuple[]` type expression, we would always infer some kind of tuple type as the result of the type expression. Any tuple type should be assignable to `tuple[Todo, ...]`, so this shouldn't introduce any new false positives; this can be seen in the ecosystem report. As a result of the change, we are now able to enforce in the signature of `Type::infer_tuple_type_expression` that it returns an `Option<TupleType<'db>>`, which is more strongly typed and expresses clearly the invariant that a tuple type expression should always be inferred as a `tuple` type. To enable this, it was necessary to refactor several `TupleType` constructors in `tuple.rs` so that they return `Option<TupleType>` rather than `Type`; this means that callers of these constructor functions are now free to either propagate the `Option<TupleType<'db>>` or convert it to a `Type<'db>`. ## Test Plan Mdtests updated.
This commit is contained in:
parent
e4d6b54a16
commit
bc6e8b58ce
14 changed files with 156 additions and 142 deletions
|
@ -18,5 +18,5 @@ def append_int(*args: *Ts) -> tuple[*Ts, int]:
|
|||
return (*args, 1)
|
||||
|
||||
# TODO should be tuple[Literal[True], Literal["a"], int]
|
||||
reveal_type(append_int(True, "a")) # revealed: @Todo(PEP 646)
|
||||
reveal_type(append_int(True, "a")) # revealed: tuple[@Todo(PEP 646), ...]
|
||||
```
|
||||
|
|
|
@ -17,6 +17,7 @@ Alias: TypeAlias = int
|
|||
def f(*args: Unpack[Ts]) -> tuple[Unpack[Ts]]:
|
||||
reveal_type(args) # revealed: tuple[@Todo(`Unpack[]` special form), ...]
|
||||
reveal_type(Alias) # revealed: @Todo(Support for `typing.TypeAlias`)
|
||||
return args
|
||||
|
||||
def g() -> TypeGuard[int]: ...
|
||||
def i(callback: Callable[Concatenate[int, P], R_co], *args: P.args, **kwargs: P.kwargs) -> R_co:
|
||||
|
|
|
@ -59,7 +59,7 @@ reveal_type(d) # revealed: tuple[tuple[str, str], tuple[int, int]]
|
|||
reveal_type(e) # revealed: tuple[str, ...]
|
||||
|
||||
reveal_type(f) # revealed: tuple[str, *tuple[int, ...], bytes]
|
||||
reveal_type(g) # revealed: @Todo(PEP 646)
|
||||
reveal_type(g) # revealed: tuple[@Todo(PEP 646), ...]
|
||||
|
||||
reveal_type(h) # revealed: tuple[list[int], list[int]]
|
||||
reveal_type(i) # revealed: tuple[str | int, str | int]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue