1
0
forked from VimPlug/jedi

Implement Self, fixes #2023, fixes #2068

This commit is contained in:
Dave Halter
2026-04-29 17:51:01 +02:00
parent c30732eb04
commit aa72381ed1
16 changed files with 92 additions and 47 deletions
+6 -6
View File
@@ -249,12 +249,12 @@ def infer_return_types(function, arguments):
return _infer_annotation_string(
context,
match.group(1).strip()
).execute_annotation()
).execute_annotation(context)
unknown_type_vars = find_unknown_type_vars(context, annotation)
annotation_values = infer_annotation(context, annotation)
if not unknown_type_vars:
return annotation_values.execute_annotation()
return annotation_values.execute_annotation(context)
type_var_dict = infer_type_vars_for_execution(function, arguments, all_annotations)
@@ -262,7 +262,7 @@ def infer_return_types(function, arguments):
ann.define_generics(type_var_dict)
if isinstance(ann, (DefineGenericBaseClass, TypeVar)) else ValueSet({ann})
for ann in annotation_values
).execute_annotation()
).execute_annotation(context)
def infer_type_vars_for_execution(function, arguments, annotation_dict):
@@ -315,7 +315,7 @@ def infer_return_for_callable(arguments, param_values, result_values):
if isinstance(v, (DefineGenericBaseClass, TypeVar))
else ValueSet({v})
for v in result_values
).execute_annotation()
).execute_annotation(arguments.context)
def _infer_type_vars_for_callable(arguments, lazy_params):
@@ -391,7 +391,7 @@ def merge_pairwise_generics(annotation_value, annotated_argument_class):
for annotation_generics_set, actual_generic_set in zip(annotation_generics, actual_generics):
merge_type_var_dicts(
type_var_dict,
annotation_generics_set.infer_type_vars(actual_generic_set.execute_annotation()),
annotation_generics_set.infer_type_vars(actual_generic_set.execute_annotation(None)),
)
return type_var_dict
@@ -438,7 +438,7 @@ def _find_type_from_comment_hint(context, node, varlist, name):
return []
return _infer_annotation_string(
context, match.group(1).strip(), index
).execute_annotation()
).execute_annotation(context)
def find_unknown_type_vars(context, node):
+2 -2
View File
@@ -306,7 +306,7 @@ class _GenericInstanceWrapper(ValueWrapper):
if cls.py__name__() == 'Generator':
generics = cls.get_generics()
try:
return generics[2].execute_annotation()
return generics[2].execute_annotation(None)
except IndexError:
pass
elif cls.py__name__() == 'Iterator':
@@ -427,7 +427,7 @@ class BaseTypingInstance(LazyValueWrapper):
return ValueName(self, self._tree_name)
def _get_wrapped_value(self):
object_, = builtin_from_name(self.inference_state, 'object').execute_annotation()
object_, = builtin_from_name(self.inference_state, 'object').execute_annotation(None)
return object_
def __repr__(self):
+1 -1
View File
@@ -35,7 +35,7 @@ class _AbstractGenericManager:
def get_index_and_execute(self, index):
try:
return self[index].execute_annotation()
return self[index].execute_annotation(None)
except IndexError:
debug.warning('No param #%s found for annotation %s', index, self)
return NO_VALUES
+3 -3
View File
@@ -100,8 +100,8 @@ class TypeVar(BaseTypingValue):
return found
return ValueSet({self})
def execute_annotation(self):
return self._get_classes().execute_annotation()
def execute_annotation(self, context):
return self._get_classes().execute_annotation(context)
def infer_type_vars(self, value_set):
def iterate():
@@ -123,5 +123,5 @@ class TypeWrapper(ValueWrapper):
super().__init__(wrapped_value)
self._original_value = original_value
def execute_annotation(self):
def execute_annotation(self, context):
return ValueSet({self._original_value})
+25 -13
View File
@@ -83,6 +83,9 @@ class TypingModuleName(NameWrapper):
elif name == 'cast':
cast_fn, = self._wrapped_name.infer()
yield CastFunction.create_cached(inference_state, cast_fn)
elif name == 'Self':
yield SelfClass.create_cached(
inference_state, self.parent_context, self.tree_name)
elif name == 'TypedDict':
# TODO doesn't even exist in typeshed/typing.py, yet. But will be
# added soon.
@@ -100,24 +103,24 @@ class TypingModuleFilterWrapper(FilterWrapper):
class ProxyWithGenerics(BaseTypingClassWithGenerics):
def execute_annotation(self):
def execute_annotation(self, context):
string_name = self._tree_name.value
if string_name == 'Union':
# This is kind of a special case, because we have Unions (in Jedi
# ValueSets).
return self.gather_annotation_classes().execute_annotation()
return self.gather_annotation_classes().execute_annotation(context)
elif string_name == 'Optional':
# Optional is basically just saying it's either None or the actual
# type.
return self.gather_annotation_classes().execute_annotation() \
return self.gather_annotation_classes().execute_annotation(context) \
| ValueSet([builtin_from_name(self.inference_state, 'None')])
elif string_name == 'Type':
# The type is actually already given in the index_value
return self._generics_manager[0]
elif string_name in IGNORE_ANNOTATION_PARTS:
# For now don't do anything here, ClassVars are always used.
return self._generics_manager[0].execute_annotation()
return self._generics_manager[0].execute_annotation(context)
mapped = {
'Tuple': Tuple,
@@ -217,17 +220,17 @@ class TypingClassWithGenerics(ProxyWithGenerics, _TypingClassMixin):
# This is basically a trick to avoid extra code: We execute the
# incoming classes to be able to use the normal code for type
# var inference.
value_set.execute_annotation(),
value_set.execute_annotation(None),
)
elif annotation_name == 'Callable':
if len(annotation_generics) == 2:
return annotation_generics[1].infer_type_vars(
value_set.execute_annotation(),
value_set.execute_annotation(None),
)
elif annotation_name == 'Tuple':
tuple_annotation, = self.execute_annotation()
tuple_annotation, = self.execute_annotation(None)
return tuple_annotation.infer_type_vars(value_set)
return type_var_dict
@@ -323,7 +326,7 @@ class Tuple(BaseTypingInstance):
yield LazyKnownValues(self._generics_manager.get_index_and_execute(0))
else:
for v in self._generics_manager.to_tuple():
yield LazyKnownValues(v.execute_annotation())
yield LazyKnownValues(v.execute_annotation(None))
def py__getitem__(self, index_value_set, contextualized_node):
if self._is_homogenous():
@@ -331,11 +334,11 @@ class Tuple(BaseTypingInstance):
return ValueSet.from_sets(
self._generics_manager.to_tuple()
).execute_annotation()
).execute_annotation(None)
def _get_wrapped_value(self):
tuple_, = self.inference_state.builtins_module \
.py__getattribute__('tuple').execute_annotation()
.py__getattribute__('tuple').execute_annotation(None)
return tuple_
@property
@@ -392,11 +395,20 @@ class Protocol(BaseTypingInstance):
class AnyClass(BaseTypingValue):
def execute_annotation(self):
def execute_annotation(self, context):
debug.warning('Used Any - returned no results')
return NO_VALUES
class SelfClass(BaseTypingValue):
def execute_annotation(self, context):
debug.warning('Used Self')
if context is not None:
# Execute the class of Self
return context.get_value().execute_annotation(None)
return NO_VALUES
class OverloadFunction(BaseTypingValue):
@repack_with_argument_clinic('func, /')
def py__call__(self, func_value_set):
@@ -431,7 +443,7 @@ class NewType(Value):
return c
def py__call__(self, arguments):
return self._type_value_set.execute_annotation()
return self._type_value_set.execute_annotation(arguments.context)
@property
def name(self):
@@ -445,7 +457,7 @@ class NewType(Value):
class CastFunction(ValueWrapper):
@repack_with_argument_clinic('type, object, /')
def py__call__(self, type_value_set, object_value_set):
return type_value_set.execute_annotation()
return type_value_set.execute_annotation(None)
class TypedDictClass(BaseTypingValue):