forked from VimPlug/jedi
Implement support for TypeVar inference for __new__
This commit is contained in:
@@ -17,7 +17,7 @@ from jedi.inference.arguments import ValuesArguments, TreeArgumentsWrapper
|
||||
from jedi.inference.value.function import \
|
||||
FunctionValue, FunctionMixin, OverloadedFunctionValue, \
|
||||
BaseFunctionExecutionContext, FunctionExecutionContext, FunctionNameInClass
|
||||
from jedi.inference.value.klass import ClassFilter
|
||||
from jedi.inference.value.klass import ClassFilter, init_or_new_func
|
||||
from jedi.inference.value.dynamic_arrays import get_dynamic_array_instance
|
||||
from jedi.parser_utils import function_is_staticmethod, function_is_classmethod
|
||||
|
||||
@@ -327,7 +327,7 @@ class TreeInstance(_BaseTreeInstance):
|
||||
infer_type_vars_for_execution
|
||||
|
||||
args = InstanceArguments(self, self._arguments)
|
||||
for signature in self.class_value.py__getattribute__('__init__').get_signatures():
|
||||
for signature in init_or_new_func(self.class_value).get_signatures():
|
||||
# Just take the first result, it should always be one, because we
|
||||
# control the typeshed code.
|
||||
funcdef = signature.value.tree_node
|
||||
|
||||
@@ -376,19 +376,8 @@ class ClassMixin:
|
||||
if sigs:
|
||||
return sigs
|
||||
args = ValuesArguments([])
|
||||
init_funcs = self.py__call__(args).py__getattribute__('__init__')
|
||||
if len(init_funcs) == 1:
|
||||
init = next(iter(init_funcs))
|
||||
try:
|
||||
class_context = init.class_context
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
# In the case where we are on object.__init__, we try to use
|
||||
# __new__.
|
||||
if class_context.get_root_context().is_builtins_module() \
|
||||
and init.class_context.name.string_name == "object":
|
||||
init_funcs = self.py__call__(args).py__getattribute__('__new__')
|
||||
instance = self.py__call__(args)
|
||||
init_funcs = init_or_new_func(instance)
|
||||
|
||||
dataclass_sigs = self._get_dataclass_transform_signatures()
|
||||
if dataclass_sigs:
|
||||
@@ -481,6 +470,23 @@ class ClassMixin:
|
||||
return ValueSet({self})
|
||||
|
||||
|
||||
def init_or_new_func(value):
|
||||
init_funcs = value.py__getattribute__('__init__')
|
||||
if len(init_funcs) == 1:
|
||||
init = next(iter(init_funcs))
|
||||
try:
|
||||
class_context = init.class_context
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
# In the case where we are on object.__init__, we try to use
|
||||
# __new__.
|
||||
if class_context.get_root_context().is_builtins_module() \
|
||||
and init.class_context.name.string_name == "object":
|
||||
return value.py__getattribute__('__new__')
|
||||
return init_funcs
|
||||
|
||||
|
||||
class DataclassParamName(BaseTreeParamName):
|
||||
"""
|
||||
Represent a field declaration on a class with dataclass semantics.
|
||||
|
||||
Reference in New Issue
Block a user