@@ -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
107228A tuple class with an ` Unknown ` element is not fully static, even when its other element is ` object `
0 commit comments