Skip to content

Commit ea29bd6

Browse files
elazargJukkaL
authored andcommitted
treat varargs as legal context (#2770)
Fix #2764. This is somewhat of an ad-hoc fix, but I think it's an improvement. See also #2634.
1 parent 7c102f0 commit ea29bd6

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ def infer_lambda_type_using_context(self, e: FuncExpr) -> Optional[CallableType]
17991799

18001800
if ARG_STAR in arg_kinds or ARG_STAR2 in arg_kinds:
18011801
# TODO treat this case appropriately
1802-
return None
1802+
return callable_ctx
18031803
if callable_ctx.arg_kinds != arg_kinds:
18041804
# Incompatible context; cannot use it to infer types.
18051805
self.chk.fail(messages.CANNOT_INFER_LAMBDA_TYPE, e)

test-data/unit/check-inference.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,18 @@ b = lambda: None # type: Callable[[], None]
10331033
f(b)
10341034
g(b) # E: Argument 1 to "g" has incompatible type Callable[[], None]; expected Callable[[], int]
10351035

1036+
[case testLambdaDefaultContext]
1037+
# flags: --strict-optional
1038+
from typing import Callable
1039+
def f(a: Callable[..., None] = lambda *a, **k: None):
1040+
pass
1041+
1042+
def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible return value type (got "int", expected None)
1043+
pass
1044+
[out]
1045+
main:6: error: Incompatible types in assignment (expression has type Callable[[StarArg(Any), KwArg(Any)], int], variable has type Callable[..., None])
10361046

1047+
[builtins fixtures/dict.pyi]
10371048
-- Boolean operators
10381049
-- -----------------
10391050

0 commit comments

Comments
 (0)