Skip to content

Commit

Permalink
Merge cc234f9 into 17a1a0e
Browse files Browse the repository at this point in the history
  • Loading branch information
reinhrst committed Jan 31, 2016
2 parents 17a1a0e + cc234f9 commit 887279f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jedi/evaluate/finder.py
Expand Up @@ -355,6 +355,9 @@ def _remove_statements(evaluator, stmt, name):
check_instance = stmt.instance
stmt = stmt.var

pep0484types = pep0484.find_type_from_comment_hint(evaluator, stmt)
if pep0484types:
return pep0484types
types |= evaluator.eval_statement(stmt, seek_name=name)

if check_instance is not None:
Expand Down
28 changes: 28 additions & 0 deletions jedi/evaluate/pep0484.py
Expand Up @@ -28,6 +28,7 @@
from jedi.evaluate import compiled
from jedi import debug
from jedi import _compatibility
import re


def _evaluate_for_annotation(evaluator, annotation):
Expand Down Expand Up @@ -136,3 +137,30 @@ def get_types_for_typing_module(evaluator, typ, node):

result = evaluator.execute_evaluated(factory, compiled_classname, args)
return result


def find_type_from_comment_hint(evaluator, stmt):
try:
stmtpos = stmt.parent.children.index(stmt)
except ValueError:
return []
try:
next_sibling = stmt.parent.children[stmtpos + 1]
except IndexError:
return []
if not isinstance(next_sibling, tree.Whitespace):
return []
comment = next_sibling.get_pre_comment()
if comment is None:
return []
match = re.match(r"\s*type:\s*([^#]*)", comment)
if not match:
return []
start_pos = (next_sibling.start_pos[0],
next_sibling.start_pos[1] - len(comment))
annotation = tree.String(
tree.zero_position_modifier,
repr(str(match.group(1).strip())),
start_pos)
annotation.parent = stmt.parent
return _evaluate_for_annotation(evaluator, annotation)
9 changes: 9 additions & 0 deletions jedi/parser/tree.py
Expand Up @@ -280,6 +280,15 @@ def prev_sibling(self):
def nodes_to_execute(self, last_added=False):
return []

def get_pre_comment(self):
"""
returns comment before this leaf, excluding #, or None if no comment
"""
match = re.match(r"\s*#(.*)$", self.prefix)
if match:
return match.group(1)
return None

@utf8_repr
def __repr__(self):
return "<%s: %s>" % (type(self).__name__, self.value)
Expand Down
24 changes: 24 additions & 0 deletions test/completion/pep0484.py
Expand Up @@ -157,3 +157,27 @@ def function_with_assined_class_in_reference(x: X, y: "Y"):
def just_because_we_can(x: "flo" + "at"):
#? float()
x

# python >= 2.6

x = 3 # type: str
#? str()
x

y = 3 # type: str but I write more
#? int()
y

z = 3 # type: str # I comment more
#? str()
z

class BB: pass

def test(a, b):
a = a # type: BB
c = a # type: str
#? BB()
a
#? str()
c

0 comments on commit 887279f

Please sign in to comment.