From 67cbc5ebd12a8beb32c80c5dc14dbbaddca408a0 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 30 Dec 2015 20:00:08 +0100 Subject: [PATCH] made code slightly more pytho2 friendly --- jedi/evaluate/jedi_typing.py | 6 ++- jedi/evaluate/pep0484.py | 10 ++--- test/completion/pep0484.py | 67 ---------------------------- test/completion/pep0484_typing.py | 73 +++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 73 deletions(-) create mode 100644 test/completion/pep0484_typing.py diff --git a/jedi/evaluate/jedi_typing.py b/jedi/evaluate/jedi_typing.py index a9fc813c..2c547482 100644 --- a/jedi/evaluate/jedi_typing.py +++ b/jedi/evaluate/jedi_typing.py @@ -3,7 +3,11 @@ 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 +try: + from collections import abc +except ImportError: + # python 2 + import collections as abc def factory(typing_name, indextype): diff --git a/jedi/evaluate/pep0484.py b/jedi/evaluate/pep0484.py index 3730e7bc..4b507a56 100644 --- a/jedi/evaluate/pep0484.py +++ b/jedi/evaluate/pep0484.py @@ -56,7 +56,7 @@ def _fix_forward_reference(evaluator, item, module): element.parent = module return evaluator.eval_element(element) else: - return {item} + return set([item]) @memoize_default(None, evaluator_is_first_arg=True) @@ -95,7 +95,7 @@ def get_types_for_typing_module(evaluator, typ, trailer): # actually the PEP-0484 typing module and not some other indextypes = evaluator.eval_element(trailer.children[1]) if not isinstance(indextypes, set): - indextypes = {indextypes} + indextypes = set([indextypes]) module = trailer.get_parent_until() dereferencedindextypes = set() @@ -109,9 +109,9 @@ def get_types_for_typing_module(evaluator, typ, trailer): factory = list(factories)[0] assert factory function_body_nodes = factory.children[4].children - valid_classnames = {child.name.value - for child in function_body_nodes - if isinstance(child, tree.Class)} + valid_classnames = set(child.name.value + for child in function_body_nodes + if isinstance(child, tree.Class)) if typ.name.value not in valid_classnames: return None compiled_classname = compiled.create(evaluator, typ.name.value) diff --git a/test/completion/pep0484.py b/test/completion/pep0484.py index f5c2f93d..fc08460f 100644 --- a/test/completion/pep0484.py +++ b/test/completion/pep0484.py @@ -157,70 +157,3 @@ Y = int def just_because_we_can(x: "flo" + "at"): #? float() x - -import typing -def we_can_has_sequence( - p: typing.Sequence[int], - q: typing.Sequence[B], - r: "typing.Sequence[int]", - s: typing.Sequence["int"], - t: typing.MutableSequence[dict], - u: typing.List[float]): - #? ["count"] - p.c - #? int() - p[1] - #? ["count"] - q.c - #? B() - q[1] - #? ["count"] - r.c - #? int() - r[1] - #? ["count"] - s.c - #? int() - s[1] - #? [] - s.a - #? ["append"] - t.a - #? dict() - t[1] - #? ["append"] - u.a - #? float() - u[1] - -def iterators( - ps: typing.Iterable[int], - qs: typing.Iterator[str], - rs: typing.Sequence["B"], - ts: typing.AbstractSet["float"]): - for p in ps: - #? int() - p - #? - next(ps) - for q in qs: - #? str() - q - #? str() - next(qs) - for r in rs: - #? B() - r - #? - next(rs) - for t in ts: - #? float() - t - -def sets( - p: typing.AbstractSet[int], - q: typing.MutableSet[float]): - #? [] - p.a - #? ["add"] - q.a diff --git a/test/completion/pep0484_typing.py b/test/completion/pep0484_typing.py new file mode 100644 index 00000000..ef5ff908 --- /dev/null +++ b/test/completion/pep0484_typing.py @@ -0,0 +1,73 @@ +# python >= 3.2 +import typing +class B: + pass + +def we_can_has_sequence( + p: typing.Sequence[int], + q: typing.Sequence[B], + r: "typing.Sequence[int]", + s: typing.Sequence["int"], + t: typing.MutableSequence[dict], + u: typing.List[float]): + #? ["count"] + p.c + #? int() + p[1] + #? ["count"] + q.c + #? B() + q[1] + #? ["count"] + r.c + #? int() + r[1] + #? ["count"] + s.c + #? int() + s[1] + #? [] + s.a + #? ["append"] + t.a + #? dict() + t[1] + #? ["append"] + u.a + #? float() + u[1] + +def iterators( + ps: typing.Iterable[int], + qs: typing.Iterator[str], + rs: typing.Sequence["ForwardReference"], + ts: typing.AbstractSet["float"]): + for p in ps: + #? int() + p + #? + next(ps) + for q in qs: + #? str() + q + #? str() + next(qs) + for r in rs: + #? ForwardReference() + r + #? + next(rs) + for t in ts: + #? float() + t + +def sets( + p: typing.AbstractSet[int], + q: typing.MutableSet[float]): + #? [] + p.a + #? ["add"] + q.a + +class ForwardReference: + pass