Skip to content

Commit 9579d15

Browse files
elazarggvanrossum
authored andcommitted
do not construct malformed CallableType (#2391)
1 parent 617903a commit 9579d15

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

mypy/fastparse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ def do_func_def(self, n: Union[ast35.FunctionDef, ast35.AsyncFunctionDef],
296296

297297
func_type = None
298298
if any(arg_types) or return_type:
299+
if len(arg_types) > len(arg_kinds):
300+
raise FastParserError('Type signature has too many arguments', n.lineno, offset=0)
301+
if len(arg_types) < len(arg_kinds):
302+
raise FastParserError('Type signature has too few arguments', n.lineno, offset=0)
299303
func_type = CallableType([a if a is not None else AnyType() for a in arg_types],
300304
arg_kinds,
301305
arg_names,

mypy/fastparse2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
print('The typed_ast package required by --fast-parser is only compatible with'
5454
' Python 3.3 and greater.')
5555
sys.exit(1)
56+
from mypy.fastparse import FastParserError
5657

5758
T = TypeVar('T', bound=Union[ast27.expr, ast27.stmt])
5859
U = TypeVar('U', bound=Node)
@@ -302,6 +303,10 @@ def visit_FunctionDef(self, n: ast27.FunctionDef) -> Statement:
302303

303304
func_type = None
304305
if any(arg_types) or return_type:
306+
if len(arg_types) > len(arg_kinds):
307+
raise FastParserError('Type signature has too many arguments', n.lineno, offset=0)
308+
if len(arg_types) < len(arg_kinds):
309+
raise FastParserError('Type signature has too few arguments', n.lineno, offset=0)
305310
func_type = CallableType([a if a is not None else AnyType() for a in arg_types],
306311
arg_kinds,
307312
arg_names,

mypy/parse.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ def parse_function(self, no_type_checks: bool = False) -> FuncDef:
456456
else:
457457
self.check_argument_kinds(arg_kinds, sig.arg_kinds,
458458
def_tok.line, def_tok.column)
459+
if len(sig.arg_types) > len(arg_kinds):
460+
raise ParseError('Type signature has too many arguments')
461+
if len(sig.arg_types) < len(arg_kinds):
462+
raise ParseError('Type signature has too few arguments')
459463
typ = CallableType(
460464
sig.arg_types,
461465
arg_kinds,

mypy/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ def __init__(self,
576576
) -> None:
577577
if variables is None:
578578
variables = []
579+
assert len(arg_types) == len(arg_kinds)
579580
self.arg_types = arg_types
580581
self.arg_kinds = arg_kinds
581582
self.arg_names = arg_names

test-data/unit/check-fastparse.test

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,12 @@ main: note: In function "f":
160160
def f(): # E: Type signature has too many arguments
161161
# type: (int) -> None
162162
pass
163-
[out]
164-
main: note: In function "f":
165163

166164
[case testFasterParseTooFewArgumentsAnnotation]
167165
# flags: --fast-parser
168166
def f(x): # E: Type signature has too few arguments
169167
# type: () -> None
170168
pass
171-
[out]
172-
main: note: In function "f":
173169

174170
[case testFasterParseTypeCommentError_python2]
175171
# flags: --fast-parser

0 commit comments

Comments
 (0)