Skip to content

Commit 0b69630

Browse files
committed
make all tests pass now
1 parent 3f2f617 commit 0b69630

File tree

10 files changed

+36
-32
lines changed

10 files changed

+36
-32
lines changed

mypy/exprtotype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
class TypeTranslationError(Exception):
1414
"""Exception raised when an expression is not valid as a type."""
1515

16+
1617
def _extract_str(expr: Expression) -> Optional[str]:
1718
if isinstance(expr, NameExpr) and expr.name == 'None':
1819
return None
@@ -66,7 +67,7 @@ def expr_to_unanalyzed_type(expr: Expression) -> Type:
6667
except KeyError:
6768
raise TypeTranslationError()
6869
name = None
69-
typ = AnyType(implicit=True)
70+
typ = AnyType(implicit=True) # type: Type
7071
star = arg_const in STAR_ARG_CONSTRUCTORS
7172
for i, arg in enumerate(it.args):
7273
if it.arg_names[i] is not None:

mypy/fastparse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,8 @@ def __init__(self, msg: str, lineno: int, offset: int) -> None:
984984
class FastParserError(TypeCommentParseError):
985985
pass
986986

987-
def _extract_str(arg: ast35.AST) -> Optional[str]:
987+
988+
def _extract_str(arg: ast35.expr) -> Optional[str]:
988989
if isinstance(arg, ast35.Name) and arg.id == 'None':
989990
return None
990991
elif isinstance(arg, ast35.NameConstant) and arg.value is None:

mypy/parse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ def parse_function(self, no_type_checks: bool = False) -> FuncDef:
433433
self.errors.report(
434434
def_tok.line, def_tok.column, 'Function has duplicate type signatures')
435435
sig = cast(CallableType, comment_type)
436+
436437
if sig.is_ellipsis_args:
437438
# When we encounter an ellipsis, fill in the arg_types with
438439
# a bunch of AnyTypes, emulating Callable[..., T]

mypy/parsetype.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from mypy.types import (
88
Type, UnboundType, TupleType, ArgumentList, CallableType, StarType,
9-
EllipsisType, AnyType
9+
EllipsisType, AnyType, ArgNameException, ArgKindException
1010
)
1111

1212
from mypy.sharedparse import ARG_KINDS_BY_CONSTRUCTOR, STAR_ARG_CONSTRUCTORS
@@ -279,6 +279,8 @@ def parse_signature(tokens: List[Token]) -> Tuple[CallableType, int]:
279279
i = 0
280280
if tokens[i].string != '(':
281281
raise TypeParseError(tokens[i], i)
282+
begin = tokens[i]
283+
begin_idx = i
282284
i += 1
283285
arg_types = [] # type: List[Type]
284286
arg_kinds = [] # type: List[int]
@@ -314,8 +316,11 @@ def parse_signature(tokens: List[Token]) -> Tuple[CallableType, int]:
314316
raise TypeParseError(tokens[i], i)
315317
i += 1
316318
ret_type, i = parse_type(tokens, i)
317-
return CallableType(arg_types,
318-
arg_kinds,
319-
[None] * len(arg_types),
320-
ret_type, None,
321-
is_ellipsis_args=encountered_ellipsis), i
319+
try:
320+
return CallableType(arg_types,
321+
arg_kinds,
322+
[None] * len(arg_types),
323+
ret_type, None,
324+
is_ellipsis_args=encountered_ellipsis), i
325+
except (ArgKindException, ArgNameException) as e:
326+
raise TypeParseError(begin, begin_idx, e.message)

mypy/sharedparse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103

104104
STAR_ARG_CONSTRUCTORS = {'StarArg', 'KwArg'}
105105

106+
106107
def special_function_elide_names(name: str) -> bool:
107108
return name in MAGIC_METHODS_POS_ARGS_ONLY
108109

mypy/types.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,20 @@ def deserialize(cls, data: JsonDict) -> 'FunctionLike':
567567
('typ', Type),
568568
('required', bool)])
569569

