mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Use CompiledValue for simple values
This commit is contained in:
@@ -25,6 +25,9 @@ class HelperContextMixin:
|
||||
def create_cached(cls, *args, **kwargs):
|
||||
return cls(*args, **kwargs)
|
||||
|
||||
def execute(self, arguments=None):
|
||||
return self.evaluator.execute(self, arguments=arguments)
|
||||
|
||||
def execute_evaluated(self, *value_list):
|
||||
return execute_evaluated(self, *value_list)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ from jedi._compatibility import unicode
|
||||
from jedi.evaluate.compiled.context import CompiledObject, CompiledName, \
|
||||
CompiledObjectFilter, CompiledContextName, create_from_access_path, \
|
||||
create_from_name
|
||||
from jedi.evaluate.base_context import ContextWrapper
|
||||
from jedi.evaluate.helpers import execute_evaluated
|
||||
|
||||
|
||||
@@ -13,16 +14,30 @@ def builtin_from_name(evaluator, string):
|
||||
return context
|
||||
|
||||
|
||||
class CompiledValue(ContextWrapper):
|
||||
def __init__(self, instance, compiled_obj):
|
||||
super(CompiledValue, self).__init__(instance)
|
||||
self._compiled_obj = compiled_obj
|
||||
|
||||
def get_safe_value(self, *args, **kwargs):
|
||||
return self._compiled_obj.get_safe_value(*args, **kwargs)
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s: %s>' % (self.__class__.__name__, self._compiled_obj)
|
||||
|
||||
|
||||
def create_simple_object(evaluator, obj):
|
||||
"""
|
||||
Only allows creations of objects that are easily picklable across Python
|
||||
versions.
|
||||
"""
|
||||
assert isinstance(obj, (int, float, str, bytes, unicode, slice, complex))
|
||||
return create_from_access_path(
|
||||
assert type(obj) in (int, float, str, bytes, unicode, slice, complex)
|
||||
compiled_obj = create_from_access_path(
|
||||
evaluator,
|
||||
evaluator.compiled_subprocess.create_simple_object(obj)
|
||||
)
|
||||
instance, = builtin_from_name(evaluator, compiled_obj.name.string_name).execute()
|
||||
return CompiledValue(instance, compiled_obj)
|
||||
|
||||
|
||||
def get_special_object(evaluator, identifier):
|
||||
|
||||
@@ -341,16 +341,6 @@ def signature_matches(function_context, arguments):
|
||||
return True
|
||||
|
||||
|
||||
def has_same_class(context_set1, context_set2):
|
||||
for c1 in context_set1:
|
||||
for c2 in context_set2:
|
||||
if c1.name.string_name == c2.name.string_name:
|
||||
# TODO This is wrong, it doesn't account for a lot of things.
|
||||
return True
|
||||
|
||||
return bool(context_set1 & context_set2)
|
||||
|
||||
|
||||
def _find_overload_functions(context, tree_node):
|
||||
def _is_overload_decorated(funcdef):
|
||||
if funcdef.parent.type == 'decorated':
|
||||
|
||||
@@ -3,6 +3,7 @@ We need to somehow work with the typing objects. Since the typing objects are
|
||||
pretty bare we need to add all the Jedi customizations to make them work as
|
||||
contexts.
|
||||
"""
|
||||
from jedi._compatibility import unicode
|
||||
from jedi import debug
|
||||
from jedi.evaluate.cache import evaluator_method_cache
|
||||
from jedi.evaluate.compiled import builtin_from_name, CompiledObject
|
||||
@@ -359,9 +360,15 @@ class TypeVarClass(_BaseTypingContext):
|
||||
debug.warning('Found multiple contexts for a type variable: %s', context_set)
|
||||
|
||||
name_context = next(iter(context_set))
|
||||
if isinstance(name_context, CompiledObject):
|
||||
return name_context.get_safe_value(default=None)
|
||||
return None
|
||||
try:
|
||||
method = name_context.get_safe_value
|
||||
except AttributeError:
|
||||
return None
|
||||
else:
|
||||
safe_value = method(default=None)
|
||||
if isinstance(safe_value, (str, unicode)):
|
||||
return safe_value
|
||||
return None
|
||||
|
||||
|
||||
class TypeVar(_BaseTypingContext):
|
||||
|
||||
Reference in New Issue
Block a user