28
28
Const ,
29
29
JoinedStr ,
30
30
Name ,
31
+ NodeNG ,
31
32
Subscript ,
32
33
Tuple ,
33
34
)
@@ -379,6 +380,36 @@ def infer_special_alias(
379
380
return iter ([class_def ])
380
381
381
382
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
+
382
413
AstroidManager ().register_transform (
383
414
Call ,
384
415
inference_tip (infer_typing_typevar_or_newtype ),
@@ -387,6 +418,9 @@ def infer_special_alias(
387
418
AstroidManager ().register_transform (
388
419
Subscript , inference_tip (infer_typing_attr ), _looks_like_typing_subscript
389
420
)
421
+ AstroidManager ().register_transform (
422
+ Call , inference_tip (infer_typing_cast ), _looks_like_typing_cast
423
+ )
390
424
391
425
if PY39_PLUS :
392
426
AstroidManager ().register_transform (
0 commit comments