1
0
forked from VimPlug/jedi

Support mypy annotations using comment syntax

This allows us to use mypy annotations for completion in Python 2.

Closes #946
This commit is contained in:
Lee Danilek
2017-07-24 19:01:56 -07:00
committed by Wilfred Hughes
parent d0b8f9e5a2
commit b9903ede1b
3 changed files with 145 additions and 2 deletions

View File

@@ -47,7 +47,7 @@ b
class Employee:
pass
from typing import List
from typing import List, Tuple
x = [] # type: List[Employee]
#? Employee()
x[1]
@@ -103,3 +103,67 @@ aaa = some_extremely_long_function_name_that_doesnt_leave_room_for_hints() \
# type: float # We should be able to put hints on the next line with a \
#? float()
aaa
# Test instance methods
class Dog:
def __init__(self, age, friends, name):
# type: (int, List[Tuple[str, Dog]], str) -> None
#? int()
self.age = age
self.friends = friends
#? Dog()
friends[0][1]
#? str()
self.name = name
def friend_for_name(self, name):
# type: (str) -> Dog
for (friend_name, friend) in self.friends:
if friend_name == name:
return friend
raise ValueError()
def bark(self):
pass
buddy = Dog(UNKNOWN_NAME1, UNKNOWN_NAME2, UNKNOWN_NAME3)
friend = buddy.friend_for_name('buster')
# type of friend is determined by function return type
#! 9 ['def bark']
friend.bark()
friend = buddy.friends[0][1]
# type of friend is determined by function parameter type
#! 9 ['def bark']
friend.bark()
# type is determined by function parameter type following nested generics
#? str()
friend.name
# Mypy comment describing function return type.
def annot():
# type: () -> str
pass
#? str()
annot()
# Mypy variable type annotation.
x = UNKNOWN_NAME2 # type: str
#? str()
x
class Cat(object):
def __init__(self, age, friends, name):
# type: (int, List[Dog], str) -> None
self.age = age
self.friends = friends
self.name = name
cat = Cat(UNKNOWN_NAME4, UNKNOWN_NAME5, UNKNOWN_NAME6)
#? str()
cat.name