mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
[ty] Add tests for nested generic functions (#20631)
## Summary Add two simple tests that we recently discussed with @dcreager. They demonstrate that the `TypeMapping::MarkTypeVarsInferable` operation really does need to keep track of the binding context. ## Test Plan Made sure that those tests fail if we create `TypeMapping::MarkTypeVarsInferable(None)`s everywhere.
This commit is contained in:
parent
1c08f71a00
commit
130a794c2b
2 changed files with 15 additions and 0 deletions
|
@ -464,6 +464,7 @@ def f(x: str):
|
|||
from typing import TypeVar, overload
|
||||
|
||||
T = TypeVar("T")
|
||||
S = TypeVar("S")
|
||||
|
||||
def outer(t: T) -> None:
|
||||
def inner(t: T) -> None: ...
|
||||
|
@ -479,6 +480,13 @@ def overloaded_outer(t: T | None = None) -> None:
|
|||
|
||||
if t is not None:
|
||||
inner(t)
|
||||
|
||||
def outer(t: T) -> None:
|
||||
def inner(inner_t: T, s: S) -> tuple[T, S]:
|
||||
return inner_t, s
|
||||
reveal_type(inner(t, 1)) # revealed: tuple[T@outer, Literal[1]]
|
||||
|
||||
inner("wrong", 1) # error: [invalid-argument-type]
|
||||
```
|
||||
|
||||
## Unpacking a TypeVar
|
||||
|
|
|
@ -474,6 +474,13 @@ def overloaded_outer[T](t: T | None = None) -> None:
|
|||
|
||||
if t is not None:
|
||||
inner(t)
|
||||
|
||||
def outer[T](t: T) -> None:
|
||||
def inner[S](inner_t: T, s: S) -> tuple[T, S]:
|
||||
return inner_t, s
|
||||
reveal_type(inner(t, 1)) # revealed: tuple[T@outer, Literal[1]]
|
||||
|
||||
inner("wrong", 1) # error: [invalid-argument-type]
|
||||
```
|
||||
|
||||
## Unpacking a TypeVar
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue