Implement typing.cast

This commit is contained in:
Dave Halter
2019-05-27 20:59:04 +02:00
parent 8d24e35fa9
commit 5a6d8ba010
2 changed files with 18 additions and 2 deletions

View File

@@ -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.

View File

@@ -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()