From 5a6d8ba01069effcf3e627d3b95afe926354f335 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 27 May 2019 20:59:04 +0200 Subject: [PATCH] Implement typing.cast --- jedi/evaluate/gradual/typing.py | 9 +++++++-- test/completion/pep0484_typing.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/jedi/evaluate/gradual/typing.py b/jedi/evaluate/gradual/typing.py index e7733578..b494f274 100644 --- a/jedi/evaluate/gradual/typing.py +++ b/jedi/evaluate/gradual/typing.py @@ -114,8 +114,7 @@ class TypingModuleName(NameWrapper): yield OverloadFunction.create_cached(evaluator, self.parent_context, self.tree_name) elif name == 'cast': # TODO implement cast - for c in self._wrapped_name.infer(): # Fuck my life Python 2 - yield c + yield CastFunction.create_cached(evaluator, self.parent_context, self.tree_name) elif name == 'TypedDict': # TODO doesn't even exist in typeshed/typing.py, yet. But will be # added soon. @@ -449,6 +448,12 @@ class OverloadFunction(_BaseTypingContext): return func_context_set +class CastFunction(_BaseTypingContext): + @repack_with_argument_clinic('type, object, /') + def py__call__(self, type_context_set, object_context_set): + return type_context_set.execute_annotation() + + class BoundTypeVarName(AbstractNameDefinition): """ This type var was bound to a certain type, e.g. int. diff --git a/test/completion/pep0484_typing.py b/test/completion/pep0484_typing.py index 0590ccee..8d48ce46 100644 --- a/test/completion/pep0484_typing.py +++ b/test/completion/pep0484_typing.py @@ -351,3 +351,14 @@ def foo(a: typing.List, b: typing.Dict, c: typing.MutableMapping) -> typing.Type c['asdf'] #? int foo() + +def cast_tests(): + x = 3.0 + y = typing.cast(int, x) + #? int() + y + return typing.cast(str, x) + + +#? str() +cast_tests()