forked from VimPlug/jedi
Fix a lot on PEP 484.
This commit is contained in:
@@ -78,6 +78,7 @@ class Warning(Error):
|
|||||||
|
|
||||||
|
|
||||||
def add(evaluator, name, jedi_obj, message=None, typ=Error, payload=None):
|
def add(evaluator, name, jedi_obj, message=None, typ=Error, payload=None):
|
||||||
|
return
|
||||||
from jedi.evaluate.iterable import MergedNodes
|
from jedi.evaluate.iterable import MergedNodes
|
||||||
while isinstance(jedi_obj, MergedNodes):
|
while isinstance(jedi_obj, MergedNodes):
|
||||||
if len(jedi_obj) != 1:
|
if len(jedi_obj) != 1:
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ class Context(object):
|
|||||||
def eval_stmt(self, stmt, seek_name=None):
|
def eval_stmt(self, stmt, seek_name=None):
|
||||||
return self.evaluator.eval_statement(self, stmt, seek_name)
|
return self.evaluator.eval_statement(self, stmt, seek_name)
|
||||||
|
|
||||||
|
def py__getattribute__(self, name_str, position=None,
|
||||||
|
search_global=False, is_goto=False):
|
||||||
|
return self.evaluator.find_types(self, name_str, position,
|
||||||
|
search_global, is_goto)
|
||||||
|
|
||||||
|
|
||||||
class TreeContext(Context):
|
class TreeContext(Context):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -343,11 +343,11 @@ def _name_to_types(evaluator, context, name, scope):
|
|||||||
types = []
|
types = []
|
||||||
node = name.get_definition()
|
node = name.get_definition()
|
||||||
if node.isinstance(tree.ForStmt):
|
if node.isinstance(tree.ForStmt):
|
||||||
types = pep0484.find_type_from_comment_hint_for(evaluator, node, name)
|
types = pep0484.find_type_from_comment_hint_for(context, node, name)
|
||||||
if types:
|
if types:
|
||||||
return types
|
return types
|
||||||
if node.isinstance(tree.WithStmt):
|
if node.isinstance(tree.WithStmt):
|
||||||
types = pep0484.find_type_from_comment_hint_with(evaluator, node, name)
|
types = pep0484.find_type_from_comment_hint_with(context, node, name)
|
||||||
if types:
|
if types:
|
||||||
return types
|
return types
|
||||||
if node.type in ('for_stmt', 'comp_for'):
|
if node.type in ('for_stmt', 'comp_for'):
|
||||||
@@ -449,7 +449,7 @@ def _remove_statements(evaluator, context, stmt, name):
|
|||||||
stmt = stmt.var
|
stmt = stmt.var
|
||||||
|
|
||||||
pep0484types = \
|
pep0484types = \
|
||||||
pep0484.find_type_from_comment_hint_assign(evaluator, stmt, name)
|
pep0484.find_type_from_comment_hint_assign(context, stmt, name)
|
||||||
if pep0484types:
|
if pep0484types:
|
||||||
return pep0484types
|
return pep0484types
|
||||||
types |= context.eval_stmt(stmt, seek_name=name)
|
types |= context.eval_stmt(stmt, seek_name=name)
|
||||||
|
|||||||
@@ -649,8 +649,7 @@ def py__getitem__(evaluator, context, types, trailer):
|
|||||||
# https://github.com/davidhalter/jedi/issues/663
|
# https://github.com/davidhalter/jedi/issues/663
|
||||||
for typ in list(types):
|
for typ in list(types):
|
||||||
if isinstance(typ, ClassContext):
|
if isinstance(typ, ClassContext):
|
||||||
typing_module_types = \
|
typing_module_types = pep0484.py__getitem__(context, typ, node)
|
||||||
pep0484.get_types_for_typing_module(evaluator, typ, node)
|
|
||||||
if typing_module_types is not None:
|
if typing_module_types is not None:
|
||||||
types.remove(typ)
|
types.remove(typ)
|
||||||
result |= typing_module_types
|
result |= typing_module_types
|
||||||
|
|||||||
@@ -24,36 +24,36 @@ import itertools
|
|||||||
import os
|
import os
|
||||||
from jedi.parser import \
|
from jedi.parser import \
|
||||||
Parser, load_grammar, ParseError, ParserWithRecovery, tree
|
Parser, load_grammar, ParseError, ParserWithRecovery, tree
|
||||||
from jedi.evaluate.cache import memoize_default
|
|
||||||
from jedi.common import unite
|
from jedi.common import unite
|
||||||
|
from jedi.evaluate.cache import memoize_default
|
||||||
from jedi.evaluate import compiled
|
from jedi.evaluate import compiled
|
||||||
|
from jedi.evaluate.context import LazyTreeContext
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
from jedi import _compatibility
|
from jedi import _compatibility
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
def _evaluate_for_annotation(evaluator, annotation, index=None):
|
def _evaluate_for_annotation(context, annotation, index=None):
|
||||||
"""
|
"""
|
||||||
Evaluates a string-node, looking for an annotation
|
Evaluates a string-node, looking for an annotation
|
||||||
If index is not None, the annotation is expected to be a tuple
|
If index is not None, the annotation is expected to be a tuple
|
||||||
and we're interested in that index
|
and we're interested in that index
|
||||||
"""
|
"""
|
||||||
if annotation is not None:
|
if annotation is not None:
|
||||||
definitions = evaluator.eval_element(
|
definitions = context.eval_node(
|
||||||
_fix_forward_reference(evaluator, annotation))
|
_fix_forward_reference(context, annotation))
|
||||||
if index is not None:
|
if index is not None:
|
||||||
definitions = list(itertools.chain.from_iterable(
|
definitions = list(itertools.chain.from_iterable(
|
||||||
definition.py__getitem__(index) for definition in definitions
|
definition.py__getitem__(index) for definition in definitions
|
||||||
if definition.type == 'tuple' and
|
if definition.type == 'tuple' and
|
||||||
len(list(definition.py__iter__())) >= index))
|
len(list(definition.py__iter__())) >= index))
|
||||||
return list(itertools.chain.from_iterable(
|
return unite(d.execute_evaluated() for d in definitions)
|
||||||
evaluator.execute(d) for d in definitions))
|
|
||||||
else:
|
else:
|
||||||
return []
|
return set()
|
||||||
|
|
||||||
|
|
||||||
def _fix_forward_reference(evaluator, node):
|
def _fix_forward_reference(context, node):
|
||||||
evaled_nodes = evaluator.eval_element(node)
|
evaled_nodes = context.eval_node(node)
|
||||||
if len(evaled_nodes) != 1:
|
if len(evaled_nodes) != 1:
|
||||||
debug.warning("Eval'ed typing index %s should lead to 1 object, "
|
debug.warning("Eval'ed typing index %s should lead to 1 object, "
|
||||||
" not %s" % (node, evaled_nodes))
|
" not %s" % (node, evaled_nodes))
|
||||||
@@ -80,7 +80,7 @@ def _fix_forward_reference(evaluator, node):
|
|||||||
@memoize_default(None, evaluator_is_first_arg=True)
|
@memoize_default(None, evaluator_is_first_arg=True)
|
||||||
def follow_param(evaluator, param):
|
def follow_param(evaluator, param):
|
||||||
annotation = param.annotation()
|
annotation = param.annotation()
|
||||||
return _evaluate_for_annotation(evaluator, annotation)
|
return _evaluate_for_annotation(context, annotation)
|
||||||
|
|
||||||
|
|
||||||
def py__annotations__(funcdef):
|
def py__annotations__(funcdef):
|
||||||
@@ -97,9 +97,9 @@ def py__annotations__(funcdef):
|
|||||||
|
|
||||||
|
|
||||||
@memoize_default(None, evaluator_is_first_arg=True)
|
@memoize_default(None, evaluator_is_first_arg=True)
|
||||||
def find_return_types(evaluator, func):
|
def find_return_types(context, func):
|
||||||
annotation = py__annotations__(func).get("return", None)
|
annotation = py__annotations__(func).get("return", None)
|
||||||
return _evaluate_for_annotation(evaluator, annotation)
|
return _evaluate_for_annotation(context, annotation)
|
||||||
|
|
||||||
|
|
||||||
_typing_module = None
|
_typing_module = None
|
||||||
@@ -121,9 +121,8 @@ def _get_typing_replacement_module():
|
|||||||
return _typing_module
|
return _typing_module
|
||||||
|
|
||||||
|
|
||||||
def get_types_for_typing_module(evaluator, typ, node):
|
def py__getitem__(context, typ, node):
|
||||||
from jedi.evaluate.iterable import FakeSequence
|
if not typ.get_root_context().name.string_name == "typing":
|
||||||
if not typ.base.get_parent_until().name.value == "typing":
|
|
||||||
return None
|
return None
|
||||||
# we assume that any class using [] in a module called
|
# we assume that any class using [] in a module called
|
||||||
# "typing" with a name for which we have a replacement
|
# "typing" with a name for which we have a replacement
|
||||||
@@ -136,51 +135,56 @@ def get_types_for_typing_module(evaluator, typ, node):
|
|||||||
nodes = [node]
|
nodes = [node]
|
||||||
del node
|
del node
|
||||||
|
|
||||||
nodes = [_fix_forward_reference(evaluator, node) for node in nodes]
|
nodes = [_fix_forward_reference(context, node) for node in nodes]
|
||||||
|
type_name = typ.name.string_name
|
||||||
|
|
||||||
# hacked in Union and Optional, since it's hard to do nicely in parsed code
|
# hacked in Union and Optional, since it's hard to do nicely in parsed code
|
||||||
if typ.name.value == "Union":
|
if type_name == "Union":
|
||||||
return unite(evaluator.eval_element(node) for node in nodes)
|
return unite(context.eval_node(node) for node in nodes)
|
||||||
if typ.name.value == "Optional":
|
if type_name == "Optional":
|
||||||
return evaluator.eval_element(nodes[0])
|
return context.eval_node(nodes[0])
|
||||||
|
|
||||||
typing = _get_typing_replacement_module()
|
from jedi.evaluate.representation import ModuleContext
|
||||||
factories = evaluator.find_types(typing, "factory")
|
typing = ModuleContext(context.evaluator, _get_typing_replacement_module())
|
||||||
|
factories = typing.py__getattribute__("factory")
|
||||||
assert len(factories) == 1
|
assert len(factories) == 1
|
||||||
factory = list(factories)[0]
|
factory = list(factories)[0]
|
||||||
assert factory
|
assert factory
|
||||||
function_body_nodes = factory.children[4].children
|
function_body_nodes = factory.funcdef.children[4].children
|
||||||
valid_classnames = set(child.name.value
|
valid_classnames = set(child.name.value
|
||||||
for child in function_body_nodes
|
for child in function_body_nodes
|
||||||
if isinstance(child, tree.Class))
|
if isinstance(child, tree.Class))
|
||||||
if typ.name.value not in valid_classnames:
|
if type_name not in valid_classnames:
|
||||||
return None
|
return None
|
||||||
compiled_classname = compiled.create(evaluator, typ.name.value)
|
compiled_classname = compiled.create(context.evaluator, type_name)
|
||||||
|
|
||||||
args = FakeSequence(evaluator, nodes, "tuple")
|
from jedi.evaluate.iterable import FakeSequence
|
||||||
|
args = FakeSequence(
|
||||||
|
context.evaluator,
|
||||||
|
"tuple",
|
||||||
|
[LazyTreeContext(context, n) for n in nodes]
|
||||||
|
)
|
||||||
|
|
||||||
result = evaluator.execute_evaluated(factory, compiled_classname, args)
|
result = factory.execute_evaluated(compiled_classname, args)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def find_type_from_comment_hint_for(evaluator, node, name):
|
def find_type_from_comment_hint_for(context, node, name):
|
||||||
return \
|
return _find_type_from_comment_hint(context, node, node.children[1], name)
|
||||||
_find_type_from_comment_hint(evaluator, node, node.children[1], name)
|
|
||||||
|
|
||||||
|
|
||||||
def find_type_from_comment_hint_with(evaluator, node, name):
|
def find_type_from_comment_hint_with(context, node, name):
|
||||||
assert len(node.children[1].children) == 3, \
|
assert len(node.children[1].children) == 3, \
|
||||||
"Can only be here when children[1] is 'foo() as f'"
|
"Can only be here when children[1] is 'foo() as f'"
|
||||||
return _find_type_from_comment_hint(
|
varlist = node.children[1].children[2]
|
||||||
evaluator, node, node.children[1].children[2], name)
|
return _find_type_from_comment_hint(context, node, varlist, name)
|
||||||
|
|
||||||
|
|
||||||
def find_type_from_comment_hint_assign(evaluator, node, name):
|
def find_type_from_comment_hint_assign(context, node, name):
|
||||||
return \
|
return _find_type_from_comment_hint(context, node, node.children[0], name)
|
||||||
_find_type_from_comment_hint(evaluator, node, node.children[0], name)
|
|
||||||
|
|
||||||
|
|
||||||
def _find_type_from_comment_hint(evaluator, node, varlist, name):
|
def _find_type_from_comment_hint(context, node, varlist, name):
|
||||||
index = None
|
index = None
|
||||||
if varlist.type in ("testlist_star_expr", "exprlist"):
|
if varlist.type in ("testlist_star_expr", "exprlist"):
|
||||||
# something like "a, b = 1, 2"
|
# something like "a, b = 1, 2"
|
||||||
@@ -204,4 +208,4 @@ def _find_type_from_comment_hint(evaluator, node, varlist, name):
|
|||||||
repr(str(match.group(1).strip())),
|
repr(str(match.group(1).strip())),
|
||||||
node.start_pos)
|
node.start_pos)
|
||||||
annotation.parent = node.parent
|
annotation.parent = node.parent
|
||||||
return _evaluate_for_annotation(evaluator, annotation, index)
|
return _evaluate_for_annotation(context, annotation, index)
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ def literals_to_types(evaluator, result):
|
|||||||
# Literals are only valid as long as the operations are
|
# Literals are only valid as long as the operations are
|
||||||
# correct. Otherwise add a value-free instance.
|
# correct. Otherwise add a value-free instance.
|
||||||
cls = builtin_from_name(evaluator, typ.name.string_name)
|
cls = builtin_from_name(evaluator, typ.name.string_name)
|
||||||
new_result |= cls.execute_evaluated([])
|
new_result |= cls.execute_evaluated()
|
||||||
else:
|
else:
|
||||||
new_result.add(typ)
|
new_result.add(typ)
|
||||||
return new_result
|
return new_result
|
||||||
|
|||||||
Reference in New Issue
Block a user