570+
570571
class ArgKindException(Exception):
572+
"""Raised when the argument kinds are not in the right order"""
571573
message = None # type: str
572-
def __init__(self, message: str):
574+
575+
def __init__(self, message: str) -> None:
573576
self.message = message
574577

578+
575579
class ArgNameException(Exception):
580+
"""Raised when there are duplicate arg names"""
576581
message = None # type: str
577-
def __init__(self, message: str):
582+
583+
def __init__(self, message: str) -> None:
578584
self.message = message
579585

580586

@@ -660,7 +666,8 @@ def _process_kinds_on_init(self, arg_kinds):
660666
"after default, named or star args")
661667
elif kind == ARG_OPT:
662668
if self.is_var_arg or self.is_kw_arg or seen_named:
663-
raise ArgKindException("Positional default args may not appear after named or star args")
669+
raise ArgKindException("Positional default args may not appear "
670+
"after named or star args")
664671
seen_opt = True
665672
elif kind == ARG_STAR:
666673
if self.is_var_arg or self.is_kw_arg or seen_named:

test-data/unit/check-functions.test

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,10 +1412,13 @@ from typing import Callable
14121412
from mypy_extensions import Arg, StarArg, KwArg
14131413

14141414
def WrongArg(x, y): return y
1415-
1416-
F = Callable[[WrongArg('x', int)], int] # E: Invalid type alias
1417-
G = Callable[[Arg('x', 1)], int] # E: Invalid type alias
1418-
H = Callable[[StarArg('x', int)], int] # E: Invalid type alias # E: Too many arguments for "StarArg"
1415+
# Note that for this test, the 'Value of type "int" is not indexable' errors are silly,
1416+
# and a consequence of Callable being set to an int in the test stub. We can't set it to
1417+
# something else sensible, because other tests require the stub not have anything
1418+
# that looks like a function call.
1419+
F = Callable[[WrongArg('x', int)], int] # E: Invalid type alias # E: Value of type "int" is not indexable
1420+
G = Callable[[Arg('x', 1)], int] # E: Invalid type alias # E: Value of type "int" is not indexable
1421+
H = Callable[[StarArg('x', int)], int] # E: Invalid type alias # E: Value of type "int" is not indexable # E: Too many arguments for "StarArg"
14191422
I = Callable[[StarArg(int)], int] # ok
14201423
J = Callable[[StarArg(), KwArg()], int] # ok
14211424
K = Callable[[StarArg(), int], int] # E: Required positional args may not appear after default, named or star args

test-data/unit/lib-stub/typing.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ _promote = 0
1616
NamedTuple = 0
1717
Type = 0
1818
no_type_check = 0
19+
Callable = 0
1920

2021
# Type aliases.
2122
List = 0
@@ -27,11 +28,6 @@ U = TypeVar('U')
2728
V = TypeVar('V')
2829
S = TypeVar('S')
2930

30-
class _Callable(object):
31-
def __getattr__(self, o): pass
32-
33-
Callable = _Callable()
34-
3531
class Container(Generic[T]):
3632
@abstractmethod
3733
# Use int because bool isn't in the default test builtins

test-data/unit/parse-errors.test

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,6 @@ def g(*x): # type: (X) -> Y
287287
file:1: error: Inconsistent use of '*' in function signature
288288
file:3: error: Inconsistent use of '*' in function signature
289289

290-
[case testCommentFunctionAnnotationVarArgMispatch2]
291-
def f(*x, **y): # type: (**X, *Y) -> Z
292-
pass
293-
def g(*x, **y): # type: (*X, *Y) -> Z
294-
pass
295-
[out]
296-
file:1: error: Inconsistent use of '*' in function signature
297-
file:1: error: Inconsistent use of '**' in function signature
298-
file:3: error: Inconsistent use of '*' in function signature
299-
file:3: error: Inconsistent use of '**' in function signature
300-
301290
[case testPrintStatementInPython3]
302291
print 1
303292
[out]

typeshed

Submodule typeshed updated 43 files

0 commit comments

Comments
 (0)