forked from VimPlug/jedi
Make Union/Optional works with compiled objects
This commit is contained in:
@@ -484,6 +484,19 @@ class DirectObjectAccess(object):
|
||||
op = _OPERATORS[operator]
|
||||
return self._create_access_path(op(self._obj, other_access._obj))
|
||||
|
||||
def get_annotation_name_and_args(self):
|
||||
"""
|
||||
Returns Tuple[Optional[str], Tuple[AccessPath, ...]]
|
||||
"""
|
||||
if sys.version_info < (3, 5):
|
||||
return None, ()
|
||||
|
||||
import typing
|
||||
args = typing.get_args(self._obj)
|
||||
origin = typing.get_origin(self._obj)
|
||||
name = None if origin is None else str(origin)
|
||||
return name, tuple(self._create_access_path(arg) for arg in args)
|
||||
|
||||
def needs_type_completions(self):
|
||||
return inspect.isclass(self._obj) and self._obj != type
|
||||
|
||||
|
||||
@@ -271,6 +271,11 @@ class CompiledObject(Value):
|
||||
if self.access_handle.get_repr() == 'None':
|
||||
# None as an annotation doesn't need to be executed.
|
||||
return ValueSet([self])
|
||||
|
||||
name, args = self.access_handle.get_annotation_name_and_args()
|
||||
arguments = [create_from_access_path(self.inference_state, path) for path in args]
|
||||
if name == 'typing.Union':
|
||||
return ValueSet.from_sets(arg.execute_annotation() for arg in arguments)
|
||||
return super(CompiledObject, self).execute_annotation()
|
||||
|
||||
def negate(self):
|
||||
|
||||
@@ -653,7 +653,11 @@ def bar():
|
||||
({'return': 'foo()'}, []),
|
||||
({'return': 'bar()'}, ['float']),
|
||||
# typing is available via globals.
|
||||
#({'return': 'typing.Union[str, int]'}, ['str']),
|
||||
({'return': 'typing.Union[str, int]'}, ['int', 'str']),
|
||||
({'return': 'typing.Union["str", int]'}, ['int']),
|
||||
({'return': 'typing.Union["str", 1]'}, []),
|
||||
({'return': 'typing.Optional[str]'}, ['NoneType', 'str']),
|
||||
({'return': 'typing.Optional[str, int]'}, []), # Takes only one arg
|
||||
|
||||
({'return': 'decimal.Decimal'}, []),
|
||||
({'return': 'lalalalallalaa'}, []),
|
||||
|
||||
Reference in New Issue
Block a user