diff options
| author | Tim Martin <tim@asymptotic.co.uk> | 2021-07-30 20:18:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-30 21:18:17 +0200 |
| commit | f236e36729265602143ace2d969b44e53e4d57c1 (patch) | |
| tree | c634d68f7e5bd1e4fb5c21cbef0362ac54751f51 | |
| parent | e7f4def327e749f2f2db3bf3412b9dc4e22359ef (diff) | |
| download | astroid-git-f236e36729265602143ace2d969b44e53e4d57c1.tar.gz | |
Infer the type of the result of calling typing.cast() (#1076)
* Infer the type of the result of calling typing.cast()
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
| -rw-r--r-- | ChangeLog | 1 | ||||
| -rw-r--r-- | astroid/brain/brain_typing.py | 34 | ||||
| -rw-r--r-- | tests/unittest_brain.py | 32 |
3 files changed, 67 insertions, 0 deletions
@@ -11,6 +11,7 @@ What's New in astroid 2.6.6? ============================ Release date: TBA +* Added support to infer return type of ``typing.cast()`` What's New in astroid 2.6.5? diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index a949bd91..a28da573 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -28,6 +28,7 @@ from astroid.node_classes import ( Call, Const, Name, + NodeNG, Subscript, ) from astroid.scoped_nodes import ClassDef, FunctionDef @@ -356,6 +357,36 @@ def infer_tuple_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 +) -> typing.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 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), @@ -364,6 +395,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( diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py index 31d190d2..dd040bd4 100644 --- a/tests/unittest_brain.py +++ b/tests/unittest_brain.py @@ -1810,6 +1810,38 @@ class TypingBrain(unittest.TestCase): self.assertIsInstance(inferred, nodes.ClassDef) self.assertIsInstance(inferred.getattr("__iter__")[0], nodes.FunctionDef) + def test_typing_cast(self): + node = builder.extract_node( + """ + from typing import cast + class A: + pass + + b = 42 + a = cast(A, b) + a + """ + ) + inferred = next(node.infer()) + assert isinstance(inferred, nodes.Const) + assert inferred.value == 42 + + def test_typing_cast_attribute(self): + node = builder.extract_node( + """ + import typing + class A: + pass + + b = 42 + a = typing.cast(A, b) + a + """ + ) + inferred = next(node.infer()) + assert isinstance(inferred, nodes.Const) + assert inferred.value == 42 + class ReBrainTest(unittest.TestCase): def test_regex_flags(self): |
