2828 Const ,
2929 JoinedStr ,
3030 Name ,
31+ NodeNG ,
3132 Subscript ,
3233 Tuple ,
3334)
@@ -379,6 +380,36 @@ def infer_special_alias(
379380 return iter ([class_def ])
380381
381382
383+ def _looks_like_typing_cast (node : Call ) -> bool :
384+ return isinstance (node , Call ) and (
385+ isinstance (node .func , Name )
386+ and node .func .name == "cast"
387+ or isinstance (node .func , Attribute )
388+ and node .func .attrname == "cast"
389+ )
390+
391+
392+ def infer_typing_cast (
393+ node : Call , ctx : context .InferenceContext | None = None
394+ ) -> Iterator [NodeNG ]:
395+ """Infer call to cast() returning same type as casted-from var."""
396+ if not isinstance (node .func , (Name , Attribute )):
397+ raise UseInferenceDefault
398+
399+ try :
400+ func = next (node .func .infer (context = ctx ))
401+ except (InferenceError , StopIteration ) as exc :
402+ raise UseInferenceDefault from exc
403+ if (
404+ not isinstance (func , FunctionDef )
405+ or func .qname () != "typing.cast"
406+ or len (node .args ) != 2
407+ ):
408+ raise UseInferenceDefault
409+
410+ return node .args [1 ].infer (context = ctx )
411+
412+
382413AstroidManager ().register_transform (
383414 Call ,
384415 inference_tip (infer_typing_typevar_or_newtype ),
@@ -387,6 +418,9 @@ def infer_special_alias(
387418AstroidManager ().register_transform (
388419 Subscript , inference_tip (infer_typing_attr ), _looks_like_typing_subscript
389420)
421+ AstroidManager ().register_transform (
422+ Call , inference_tip (infer_typing_cast ), _looks_like_typing_cast
423+ )
390424
391425if PY39_PLUS :
392426 AstroidManager ().register_transform (
0 commit comments