diff --git a/mypy/fastparse.py b/mypy/fastparse.py index f68d1b0279d09..778401a389935 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -296,6 +296,10 @@ def do_func_def(self, n: Union[ast35.FunctionDef, ast35.AsyncFunctionDef], func_type = None if any(arg_types) or return_type: + if len(arg_types) > len(arg_kinds): + raise FastParserError('Type signature has too many arguments', n.lineno, offset=0) + if len(arg_types) < len(arg_kinds): + raise FastParserError('Type signature has too few arguments', n.lineno, offset=0) func_type = CallableType([a if a is not None else AnyType() for a in arg_types], arg_kinds, arg_names, diff --git a/mypy/fastparse2.py b/mypy/fastparse2.py index 76e95bb4cd55d..19af86c57876d 100644 --- a/mypy/fastparse2.py +++ b/mypy/fastparse2.py @@ -53,6 +53,7 @@ print('The typed_ast package required by --fast-parser is only compatible with' ' Python 3.3 and greater.') sys.exit(1) +from mypy.fastparse import FastParserError T = TypeVar('T', bound=Union[ast27.expr, ast27.stmt]) U = TypeVar('U', bound=Node) @@ -302,6 +303,10 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement: func_type = None if any(arg_types) or return_type: + if len(arg_types) > len(arg_kinds): + raise FastParserError('Type signature has too many arguments', n.lineno, offset=0) + if len(arg_types) < len(arg_kinds): + raise FastParserError('Type signature has too few arguments', n.lineno, offset=0) func_type = CallableType([a if a is not None else AnyType() for a in arg_types], arg_kinds, arg_names, diff --git a/mypy/parse.py b/mypy/parse.py index 02aa292ecbbf9..f5fa404e32752 100644 --- a/mypy/parse.py +++ b/mypy/parse.py @@ -456,6 +456,10 @@ def parse_function(self, no_type_checks: bool = False) -> FuncDef: else: self.check_argument_kinds(arg_kinds, sig.arg_kinds, def_tok.line, def_tok.column) + if len(sig.arg_types) > len(arg_kinds): + raise ParseError('Type signature has too many arguments') + if len(sig.arg_types) < len(arg_kinds): + raise ParseError('Type signature has too few arguments') typ = CallableType( sig.arg_types, arg_kinds, diff --git a/mypy/types.py b/mypy/types.py index 9c80b590cd38e..8ea267731f829 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -576,6 +576,7 @@ def __init__(self, ) -> None: if variables is None: variables = [] + assert len(arg_types) == len(arg_kinds) self.arg_types = arg_types self.arg_kinds = arg_kinds self.arg_names = arg_names diff --git a/test-data/unit/check-fastparse.test b/test-data/unit/check-fastparse.test index b0f0281457166..3aa66d518bdaa 100644 --- a/test-data/unit/check-fastparse.test +++ b/test-data/unit/check-fastparse.test @@ -160,16 +160,12 @@ main: note: In function "f": def f(): # E: Type signature has too many arguments # type: (int) -> None pass -[out] -main: note: In function "f": [case testFasterParseTooFewArgumentsAnnotation] # flags: --fast-parser def f(x): # E: Type signature has too few arguments # type: () -> None pass -[out] -main: note: In function "f": [case testFasterParseTypeCommentError_python2] # flags: --fast-parser