Skip to content
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

Better callable: Callable[[Arg('x', int), VarArg(str)], int] now a thing you can do #2607

Merged
merged 34 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
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
21 changes: 20 additions & 1 deletion extensions/mypy_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from mypy_extensions import TypedDict
"""

from typing import Any

# NOTE: This module must support Python 2.7 in addition to Python 3.x

import sys
# _type_check is NOT a part of public typing API, it is used here only to mimic
# 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']:
Expand Down Expand Up @@ -88,3 +89,21 @@ class Point2D(TypedDict):
The latter syntax is only supported in Python 3.6+, while two other
syntax forms work for Python 2.7 and 3.2+
"""

def Arg(name=None, typ=Any):
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(name=None, typ=Any):
return typ

def NamedArg(name=None, typ=Any):
return typ

def DefaultNamedArg(name=None, typ=Any):
return typ

def StarArg(typ=Any):
return typ

def KwArg(typ=Any):
return typ
4 changes: 2 additions & 2 deletions mypy/erasetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mypy.types import (
Type, TypeVisitor, UnboundType, ErrorType, AnyType, Void, NoneTyp, TypeVarId,
Instance, TypeVarType, CallableType, TupleType, TypedDictType, UnionType, Overloaded,
ErasedType, PartialType, DeletedType, TypeTranslator, TypeList, UninhabitedType, TypeType
ErasedType, PartialType, DeletedType, TypeTranslator, ArgumentList, UninhabitedType, TypeType
)
from mypy import experiments

Expand Down Expand Up @@ -32,7 +32,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
def visit_error_type(self, t: ErrorType) -> Type:
return t

def visit_type_list(self, t: TypeList) -> Type:
def visit_type_list(self, t: ArgumentList) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
Expand Down
4 changes: 2 additions & 2 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mypy.types import (
Type, Instance, CallableType, TypeVisitor, UnboundType, ErrorType, AnyType,
Void, NoneTyp, TypeVarType, Overloaded, TupleType, TypedDictType, UnionType,
ErasedType, TypeList, PartialType, DeletedType, UninhabitedType, TypeType, TypeVarId
ErasedType, ArgumentList, PartialType, DeletedType, UninhabitedType, TypeType, TypeVarId
)


Expand Down Expand Up @@ -42,7 +42,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
def visit_error_type(self, t: ErrorType) -> Type:
return t

def visit_type_list(self, t: TypeList) -> Type:
def visit_type_list(self, t: ArgumentList) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
Expand Down
57 changes: 53 additions & 4 deletions mypy/exprtotype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

from mypy.nodes import (
Expression, NameExpr, MemberExpr, IndexExpr, TupleExpr,
ListExpr, StrExpr, BytesExpr, UnicodeExpr, EllipsisExpr
ListExpr, StrExpr, BytesExpr, UnicodeExpr, EllipsisExpr, CallExpr,
ARG_POS, ARG_NAMED,
)
from mypy.sharedparse import ARG_KINDS_BY_CONSTRUCTOR, STAR_ARG_CONSTRUCTORS
from mypy.parsetype import parse_str_as_type, TypeParseError
from mypy.types import Type, UnboundType, TypeList, EllipsisType
from mypy.types import Type, UnboundType, ArgumentList, EllipsisType, AnyType, Optional


class TypeTranslationError(Exception):
"""Exception raised when an expression is not valid as a type."""


def _extract_str(expr: Expression) -> Optional[str]:
if isinstance(expr, NameExpr) and expr.name == 'None':
return None
elif isinstance(expr, StrExpr):
return expr.value
else:
raise TypeTranslationError()


