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
Next Next commit
more cleanups
  • Loading branch information
sixolet committed Apr 20, 2017
commit 398fbad1b658e9509e80d28ac607fe33a1c3fdf7
14 changes: 14 additions & 0 deletions extensions/mypy_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# the (convenient) behavior of types provided by typing module.
from typing import _type_check # type: ignore


def _check_fails(cls, other):
try:
if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools', 'typing']:
Expand Down Expand Up @@ -93,23 +94,36 @@ class Point2D(TypedDict):
syntax forms work for Python 2.7 and 3.2+
"""


def Arg(typ=Any, name=None):
"""A normal positional argument"""
return typ
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add comment about why it makes sense to just return typ here and elsewhere in this file.



def DefaultArg(typ=Any, name=None):
"""A positional argument with a default value"""
return typ


def NamedArg(typ=Any, name=None):
"""A keyword-only argument"""
return typ


def DefaultNamedArg(typ=Any, name=None):
"""A keyword-only argument with a default value"""
return typ


def VarArg(typ=Any):
"""A *args-style variadic positional argument"""
return typ


def KwArg(typ=Any):
"""A **kwargs-style variadic keyword argument"""
return typ


# Return type that indicates a function does not return
class NoReturn: pass
13 changes: 5 additions & 8 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def make_argument(arg: ast3.arg, default: Optional[ast3.expr], kind: int) -> Arg
new_args.append(make_argument(args.kwarg, None, ARG_STAR2))
names.append(args.kwarg)

def fail_arg(msg: str, arg: ast3.arg):
def fail_arg(msg: str, arg: ast3.arg) -> None:
self.fail(msg, arg.lineno, arg.col_offset)

check_arg_names([name.arg for name in names], names, fail_arg)
Expand Down Expand Up @@ -957,7 +957,7 @@ def __init__(self, errors: Errors, line: int = -1) -> None:
self.line = line
self.node_stack = [] # type: List[ast3.AST]

def visit(self, node):
def visit(self, node) -> Type:
"""Modified visit -- keep track of the stack of nodes"""
self.node_stack.append(node)
try:
Expand Down Expand Up @@ -1021,19 +1021,16 @@ def visit_Call(self, e: ast3.Call) -> Type:
value.lineno, value.col_offset)
return CallableArgument(typ, name, constructor, e.lineno, e.col_offset)


def translate_argument_list(self, l: Sequence[ast3.AST]) -> TypeList:
types = [] # type: List[Type]
names = [] # type: List[Optional[str]]
kinds = [] # type: List[int]
return TypeList([self.visit(e) for e in l], line=self.line)

def _extract_str(self, n: ast3.expr) -> str:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Rename this to be like _extract_argument_name

Figure out handling unicode in python2:

Arg(int, u'eggs')

if isinstance(n, ast3.Str):
return n.s.strip()
elif isinstance(n, ast3.NameConstant) and str(n.value) == 'None':
return None
self.fail('Expected string literal for argument name, got {}'.format(type(n).__name__), self.line, 0)
self.fail('Expected string literal for argument name, got {}'.format(
type(n).__name__), self.line, 0)
return None

def visit_Name(self, n: ast3.Name) -> Type:
Expand Down Expand Up @@ -1087,9 +1084,9 @@ def visit_Ellipsis(self, n: ast3.Ellipsis) -> Type:

# List(expr* elts, expr_context ctx)
def visit_List(self, n: ast3.List) -> Type:
l = len(n.elts)
return self.translate_argument_list(n.elts)


def stringify_name(n: ast3.AST) -> Optional[str]:
if isinstance(n, ast3.Name):
return n.id
Expand Down
7 changes: 4 additions & 3 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,7 @@ def get_member_expr_fullname(expr: MemberExpr) -> str:
if isinstance(obj, type) and issubclass(obj, SymbolNode) and obj is not SymbolNode
}


def check_arg_kinds(arg_kinds: List[int], nodes: List[T], fail: Callable[[str, T], None]) -> None:
is_var_arg = False
is_kw_arg = False
Expand All @@ -2444,8 +2445,8 @@ def check_arg_kinds(arg_kinds: List[int], nodes: List[T], fail: Callable[[str, T
if kind == ARG_POS:
if is_var_arg or is_kw_arg or seen_named or seen_opt:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Change messaging to refer to "var args"

fail("Required positional args may not appear "
"after default, named or star args",
node)
"after default, named or star args",
node)
break
elif kind == ARG_OPT:
if is_var_arg or is_kw_arg or seen_named:
Expand All @@ -2467,7 +2468,7 @@ def check_arg_kinds(arg_kinds: List[int], nodes: List[T], fail: Callable[[str, T


def check_arg_names(names: List[str], nodes: List[T], fail: Callable[[str, T], None],
description: str = 'function definition') -> None:
description: str = 'function definition') -> None:
seen_names = set() # type: Set[str]
for name, node in zip(names, nodes):
if name is not None and name in seen_names:
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ def analyze_callable_type(self, t: UnboundType) -> Type:
names.append(None)
kinds.append(ARG_POS)

check_arg_names(names, [t]*len(args), self.fail, "Callable")
check_arg_kinds(kinds, [t]*len(args), self.fail)
check_arg_names(names, [t] * len(args), self.fail, "Callable")
check_arg_kinds(kinds, [t] * len(args), self.fail)
return CallableType(self.anal_array(args),
kinds,
names,
Expand Down
6 changes: 4 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def deserialize(cls, data: JsonDict) -> 'UnboundType':
return UnboundType(data['name'],
[deserialize_type(a) for a in data['args']])


class CallableArgument(Type):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Docstring here.

typ = None # type: Type
name = None # type: Optional[str]
Expand Down Expand Up @@ -254,6 +255,7 @@ def deserialize(cls, data: JsonDict) -> 'CallableArgument':
name=data['name'],
constructor=data['constructor'])


class TypeList(Type):
"""Information about argument types and names [...].

