Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
249d70f
Implement Callable[[Arg('name', Type)], ret] syntax
sixolet Nov 11, 2016
7ecdcc1
General cleanup
sixolet Dec 24, 2016
066bd5e
Change tests back to match old behavior
sixolet Dec 24, 2016
213944b
lots of lint
sixolet Dec 24, 2016
0e19070
Oh god I am tired of writing parsers
sixolet Dec 25, 2016
3f2f617
Tighten fastparse a little
sixolet Dec 25, 2016
0b69630
make all tests pass now
sixolet Dec 25, 2016
f4ccf92
go back to master version of typeshed I guess
sixolet Dec 25, 2016
967bb5a
Meged master, but tests fail again now.
sixolet Apr 12, 2017
bb5134e
Tests all pass again after merge
sixolet Apr 18, 2017
d4a83e1
Merged master again
sixolet Apr 18, 2017
54a5da9
Big refactor. Wait until semanal to get arg kinds, switch order again…
sixolet Apr 20, 2017
e79c527
Change back to TypeList
sixolet Apr 20, 2017
52ffe5c
Cleanups. Preparing to split into two diffs maybe?
sixolet Apr 20, 2017
06416f7
update typeshed to master version
sixolet Apr 20, 2017
398fbad
more cleanups
sixolet Apr 20, 2017
2c9ce02
should not have changed these test files
sixolet Apr 20, 2017
51c6f56
Semanal needs to be a SyntheticTypeVisitor
sixolet Apr 20, 2017
5e679a3
Annot
sixolet Apr 20, 2017
0926fe9
Oops
sixolet Apr 20, 2017
288a8be
Add testing for exprtotype Arg constructors in wierd places
sixolet Apr 20, 2017
6e67ab2
Remove some ill-modified modifications to tests
sixolet Apr 20, 2017
97a859b
Merge master, no longer depend on other PR
sixolet Apr 20, 2017
1c7d4c6
Jukka comments
sixolet Apr 21, 2017
f153850
Synthetic types don't serialize
sixolet Apr 21, 2017
be954f5
Remove unused instance var
sixolet Apr 21, 2017
07ae917
Merge master
sixolet Apr 22, 2017
1b97362
Revert "Remove unused instance var"
sixolet Apr 22, 2017
552f49e
Accessing TypeList types directly is not required
sixolet Apr 22, 2017
f2e3663
Undo changes to this file they were not required
sixolet Apr 22, 2017
27e2a9d
lint
sixolet Apr 22, 2017
793a663
Merge master again
sixolet Apr 22, 2017
3d212b3
Merge master
sixolet May 1, 2017
0780149
Disallow CallableArgument in exprtotype outside a TypeList
sixolet May 1, 2017
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
Prev Previous commit
Disallow CallableArgument in exprtotype outside a TypeList
Also add a lot more tests.
  • Loading branch information
sixolet committed May 1, 2017
commit 0780149243094f9094bd48131a2c3c35200e429c
17 changes: 9 additions & 8 deletions mypy/exprtotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def _extract_argument_name(expr: Expression) -> Optional[str]:
raise TypeTranslationError()


def expr_to_unanalyzed_type(expr: Expression) -> Type:
def expr_to_unanalyzed_type(expr: Expression, _parent: Optional[Expression] = None) -> Type:
"""Translate an expression to the corresponding type.

The result is not semantically analyzed. It can be UnboundType or TypeList.
Raise TypeTranslationError if the expression cannot represent a type.
"""
# The `parent` paremeter is used in recursive calls to provide context for
# understanding whether an CallableArgument is ok.
if isinstance(expr, NameExpr):
name = expr.name
return UnboundType(name, line=expr.line, column=expr.column)
Expand All @@ -42,22 +44,21 @@ def expr_to_unanalyzed_type(expr: Expression) -> Type:
else:
raise TypeTranslationError()
elif isinstance(expr, IndexExpr):
base = expr_to_unanalyzed_type(expr.base)
base = expr_to_unanalyzed_type(expr.base, expr)
if isinstance(base, UnboundType):
if base.args:
raise TypeTranslationError()
if isinstance(expr.index, TupleExpr):
args = expr.index.items
else:
args = [expr.index]
base.args = [expr_to_unanalyzed_type(arg) for arg in args]
base.args = [expr_to_unanalyzed_type(arg, expr) for arg in args]
if not base.args:
base.empty_tuple_index = True
return base
else:
raise TypeTranslationError()
elif isinstance(expr, CallExpr):