def expr_to_unanalyzed_type(expr: Expression) -> Type:
"""Translate an expression to the corresponding type.

The result is not semantically analyzed. It can be UnboundType or TypeList.
The result is not semantically analyzed. It can be UnboundType or ArgumentList.
Raise TypeTranslationError if the expression cannot represent a type.
"""
if isinstance(expr, NameExpr):
Expand Down Expand Up @@ -43,7 +54,45 @@ def expr_to_unanalyzed_type(expr: Expression) -> Type:
else:
raise TypeTranslationError()
elif isinstance(expr, ListExpr):
return TypeList([expr_to_unanalyzed_type(t) for t in expr.items],
types = [] # type: List[Type]
names = [] # type: List[Optional[str]]
kinds = [] # type: List[int]
for it in expr.items:
if isinstance(it, CallExpr):
if not isinstance(it.callee, NameExpr):
raise TypeTranslationError()
arg_const = it.callee.name
try:
kind = ARG_KINDS_BY_CONSTRUCTOR[arg_const]
except KeyError:
raise TypeTranslationError()
name = None
typ = AnyType(implicit=True) # type: Type
star = arg_const in STAR_ARG_CONSTRUCTORS
for i, arg in enumerate(it.args):
if it.arg_names[i] is not None:
if it.arg_names[i] == "name":
name = _extract_str(arg)
continue
elif it.arg_names[i] == "typ":
typ = expr_to_unanalyzed_type(arg)
continue
else:
raise TypeTranslationError()
elif i == 0 and not star:
name = _extract_str(arg)
elif i == 1 and not star or i == 0 and star:
typ = expr_to_unanalyzed_type(arg)
else:
raise TypeTranslationError()
names.append(name)
types.append(typ)
kinds.append(kind)
else:
types.append(expr_to_unanalyzed_type(it))
names.append(None)
kinds.append(ARG_POS)
return ArgumentList(types, names, kinds,
line=expr.line, column=expr.column)
elif isinstance(expr, (StrExpr, BytesExpr, UnicodeExpr)):
# Parse string literal type.
Expand Down
78 changes: 75 additions & 3 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import sys

from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, cast, List
from mypy.sharedparse import special_function_elide_names, argument_elide_name
from mypy.sharedparse import (
special_function_elide_names, argument_elide_name,
ARG_KINDS_BY_CONSTRUCTOR, STAR_ARG_CONSTRUCTORS,
)
from mypy.nodes import (
MypyFile, Node, ImportBase, Import, ImportAll, ImportFrom, FuncDef, OverloadedFuncDef,
ClassDef, Decorator, Block, Var, OperatorAssignmentStmt,
Expand All @@ -19,7 +22,7 @@
ARG_POS, ARG_OPT, ARG_STAR, ARG_NAMED, ARG_NAMED_OPT, ARG_STAR2
)
from mypy.types import (
Type, CallableType, AnyType, UnboundType, TupleType, TypeList, EllipsisType,
Type, CallableType, AnyType, UnboundType, TupleType, ArgumentList, EllipsisType,
)
from mypy import defaults
from mypy import experiments
Expand Down Expand Up @@ -862,6 +865,63 @@ def visit_NoneType(self, n: Any) -> Type:
def translate_expr_list(self, l: Sequence[ast35.AST]) -> List[Type]:
return [self.visit(e) for e in l]

def translate_argument_list(self, l: Sequence[ast35.AST]) -> ArgumentList:
types = [] # type: List[Type]
names = [] # type: List[Optional[str]]
kinds = [] # type: List[int]
for e in l:
if isinstance(e, ast35.Call):
# Parse the arg constructor
f = e.func
if not isinstance(f, ast35.Name):
raise FastParserError("Expected arg constructor name",
e.lineno, e.col_offset)
try:
kind = ARG_KINDS_BY_CONSTRUCTOR[f.id]
star = f.id in STAR_ARG_CONSTRUCTORS
except KeyError:
raise FastParserError("Unknown argument constructor {}".format(f.id),
f.lineno, f.col_offset)

name = None # type: Optional[str]
typ = AnyType(implicit=True) # type: Type
for i, arg in enumerate(e.args):
if i == 0 and not star:
name = _extract_str(arg)
elif i == 1 and not star or i == 0 and star:
try:
typ = self.visit(arg)
except TypeCommentParseError:
raise FastParserError("Bad type for callable argument",
arg.lineno, arg.col_offset)
else:
raise FastParserError("Too many arguments for argument constructor",
f.lineno, f.col_offset)
for k in e.keywords:
value = k.value
if k.arg == "name" and not star:
name = _extract_str(value)
elif k.arg == "typ":
try:
typ = self.visit(value)
except TypeCommentParseError:
raise FastParserError("Bad type for callable argument",
value.lineno, value.col_offset)
else:
raise FastParserError(
'Unexpected argument "{}" for argument constructor'.format(k.arg),
value.lineno, value.col_offset)

types.append(typ)
names.append(name)
kinds.append(kind)
else:
types.append(self.visit(e))
names.append(None)
kinds.append(ARG_POS)

return ArgumentList(types, names, kinds, line=self.line)

def visit_Name(self, n: ast35.Name) -> Type:
return UnboundType(n.id, line=self.line)

Expand Down Expand Up @@ -911,7 +971,7 @@ def visit_Ellipsis(self, n: ast35.Ellipsis) -> Type:

# List(expr* elts, expr_context ctx)
def visit_List(self, n: ast35.List) -> Type:
return TypeList(self.translate_expr_list(n.elts), line=self.line)
return self.translate_argument_list(n.elts)


class TypeCommentParseError(Exception):
Expand All @@ -923,3 +983,15 @@ def __init__(self, msg: str, lineno: int, offset: int) -> None:

class FastParserError(TypeCommentParseError):
pass


def _extract_str(arg: ast35.expr) -> Optional[str]:
if isinstance(arg, ast35.Name) and arg.id == 'None':
return None
elif isinstance(arg, ast35.NameConstant) and arg.value is None:
return None
elif isinstance(arg, ast35.Str):
return arg.s
else:
raise FastParserError("Bad type for name of argument",
arg.lineno, arg.col_offset)
6 changes: 3 additions & 3 deletions mypy/fixup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)
from mypy.types import (
CallableType, EllipsisType, Instance, Overloaded, TupleType, TypedDictType,
TypeList, TypeVarType, UnboundType, UnionType, TypeVisitor,
ArgumentList, TypeVarType, UnboundType, UnionType, TypeVisitor,
TypeType
)
from mypy.visitor import NodeVisitor
Expand Down Expand Up @@ -203,8 +203,8 @@ def visit_typeddict_type(self, tdt: TypedDictType) -> None:
if tdt.fallback is not None:
tdt.fallback.accept(self)

