Avoid function executions if they are not necessary

This also means that annotations are prefered to docstring types
This commit is contained in:
Dave Halter
2019-08-24 11:59:13 +02:00
parent e0f26dd7a1
commit 88cf198552
5 changed files with 15 additions and 11 deletions

View File

@@ -687,7 +687,7 @@ class ParamDefinition(Definition):
:param execute_annotation: If False, the values are not executed and :param execute_annotation: If False, the values are not executed and
you get classes instead of instances. you get classes instead of instances.
""" """
return _values_to_definitions(self._name.infer_annotation(**kwargs)) return _values_to_definitions(self._name.infer_annotation(ignore_stars=True, **kwargs))
def to_string(self): def to_string(self):
return self._name.to_string() return self._name.to_string()

View File

@@ -107,8 +107,10 @@ def _split_comment_param_declaration(decl_text):
@inference_state_method_cache() @inference_state_method_cache()
def infer_param(execution_context, param): def infer_param(execution_context, param, ignore_stars=False):
values = _infer_param(execution_context, param) values = _infer_param(execution_context, param)
if ignore_stars:
return values
inference_state = execution_context.inference_state inference_state = execution_context.inference_state
if param.star_count == 1: if param.star_count == 1:
tuple_ = builtin_from_name(inference_state, 'tuple') tuple_ = builtin_from_name(inference_state, 'tuple')

View File

@@ -242,11 +242,11 @@ class ParamName(BaseTreeParamName):
def annotation_node(self): def annotation_node(self):
return self._get_param_node().annotation return self._get_param_node().annotation
def infer_annotation(self, execute_annotation=True): def infer_annotation(self, execute_annotation=True, ignore_stars=False):
node = self.annotation_node from jedi.inference.gradual.annotation import infer_param
if node is None: values = infer_param(
return NO_VALUES self.parent_context, self._get_param_node(),
values = self.parent_context.parent_context.infer_node(node) ignore_stars=ignore_stars)
if execute_annotation: if execute_annotation:
values = values.execute_annotation() values = values.execute_annotation()
return values return values
@@ -299,6 +299,9 @@ class ParamName(BaseTreeParamName):
return Parameter.POSITIONAL_OR_KEYWORD return Parameter.POSITIONAL_OR_KEYWORD
def infer(self): def infer(self):
values = self.infer_annotation()
if values:
return values
return self.get_param().infer() return self.get_param().infer()
def get_param(self): def get_param(self):

View File

@@ -33,9 +33,8 @@ class ExecutedParam(object):
def infer(self, use_hints=True): def infer(self, use_hints=True):
if use_hints: if use_hints:
doc_params = docstrings.infer_param(self._execution_context, self._param_node) doc_params = docstrings.infer_param(self._execution_context, self._param_node)
ann = self.infer_annotations().execute_annotation() if doc_params:
if ann or doc_params: return doc_params
return ann | doc_params
return self._lazy_value.infer() return self._lazy_value.infer()

View File

@@ -24,7 +24,7 @@ def function_parameters(a: A, b, c: str, d: int, e: str, f: str, g: int=4):
d d
#? str() #? str()
e e
#? int() str() #? str()
f f
# int() # int()
g g