Skip to content

Do not construct malformed CallableType #2391

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 1 commit into from
Nov 4, 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
4 changes: 4 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions mypy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test-data/unit/check-fastparse.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down