From 72f5afb8c3a15114f8033a51b390f845b49a209c Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 31 Jan 2023 21:46:52 +0100 Subject: Revert CallContext change since it caused a RecursionError regression (#2000) This reverts commit a0d219cb3403cda3338a14ce67a60954b4ff5cd7 (#1982). --- astroid/brain/brain_typing.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'astroid/brain') diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index 6a134072..b11bfa19 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -28,6 +28,7 @@ from astroid.nodes.node_classes import ( Const, JoinedStr, Name, + NodeNG, Subscript, Tuple, ) @@ -379,6 +380,36 @@ def infer_special_alias( return iter([class_def]) +def _looks_like_typing_cast(node: Call) -> bool: + return isinstance(node, Call) and ( + isinstance(node.func, Name) + and node.func.name == "cast" + or isinstance(node.func, Attribute) + and node.func.attrname == "cast" + ) + + +def infer_typing_cast( + node: Call, ctx: context.InferenceContext | None = None +) -> Iterator[NodeNG]: + """Infer call to cast() returning same type as casted-from var.""" + if not isinstance(node.func, (Name, Attribute)): + raise UseInferenceDefault + + try: + func = next(node.func.infer(context=ctx)) + except (InferenceError, StopIteration) as exc: + raise UseInferenceDefault from exc + if ( + not isinstance(func, FunctionDef) + or func.qname() != "typing.cast" + or len(node.args) != 2 + ): + raise UseInferenceDefault + + return node.args[1].infer(context=ctx) + + AstroidManager().register_transform( Call, inference_tip(infer_typing_typevar_or_newtype), @@ -387,6 +418,9 @@ AstroidManager().register_transform( AstroidManager().register_transform( Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript ) +AstroidManager().register_transform( + Call, inference_tip(infer_typing_cast), _looks_like_typing_cast +) if PY39_PLUS: AstroidManager().register_transform( -- cgit v1.2.1