Expand Down Expand Up @@ -1431,9 +1433,9 @@ def visit_type_list(self, t: TypeList) -> str:
def visit_callable_argument(self, t: CallableArgument) -> str:
typ = t.typ.accept(self)
if t.name is None:
return "{}({})".format(t.constructor, t.typ)
return "{}({})".format(t.constructor, typ)
else:
return "{}({}, {})".format(t.constructor, t.typ, t.name)
return "{}({}, {})".format(t.constructor, typ, t.name)

def visit_any(self, t: AnyType) -> str:
return 'Any'
Expand Down
8 changes: 4 additions & 4 deletions test-data/unit/parse.test
Original file line number Diff line number Diff line change
Expand Up @@ -2392,7 +2392,7 @@ MypyFile:1(
AssignmentStmt:1(
NameExpr(f)
NameExpr(None)
Callable?[<ArgumentList >, None?]))
Callable?[<TypeList >, None?]))

[case testFunctionTypeWithArgument]
f = None # type: Callable[[str], int]
Expand All @@ -2401,7 +2401,7 @@ MypyFile:1(
AssignmentStmt:1(
NameExpr(f)
NameExpr(None)
Callable?[<ArgumentList str?>, int?]))
Callable?[<TypeList str?>, int?]))

[case testFunctionTypeWithTwoArguments]
f = None # type: Callable[[a[b], x.y], List[int]]
Expand All @@ -2410,7 +2410,7 @@ MypyFile:1(
AssignmentStmt:1(
NameExpr(f)
NameExpr(None)
Callable?[<ArgumentList a?[b?], x.y?>, List?[int?]]))
Callable?[<TypeList a?[b?], x.y?>, List?[int?]]))

[case testFunctionTypeWithExtraComma]
def f(x: Callable[[str,], int]): pass
Expand All @@ -2420,7 +2420,7 @@ MypyFile:1(
f
Args(
Var(x))
def (x: Callable?[<ArgumentList str?>, int?]) -> Any
def (x: Callable?[<TypeList str?>, int?]) -> Any
Block:1(
PassStmt:1())))

Expand Down