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
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):
return self._name.to_string()

View File

@@ -107,8 +107,10 @@ def _split_comment_param_declaration(decl_text):
@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)
if ignore_stars:
return values
inference_state = execution_context.inference_state
if param.star_count == 1:
tuple_ = builtin_from_name(inference_state, 'tuple')

View File

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

View File

@@ -33,9 +33,8 @@ class ExecutedParam(object):
def infer(self, use_hints=True):
if use_hints:
doc_params = docstrings.infer_param(self._execution_context, self._param_node)
ann = self.infer_annotations().execute_annotation()
if ann or doc_params:
return ann | doc_params
if doc_params:
return doc_params
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
#? str()
e
#? int() str()
#? str()
f
# int()
g