forked from VimPlug/jedi
made typing classes inheritable; added MutableSequence and List
This commit is contained in:
@@ -572,11 +572,15 @@ def py__getitem__(evaluator, types, index, node):
|
|||||||
# 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, Class):
|
if isinstance(typ, Class):
|
||||||
replacementclass = \
|
typing_module_types = \
|
||||||
pep0484.get_typing_replacement_class(evaluator, typ)
|
pep0484.get_types_for_typing_module(evaluator, typ, index)
|
||||||
if replacementclass:
|
if typing_module_types is not None:
|
||||||
types.remove(typ)
|
types.remove(typ)
|
||||||
result |= replacementclass.py__getitem__(index)
|
result |= typing_module_types
|
||||||
|
|
||||||
|
if not types:
|
||||||
|
# all consumed by special cases
|
||||||
|
return result
|
||||||
|
|
||||||
if type(index) not in (float, int, str, unicode, slice):
|
if type(index) not in (float, int, str, unicode, slice):
|
||||||
# If the index is not clearly defined, we have to get all the
|
# If the index is not clearly defined, we have to get all the
|
||||||
|
|||||||
25
jedi/evaluate/jedi_typing.py
Normal file
25
jedi/evaluate/jedi_typing.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
"""
|
||||||
|
This module is not intended to be used in jedi, rather it will be fed to the
|
||||||
|
jedi-parser to replace classes in the typing module
|
||||||
|
"""
|
||||||
|
|
||||||
|
from collections import abc
|
||||||
|
|
||||||
|
|
||||||
|
def factory(typing_name, indextype):
|
||||||
|
class Sequence(abc.Sequence):
|
||||||
|
def __getitem__(self) -> indextype:
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MutableSequence(Sequence, abc.MutableSequence):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class List(MutableSequence, list):
|
||||||
|
pass
|
||||||
|
|
||||||
|
dct = {
|
||||||
|
"Sequence": Sequence,
|
||||||
|
"MutableSequence": MutableSequence,
|
||||||
|
"List": List,
|
||||||
|
}
|
||||||
|
return dct[typing_name]
|
||||||
@@ -20,10 +20,11 @@ x support for type hint comments `# type: (int, str) -> int`. See comment from
|
|||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from jedi.parser import Parser, load_grammar, ParseError, tree, ParserWithRecovery
|
import os
|
||||||
|
from jedi.parser import \
|
||||||
|
Parser, load_grammar, ParseError, tree, ParserWithRecovery
|
||||||
from jedi.evaluate.cache import memoize_default
|
from jedi.evaluate.cache import memoize_default
|
||||||
from jedi.evaluate import compiled
|
from jedi.evaluate import compiled
|
||||||
from textwrap import dedent
|
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
|
|
||||||
|
|
||||||
@@ -34,7 +35,8 @@ def _evaluate_for_annotation(evaluator, annotation):
|
|||||||
if (isinstance(definition, compiled.CompiledObject) and
|
if (isinstance(definition, compiled.CompiledObject) and
|
||||||
isinstance(definition.obj, str)):
|
isinstance(definition.obj, str)):
|
||||||
try:
|
try:
|
||||||
p = Parser(load_grammar(), definition.obj, start='eval_input')
|
p = Parser(
|
||||||
|
load_grammar(), definition.obj, start='eval_input')
|
||||||
element = p.get_parsed_node()
|
element = p.get_parsed_node()
|
||||||
except ParseError:
|
except ParseError:
|
||||||
debug.warning('Annotation not parsed: %s' % definition.obj)
|
debug.warning('Annotation not parsed: %s' % definition.obj)
|
||||||
@@ -70,21 +72,15 @@ def get_typing_replacement_module():
|
|||||||
as discussed at https://github.com/davidhalter/jedi/issues/663
|
as discussed at https://github.com/davidhalter/jedi/issues/663
|
||||||
"""
|
"""
|
||||||
|
|
||||||
code = dedent("""
|
typing_path = os.path.abspath(os.path.join(__file__, "../jedi_typing.py"))
|
||||||
from collections import abc
|
with open(typing_path) as f:
|
||||||
|
code = f.read()
|
||||||
class MakeSequence:
|
|
||||||
def __getitem__(self, indextype):
|
|
||||||
class Sequence(abc.Sequence):
|
|
||||||
def __getitem__(self) -> indextype:
|
|
||||||
pass
|
|
||||||
return Sequence
|
|
||||||
""")
|
|
||||||
p = ParserWithRecovery(load_grammar(), code)
|
p = ParserWithRecovery(load_grammar(), code)
|
||||||
return p.module
|
return p.module
|
||||||
|
|
||||||
|
|
||||||
def get_typing_replacement_class(evaluator, typ):
|
def get_types_for_typing_module(evaluator, typ, index):
|
||||||
|
from jedi.evaluate.representation import Class
|
||||||
if not typ.base.get_parent_until(tree.Module).name.value == "typing":
|
if not typ.base.get_parent_until(tree.Module).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
|
||||||
@@ -93,8 +89,18 @@ def get_typing_replacement_class(evaluator, typ):
|
|||||||
# airtight but I don't have a better idea to check that it's
|
# airtight but I don't have a better idea to check that it's
|
||||||
# actually the PEP-0484 typing module and not some other
|
# actually the PEP-0484 typing module and not some other
|
||||||
typing = get_typing_replacement_module()
|
typing = get_typing_replacement_module()
|
||||||
types = evaluator.find_types(typing, "Make" + typ.name.value)
|
factories = evaluator.find_types(typing, "factory")
|
||||||
if not types:
|
assert len(factories) == 1
|
||||||
return None
|
factory = list(factories)[0]
|
||||||
|
assert factory
|
||||||
|
compiled_classname = compiled.create(evaluator, typ.name.value)
|
||||||
|
if isinstance(index, Class):
|
||||||
|
index_obj = index
|
||||||
else:
|
else:
|
||||||
return list(types)[0]
|
index_obj = compiled.create(evaluator, index)
|
||||||
|
result = \
|
||||||
|
evaluator.execute_evaluated(factory, compiled_classname, index_obj)
|
||||||
|
if result:
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|||||||
@@ -469,24 +469,6 @@ class Class(use_metaclass(CachedMetaClass, Wrapper)):
|
|||||||
def py__class__(self):
|
def py__class__(self):
|
||||||
return compiled.create(self._evaluator, type)
|
return compiled.create(self._evaluator, type)
|
||||||
|
|
||||||
def py__getitem__(self, index):
|
|
||||||
instances = self.py__call__(())
|
|
||||||
assert len(instances) == 1
|
|
||||||
instance = list(instances)[0]
|
|
||||||
try:
|
|
||||||
method = instance.get_subscope_by_name('__getitem__')
|
|
||||||
except KeyError:
|
|
||||||
debug.warning('No __getitem__, cannot access the array.')
|
|
||||||
return set()
|
|
||||||
else:
|
|
||||||
# it feels like this should be handled somewhere else,
|
|
||||||
# not sure where though
|
|
||||||
if isinstance(index, type(self)):
|
|
||||||
index_obj = index
|
|
||||||
else:
|
|
||||||
index_obj = compiled.create(self._evaluator, index)
|
|
||||||
return instance._evaluator.execute_evaluated(method, index_obj)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def params(self):
|
def params(self):
|
||||||
return self.get_subscope_by_name('__init__').params
|
return self.get_subscope_by_name('__init__').params
|
||||||
|
|||||||
@@ -163,7 +163,9 @@ def we_can_has_sequence(
|
|||||||
p: typing.Sequence[int],
|
p: typing.Sequence[int],
|
||||||
q: typing.Sequence[B],
|
q: typing.Sequence[B],
|
||||||
r: "typing.Sequence[int]",
|
r: "typing.Sequence[int]",
|
||||||
s: typing.Sequence["int"]):
|
s: typing.Sequence["int"],
|
||||||
|
t: typing.MutableSequence[dict],
|
||||||
|
u: typing.List[float]):
|
||||||
#? ["count"]
|
#? ["count"]
|
||||||
p.c
|
p.c
|
||||||
#? int()
|
#? int()
|
||||||
@@ -180,3 +182,13 @@ def we_can_has_sequence(
|
|||||||
s.c
|
s.c
|
||||||
#? int()
|
#? int()
|
||||||
s[1]
|
s[1]
|
||||||
|
#? []
|
||||||
|
s.a
|
||||||
|
#? ["append"]
|
||||||
|
t.a
|
||||||
|
#? dict()
|
||||||
|
t[1]
|
||||||
|
#? ["append"]
|
||||||
|
u.a
|
||||||
|
#? float()
|
||||||
|
u[1]
|
||||||
|
|||||||
Reference in New Issue
Block a user