Skip to content

Commit eedfc62

Browse files
authored
[ty] Propagate outer type context through cast calls (#28855)
1 parent ceaa6a0 commit eedfc62

2 files changed

Lines changed: 185 additions & 5 deletions

File tree

‎crates/ty_python_semantic/resources/mdtest/directives/cast.md‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,127 @@ def f(x: RecursiveAlias):
102102
cast(RecursiveAlias, x) # error: [redundant-cast]
103103
```
104104

105+
The target type of a cast does not provide type context for its value. In the following example,
106+
inferring `[42]` using the cast's target, `list[object]`, would make the cast appear redundant.
107+
However, removing the cast changes the loop variable's type from `object` to `Literal[42]`, so
108+
reporting `redundant-cast` would be incorrect here.
109+
110+
```py
111+
for x in cast(list[object], [42]): # no redundant-cast diagnostic
112+
reveal_type(x) # revealed: object
113+
114+
for x in [42]: # no diagnostic
115+
reveal_type(x) # revealed: Literal[42]
116+
```
117+
118+
## Outer type context
119+
120+
The outer type context of the cast() call passes through to the casted value argument. A list
121+
literal therefore retains its literal element type when passed to a parameter that expects it, even
122+
through an intervening cast. The cast is redundant in this case.
123+
124+
```py
125+
from typing import Literal, cast
126+
from typing_extensions import cast as extension_cast
127+
128+
def f(x: list[Literal["foo"]]) -> None: ...
129+
130+
f(["foo"]) # no diagnostic
131+
f(cast(list[Literal["foo"]], ["foo"])) # error: [redundant-cast]
132+
f(cast(val=["foo"], typ=list[Literal["foo"]])) # error: [redundant-cast]
133+
f(extension_cast(list[Literal["foo"]], ["foo"])) # error: [redundant-cast]
134+
```
135+
136+
Annotated assignments and return types also supply type context through a cast:
137+
138+
```py
139+
items: list[Literal["foo"]] = cast(list[Literal["foo"]], ["foo"]) # error: [redundant-cast]
140+
141+
def make_items() -> list[Literal["foo"]]:
142+
return cast(list[Literal["foo"]], ["foo"]) # error: [redundant-cast]
143+
```
144+
145+
A value incompatible with the outer context still produces a disjoint-cast diagnostic:
146+
147+
```py
148+
f(cast(list[Literal["foo"]], [42])) # error: [disjoint-cast]
149+
```
150+
151+
The outer context can also provide the `TypedDict` type of a dictionary literal:
152+
153+
```py
154+
from typing import TypedDict
155+
156+
class Item(TypedDict):
157+
name: str
158+
159+
def accept_item(item: Item) -> None: ...
160+
161+
accept_item(cast(Item, {"name": "foo"})) # error: [redundant-cast]
162+
```
163+
164+
## Outer type context with diagnostics
165+
166+
The outer context is an inference hint for the value passed to `cast()`. If inference with that
167+
context produces diagnostics, we discard the attempt and infer the value without the outer context.
168+
(In the common case, the first attempt will *usually* be discarded: most casts are not redundant!)
169+
Casts of incomplete or incompatible `TypedDict` literals therefore do not cause us to emit
170+
`redundant-cast`.
171+
172+
We do not yet detect disjointness between `dict` and `TypedDict` types. Adding that support may
173+
cause some of these casts to emit `disjoint-cast` in the future:
174+
175+
```py
176+
from typing import TypedDict, cast
177+
from typing_extensions import cast as extension_cast
178+
179+
class Item(TypedDict):
180+
name: str
181+
count: int
182+
183+
empty: Item = cast(Item, {}) # no redundant-cast diagnostic
184+
partial: Item = cast(Item, {"name": "foo"}) # no redundant-cast diagnostic
185+
extra: Item = cast(Item, {"name": "foo", "count": 1, "extra": True}) # no redundant-cast diagnostic
186+
incompatible: Item = cast(Item, {"name": "foo", "count": "one"}) # no redundant-cast diagnostic
187+
unpacked: Item = cast(Item, {**{"name": "foo", "count": 1}}) # no redundant-cast diagnostic
188+
189+
def accept_item(item: Item) -> None: ...
190+
191+
accept_item(cast(val={}, typ=Item)) # no redundant-cast diagnostic
192+
accept_item(extension_cast(Item, {})) # no redundant-cast diagnostic
193+
194+
def make_item() -> Item:
195+
return cast(Item, {}) # no redundant-cast diagnostic
196+
```
197+
198+
Type context can also introduce diagnostics in lambda bodies. Here, using the outer context would
199+
make the lambda parameter an `int` and report `call-non-callable`. We therefore fall back to the
200+
inference attempt that does not use the type context, and emit neither `redundant-cast` nor
201+
`call-non-callable`:
202+
203+
```py
204+
from typing import Callable
205+
206+
def callback() -> Callable[[int], str]:
207+
return cast(Callable[[int], str], lambda value: value()) # no diagnostic
208+
```
209+
210+
Diagnostics that occur without the outer context are still reported, once:
211+
212+
```py
213+
# error: [unresolved-reference]
214+
item: Item = cast(Item, {"name": undefined, "count": 1})
215+
216+
def make_item_with_error(name: str) -> Item:
217+
return cast( # no diagnostic
218+
Item,
219+
{
220+
"name": cast(str, name), # error: [redundant-cast]
221+
"count": undefined, # error: [unresolved-reference]
222+
},
223+
)
224+
```
225+
105226
## Redundant casts of tuple classes with unknown elements
106227

107228
A tuple class with an `Unknown` element is not fully static, even when its other element is `object`

‎crates/ty_python_semantic/src/types/infer/builder.rs‎

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6503,17 +6503,54 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
65036503
return infer_expression(self, tcx);
65046504
};
65056505

6506+
self.infer_with_type_context_fallback(peer_tcx, tcx, infer_expression)
6507+
}
6508+
6509+
/// Tries `tcx`, retrying with `fallback_tcx` if inference reports diagnostics other than
6510+
/// `allowed_lint`. Only the chosen attempt's types and diagnostics are retained.
6511+
///
6512+
/// For a cast's value, the outer context can introduce errors by supplying parameter types to
6513+
/// an unannotated lambda:
6514+
///
6515+
/// ```py
6516+
/// from typing import Callable, cast
6517+
///
6518+
/// def convert(value: int) -> str:
6519+
/// return str(value)
6520+
///
6521+
/// def callback() -> Callable[[int | None], str]:
6522+
/// return cast(Callable[[int | None], str], lambda value: convert(value)) # no diagnostic
6523+
/// ```
6524+
///
6525+
/// With the outer context, the lambda parameter has type `int | None`, so its call to `convert`
6526+
/// produces an `invalid-argument-type` diagnostic. Retrying without that context leaves the
6527+
/// parameter's type unknown, and the cast is neither redundant nor disjoint. Casts allow
6528+
/// `redundant-cast` diagnostics from nested casts so that those warnings do not interrupt
6529+
/// context propagation.
6530+
fn infer_with_type_context_fallback(
6531+
&mut self,
6532+
tcx: TypeContext<'db>,
6533+
fallback_tcx: TypeContext<'db>,
6534+
mut infer_expression: impl FnMut(&mut Self, TypeContext<'db>) -> Type<'db>,
6535+
) -> Type<'db> {
6536+
// Cache nested expressions so retries do not lead to exponential inference work.
6537+
let teardown_expression_cache = self.setup_expression_cache();
65066538
let mut speculative_builder = self.speculate();
6507-
let ty = infer_expression(&mut speculative_builder, peer_tcx);
6539+
let ty = infer_expression(&mut speculative_builder, tcx);
65086540

6509-
// Peer context is only an inference hint. If it introduces diagnostics, discard it and
6510-
// infer normally so that only diagnostics intrinsic to the expression are reported.
6511-
if speculative_builder.context.has_diagnostics() {
6512-
infer_expression(self, tcx)
6541+
// This context is only an inference hint. Discard it if inference reports a diagnostic
6542+
// other than the explicitly allowed lint, then infer with the original context.
6543+
let ty = if speculative_builder.context.has_diagnostics() {
6544+
infer_expression(self, fallback_tcx)
65136545
} else {
65146546
self.extend(speculative_builder);
65156547
ty
6548+
};
6549+
6550+
if teardown_expression_cache {
6551+
self.teardown_expression_cache();
65166552
}
6553+
ty
65176554
}
65186555

65196556
#[track_caller]
@@ -9590,10 +9627,32 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
95909627
&bindings,
95919628
);
95929629

9630+
let cast_value = if call_expression_tcx.annotation.is_some()
9631+
&& let Type::FunctionLiteral(function) = callable_type
9632+
&& function.is_known(db, KnownFunction::Cast)
9633+
{
9634+
arguments.find_argument_value("val", 1)
9635+
} else {
9636+
None
9637+
};
9638+
95939639
let bindings_result = self.infer_and_check_argument_types(
95949640
ArgumentsIter::from_ast(arguments),
95959641
&mut call_arguments,
95969642
&mut |builder, (_, expr, tcx)| {
9643+
if let Some(value) = cast_value
9644+
&& std::ptr::eq(expr, value)
9645+
{
9646+
// The outer context can make this cast redundant, but must not introduce
9647+
// errors in its value, such as missing keys in a partial `TypedDict` literal.
9648+
// Redundant casts inside the value are also an intended result of propagation.
9649+
return builder.infer_with_type_context_fallback(
9650+
call_expression_tcx,
9651+
tcx,
9652+
|builder, tcx| builder.infer_expression(value, tcx),
9653+
);
9654+
}
9655+
95979656
// Permit bare ParamSpecs only in direct names and dotted attributes, so nested
95989657
// type expressions and calls retain their ordinary validation.
95999658
if matches!(

0 commit comments

Comments
 (0)