From 68a28d640b20897a86d6bb247a6c35fec81583d6 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Wed, 22 Jun 2016 13:14:11 -0700 Subject: [PATCH 1/4] Support per-argument type comment syntax --- mypy/fastparse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index 987fec5cd472..cff434de7a41 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -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)] From 6560fb6b2f072bf087bb2df0519c3c3aea234da2 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Wed, 22 Jun 2016 14:37:53 -0700 Subject: [PATCH 2/4] Add tests --- test-data/unit/check-fastparse.test | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test index ce7f716a4a2f..8c2c01ad1b36 100644 --- a/test-data/unit/check-fastparse.test +++ b/test-data/unit/check-fastparse.test @@ -42,3 +42,55 @@ 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 + ): + 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": From 3f84b463828cfa5dc1913078d04231c36c261e17 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Wed, 22 Jun 2016 16:40:00 -0700 Subject: [PATCH 3/4] add bare * argument tests --- test-data/unit/check-fastparse.test | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test index 8c2c01ad1b36..98ab8e3cc39f 100644 --- a/test-data/unit/check-fastparse.test +++ b/test-data/unit/check-fastparse.test @@ -94,3 +94,25 @@ def f(a, # type: A [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": From 676532ef1edbbee008933cd1ab9b21918b39713b Mon Sep 17 00:00:00 2001 From: David Fisher Date: Thu, 23 Jun 2016 15:39:16 -0700 Subject: [PATCH 4/4] Add Python 2.7 tests --- test-data/unit/check-fastparse.test | 38 ++++++++++++++++++++++++++ test-data/unit/lib-stub/__builtin__.py | 10 +++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test index 98ab8e3cc39f..26aa79cd14c8 100644 --- a/test-data/unit/check-fastparse.test +++ b/test-data/unit/check-fastparse.test @@ -116,3 +116,41 @@ def f(*, [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": diff --git a/test-data/unit/lib-stub/__builtin__.py b/test-data/unit/lib-stub/__builtin__.py index 424e317ec20a..11540f50d70e 100644 --- a/test-data/unit/lib-stub/__builtin__.py +++ b/test-data/unit/lib-stub/__builtin__.py @@ -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