python 2.7 compatibility, typing module tested with docstring, so that it can also be tested in python 2.7

This commit is contained in:
Claude
2016-01-23 22:53:48 +01:00
parent c9bf521efd
commit e267f63657
2 changed files with 78 additions and 53 deletions

View File

@@ -27,6 +27,7 @@ from jedi.evaluate.cache import memoize_default
from jedi.common import unite from jedi.common import unite
from jedi.evaluate import compiled from jedi.evaluate import compiled
from jedi import debug from jedi import debug
from jedi import _compatibility
def _evaluate_for_annotation(evaluator, annotation): def _evaluate_for_annotation(evaluator, annotation):
@@ -49,7 +50,8 @@ def _fix_forward_reference(evaluator, node):
if isinstance(evaled_node, compiled.CompiledObject) and \ if isinstance(evaled_node, compiled.CompiledObject) and \
isinstance(evaled_node.obj, str): isinstance(evaled_node.obj, str):
try: try:
p = Parser(load_grammar(), evaled_node.obj, start='eval_input') p = Parser(load_grammar(), _compatibility.unicode(evaled_node.obj),
start='eval_input')
newnode = p.get_parsed_node() newnode = p.get_parsed_node()
except ParseError: except ParseError:
debug.warning('Annotation not parsed: %s' % evaled_node.obj) debug.warning('Annotation not parsed: %s' % evaled_node.obj)
@@ -84,7 +86,7 @@ def _get_typing_replacement_module():
typing_path = os.path.abspath(os.path.join(__file__, "../jedi_typing.py")) typing_path = os.path.abspath(os.path.join(__file__, "../jedi_typing.py"))
with open(typing_path) as f: with open(typing_path) as f:
code = f.read() code = _compatibility.unicode(f.read())
p = ParserWithRecovery(load_grammar(), code) p = ParserWithRecovery(load_grammar(), code)
return p.module return p.module

View File

@@ -1,15 +1,21 @@
# python >= 3.2 """
Test the typing library, with docstrings. This is needed since annotations
are not supported in python 2.7 else then annotating by comment (and this is
still TODO at 2016-01-23)
"""
import typing import typing
class B: class B:
pass pass
def we_can_has_sequence( def we_can_has_sequence(p, q, r, s, t, u):
p: typing.Sequence[int], """
q: typing.Sequence[B], :type p: typing.Sequence[int]
r: "typing.Sequence[int]", :type q: typing.Sequence[B]
s: typing.Sequence["int"], :type r: typing.Sequence[int]
t: typing.MutableSequence[dict], :type s: typing.Sequence["int"]
u: typing.List[float]): :type t: typing.MutableSequence[dict]
:type u: typing.List[float]
"""
#? ["count"] #? ["count"]
p.c p.c
#? int() #? int()
@@ -37,11 +43,13 @@ def we_can_has_sequence(
#? float() #? float()
u[1] u[1]
def iterators( def iterators(ps, qs, rs, ts):
ps: typing.Iterable[int], """
qs: typing.Iterator[str], :type ps: typing.Iterable[int]
rs: typing.Sequence["ForwardReference"], :type qs: typing.Iterator[str]
ts: typing.AbstractSet["float"]): :type rs: typing.Sequence["ForwardReference"]
:type ts: typing.AbstractSet["float"]
"""
for p in ps: for p in ps:
#? int() #? int()
p p
@@ -71,18 +79,22 @@ def iterators(
#? float() #? float()
t t
def sets( def sets(p, q):
p: typing.AbstractSet[int], """
q: typing.MutableSet[float]): :type p: typing.AbstractSet[int]
:type q: typing.MutableSet[float]
"""
#? [] #? []
p.a p.a
#? ["add"] #? ["add"]
q.a q.a
def tuple( def tuple(p, q, r):
p: typing.Tuple[int], """
q: typing.Tuple[int, str, float], :type p: typing.Tuple[int]
r: typing.Tuple[B, ...]): :type q: typing.Tuple[int, str, float]
:type r: typing.Tuple[B, ...]
"""
#? int() #? int()
p[0] p[0]
#? int() #? int()
@@ -117,13 +129,15 @@ class Key:
class Value: class Value:
pass pass
def mapping( def mapping(p, q, d, r, s, t):
p: typing.Mapping[Key, Value], """
q: typing.MutableMapping[Key, Value], :type p: typing.Mapping[Key, Value]
d: typing.Dict[Key, Value], :type q: typing.MutableMapping[Key, Value]
r: typing.KeysView[Key], :type d: typing.Dict[Key, Value]
s: typing.ValuesView[Value], :type r: typing.KeysView[Key]
t: typing.ItemsView[Key, Value]): :type s: typing.ValuesView[Value]
:type t: typing.ItemsView[Key, Value]
"""
#? [] #? []
p.setd p.setd
#? ["setdefault"] #? ["setdefault"]
@@ -168,12 +182,14 @@ def mapping(
#? Value() #? Value()
value value
def union( def union(p, q, r, s, t):
p: typing.Union[int], """
q: typing.Union[int, int], :type p: typing.Union[int]
r: typing.Union[int, str, "int"], :type q: typing.Union[int, int]
s: typing.Union[int, typing.Union[str, "typing.Union['float', 'dict']"]], :type r: typing.Union[int, str, "int"]
t: typing.Union[int, None]): :type s: typing.Union[int, typing.Union[str, "typing.Union['float', 'dict']"]]
:type t: typing.Union[int, None]
"""
#? int() #? int()
p p
#? int() #? int()
@@ -185,9 +201,9 @@ def union(
#? int() #? int()
t t
def optional( def optional(p):
p: typing.Optional[int]):
""" """
:type p: typing.Optional[int]
Optional does not do anything special. However it should be recognised Optional does not do anything special. However it should be recognised
as being of that type. Jedi doesn't do anything with the extra into that as being of that type. Jedi doesn't do anything with the extra into that
it can be None as well it can be None as well
@@ -198,26 +214,14 @@ def optional(
class ForwardReference: class ForwardReference:
pass pass
import typing as t
def union2(x: t.Union[int, str]):
#? int() str()
x
from typing import Union
def union3(x: Union[int, str]):
#? int() str()
x
from typing import Union as U
def union4(x: U[int, str]):
#? int() str()
x
class TestDict(typing.Dict[str, int]): class TestDict(typing.Dict[str, int]):
def setdud(self): def setdud(self):
pass pass
def testdict(x: TestDict): def testdict(x):
"""
:type x: TestDict
"""
#? ["setdud", "setdefault"] #? ["setdud", "setdefault"]
x.setd x.setd
for key in x.keys(): for key in x.keys():
@@ -236,3 +240,22 @@ for key in x.keys():
for value in x.values(): for value in x.values():
#? int() #? int()
value value
# python >= 3.2
"""
docstrings have some auto-import, annotations can use all of Python's
import logic
"""
import typing as t
def union2(x: t.Union[int, str]):
#? int() str()
x
from typing import Union
def union3(x: Union[int, str]):
#? int() str()
x
from typing import Union as U
def union4(x: U[int, str]):
#? int() str()
x