Skip to content

Support per-argument type comment syntax #1738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ def visit_FunctionDef(self, n: ast35.FunctionDef) -> Node:
# for ellipsis arg
if (len(func_type_ast.argtypes) == 1 and
isinstance(func_type_ast.argtypes[0], ast35.Ellipsis)):
arg_types = [AnyType() for a in args]
arg_types = [a.type_annotation if a.type_annotation is not None else AnyType()
for a in args]
else:
arg_types = [a if a is not None else AnyType() for
a in TypeConverter(line=n.lineno).visit_list(func_type_ast.argtypes)]
Expand Down
112 changes: 112 additions & 0 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,115 @@ class C:
@x.setter
def x(self, value: str) -> None: pass
[builtins fixtures/property.py]

[case testFastParsePerArgumentAnnotations]
# flags: fast-parser
class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
class F: pass
def f(a, # type: A
b = None, # type: B
*args, # type: C
d = None, # type: D
e, # type: E
**kwargs # type: F
):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test something about the magic * argument just for completeness?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's a good idea.

reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is '__main__.B'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
reveal_type(d) # E: Revealed type is '__main__.D'
reveal_type(e) # E: Revealed type is '__main__.E'
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
[builtins fixtures/dict.py]
[out]
main: note: In function "f":

[case testFastParsePerArgumentAnnotationsWithReturn]
# flags: fast-parser
class A: pass
class B: pass
class C: pass
class D: pass
class E: pass
class F: pass
def f(a, # type: A
b = None, # type: B
*args, # type: C
d = None, # type: D
e, # type: E
**kwargs # type: F
):
# type: (...) -> int
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is '__main__.B'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
reveal_type(d) # E: Revealed type is '__main__.D'
reveal_type(e) # E: Revealed type is '__main__.E'
reveal_type(kwargs) # E: Revealed type is 'builtins.dict[builtins.str, __main__.F]'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.py]
[out]
main: note: In function "f":

[case testFastParsePerArgumentAnnotationsWithAnnotatedBareStar]
# flags: fast-parser
def f(*, # type: int # E: bare * has associated type comment
x # type: str
):
# type: (...) -> int
pass
[builtins fixtures/dict.py]
[out]

[case testFastParsePerArgumentAnnotationsWithReturnAndBareStar]
# flags: fast-parser
def f(*,
x # type: str
):
# type: (...) -> int
reveal_type(x) # E: Revealed type is 'builtins.str'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.py]
[out]
main: note: In function "f":

[case testFastParsePerArgumentAnnotations_python2]
# flags: fast-parser
class A: pass
class B: pass
class C: pass
class D: pass
def f(a, # type: A
b = None, # type: B
*args # type: C
# kwargs not tested due to lack of 2.7 dict fixtures
):
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is '__main__.B'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
[builtins fixtures/dict.py]
[out]
main: note: In function "f":

[case testFastParsePerArgumentAnnotationsWithReturn_python2]
# flags: fast-parser
class A: pass
class B: pass
class C: pass
class D: pass
def f(a, # type: A
b = None, # type: B
*args # type: C
# kwargs not tested due to lack of 2.7 dict fixtures
):
# type: (...) -> int
reveal_type(a) # E: Revealed type is '__main__.A'
reveal_type(b) # E: Revealed type is '__main__.B'
reveal_type(args) # E: Revealed type is 'builtins.tuple[__main__.C]'
return "not an int" # E: Incompatible return value type (got "str", expected "int")
[builtins fixtures/dict.py]
[out]
main: note: In function "f":
10 changes: 8 additions & 2 deletions test-data/unit/lib-stub/__builtin__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class Any: pass

class object:
def __init__(self) -> None: pass
def __init__(self):
# type: () -> None
pass

class type:
def __init__(self, x) -> None: pass
def __init__(self, x):
# type: (Any) -> None
pass

# These are provided here for convenience.
class int: pass
Expand Down