def visit_type_list(self, tl: TypeList) -> None:
for t in tl.items:
def visit_type_list(self, tl: ArgumentList) -> None:
for t in tl.types:
t.accept(self)

def visit_type_var(self, tvt: TypeVarType) -> None:
Expand Down
4 changes: 2 additions & 2 deletions mypy/indirection.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def _visit(self, *typs: types.Type) -> Set[str]:
def visit_unbound_type(self, t: types.UnboundType) -> Set[str]:
return self._visit(*t.args)

def visit_type_list(self, t: types.TypeList) -> Set[str]:
return self._visit(*t.items)
def visit_type_list(self, t: types.ArgumentList) -> Set[str]:
return self._visit(*t.types)

def visit_error_type(self, t: types.ErrorType) -> Set[str]:
return set()
Expand Down
4 changes: 2 additions & 2 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from mypy.types import (
Type, AnyType, NoneTyp, Void, TypeVisitor, Instance, UnboundType,
ErrorType, TypeVarType, CallableType, TupleType, TypedDictType, ErasedType, TypeList,
ErrorType, TypeVarType, CallableType, TupleType, TypedDictType, ErasedType, ArgumentList,
UnionType, FunctionLike, Overloaded, PartialType, DeletedType,
UninhabitedType, TypeType, true_or_false
)
Expand Down Expand Up @@ -115,7 +115,7 @@ def visit_union_type(self, t: UnionType) -> Type:
def visit_error_type(self, t: ErrorType) -> Type:
return t

def visit_type_list(self, t: TypeList) -> Type:
def visit_type_list(self, t: ArgumentList) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
Expand Down
6 changes: 3 additions & 3 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from mypy.join import is_similar_callables, combine_similar_callables, join_type_list
from mypy.types import (
Type, AnyType, TypeVisitor, UnboundType, Void, ErrorType, NoneTyp, TypeVarType,
Instance, CallableType, TupleType, TypedDictType, ErasedType, TypeList, UnionType, PartialType,
DeletedType, UninhabitedType, TypeType
Instance, CallableType, TupleType, TypedDictType, ErasedType, ArgumentList, UnionType,
PartialType, DeletedType, UninhabitedType, TypeType
)
from mypy.subtypes import is_equivalent, is_subtype

Expand Down Expand Up @@ -134,7 +134,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
def visit_error_type(self, t: ErrorType) -> Type:
return t

def visit_type_list(self, t: TypeList) -> Type:
def visit_type_list(self, t: ArgumentList) -> Type:
assert False, 'Not supported'

def visit_any(self, t: AnyType) -> Type:
Expand Down
4 changes: 2 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ def format(self, typ: Type, verbosity: int = 0) -> str:
constructor,
strip_quotes(self.format(arg_type))))
else:
arg_strings.append("{}('{}', {})".format(
arg_strings.append("{}({}, {})".format(
constructor,
arg_name,
repr(arg_name),
strip_quotes(self.format(arg_type))))

return 'Callable[[{}], {}]'.format(", ".join(arg_strings), return_type)
Expand Down
1 change: 1 addition & 0 deletions mypy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ def parse_function(self, no_type_checks: bool = False) -> FuncDef:
self.errors.report(
def_tok.line, def_tok.column, 'Function has duplicate type signatures')
sig = cast(CallableType, comment_type)

if sig.is_ellipsis_args:
# When we encounter an ellipsis, fill in the arg_types with
# a bunch of AnyTypes, emulating Callable[..., T]
Expand Down
Loading