elif isinstance(expr, CallExpr) and isinstance(_parent, ListExpr):
c = expr.callee
names = []
# Go through the dotted member expr chain to get the full arg
Expand Down Expand Up @@ -89,19 +90,19 @@ def expr_to_unanalyzed_type(expr: Expression) -> Type:
if typ is not default_type:
# Two types
raise TypeTranslationError()
typ = expr_to_unanalyzed_type(arg)
typ = expr_to_unanalyzed_type(arg, expr)
continue
else:
raise TypeTranslationError()
elif i == 0:
typ = expr_to_unanalyzed_type(arg)
typ = expr_to_unanalyzed_type(arg, expr)
elif i == 1:
name = _extract_argument_name(arg)
else:
raise TypeTranslationError()
return CallableArgument(typ, name, arg_const, expr.line, expr.column)
elif isinstance(expr, ListExpr):
return TypeList([expr_to_unanalyzed_type(t) for t in expr.items],
return TypeList([expr_to_unanalyzed_type(t, expr) for t in expr.items],
line=expr.line, column=expr.column)
elif isinstance(expr, (StrExpr, BytesExpr, UnicodeExpr)):
# Parse string literal type.
Expand Down
3 changes: 3 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,9 @@ def check_arg_kinds(arg_kinds: List[int], nodes: List[T], fail: Callable[[str, T
is_var_arg = True
elif kind == ARG_NAMED or kind == ARG_NAMED_OPT:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we shouldn't allow these after **kwargs, since Python doesn't seem to allow anything after **kwargs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

seen_named = True
if is_kw_arg:
fail("A **kwargs argument must be the last argument", node)
break
elif kind == ARG_STAR2:
if is_kw_arg:
fail("You may only have one **kwargs argument", node)
Expand Down
43 changes: 41 additions & 2 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,19 @@ def a(f: F):
f("foo") # E: Argument 1 has incompatible type "str"; expected "int"
[builtins fixtures/dict.pyi]

[case testCallableParsingInInheritence]

from collections import namedtuple
class C(namedtuple('t', 'x')):
pass

[case testCallableParsingSameName]
from typing import Callable

def Arg(x, y): pass

F = Callable[[Arg(int, 'x')], int] # E: Invalid argument constructor "__main__.Arg"

[case testCallableParsingFromExpr]
from typing import Callable, List
from mypy_extensions import Arg, VarArg, KwArg
Expand All @@ -1466,7 +1479,7 @@ L = Callable[[Arg(name='x', type=int)], int] # ok
# I have commented out the following test because I don't know how to expect the "defined here" note part of the error.
# M = Callable[[Arg(gnome='x', type=int)], int] E: Invalid type alias E: Unexpected keyword argument "gnome" for "Arg"
N = Callable[[Arg(name=None, type=int)], int] # ok
O = Callable[[List[Arg(int)]], int] # E: Invalid type
O = Callable[[List[Arg(int)]], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: Type expected within [...] # E: The type List[T] is not generic and not indexable
P = Callable[[mypy_extensions.VarArg(int)], int] # ok
Q = Callable[[Arg(int, type=int)], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: "Arg" gets multiple values for keyword argument "type"
R = Callable[[Arg(int, 'x', name='y')], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: "Arg" gets multiple values for keyword argument "name"
Expand Down Expand Up @@ -1549,7 +1562,7 @@ def g(f: Callable[[VarArg(), VarArg()], int]): pass # E: Var args may not appear
def h(f: Callable[[KwArg(), KwArg()], int]): pass # E: You may only have one **kwargs argument
def i(f: Callable[[DefaultArg(), int], int]): pass # E: Required positional args may not appear after default, named or var args
def j(f: Callable[[NamedArg(Any, 'x'), DefaultArg(int, 'y')], int]): pass # E: Positional default args may not appear after named or var args

def k(f: Callable[[KwArg(), NamedArg(Any, 'x')], int]): pass # E: A **kwargs argument must be the last argument
[builtins fixtures/dict.pyi]

[case testCallableDuplicateNames]
Expand Down Expand Up @@ -1585,6 +1598,32 @@ def a(f: Callable[[DefaultNamedArg(int, 'x')], int]):
f(x="foo") # E: Argument 1 has incompatible type "str"; expected "int"
[builtins fixtures/dict.pyi]

[case testCallableWithKwargs]
from typing import Callable
from mypy_extensions import KwArg

def a(f: Callable[[KwArg(int)], int]):
f(x=4)
f(2) # E: Too many arguments
f()
f(y=3)
f(x=4, y=3, z=10)
f(x="foo") # E: Argument 1 has incompatible type "str"; expected "int"
[builtins fixtures/dict.pyi]


[case testCallableWithVarArg]
from typing import Callable
from mypy_extensions import VarArg

def a(f: Callable[[VarArg(int)], int]):
f(x=4) # E: Unexpected keyword argument "x"
f(2)
f()
f(3, 4, 5)
f("a") # E: Argument 1 has incompatible type "str"; expected "int"
[builtins fixtures/dict.pyi]

[case testCallableArgKindSubtyping]
from typing import Callable
from mypy_extensions import Arg, DefaultArg
Expand Down