Skip to content

Commit ed7d0c0

Browse files
authored
Revert "Treat varargs as legal context in default lambda argument" (#2790)
Here's a snippet that crashes: ``` import warnings warnings.warn = lambda *args, **kwargs: None ``` (This is literally from our codebase, except there we have a `# type: ignore` too.)
1 parent b45e8ea commit ed7d0c0

File tree

2 files changed

+1
-12
lines changed

2 files changed

+1
-12
lines changed

mypy/checkexpr.py

+1-1
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 callable_ctx
1802+
return None
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

-11
Original file line numberDiff line numberDiff line change
@@ -1033,18 +1033,7 @@ 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])
10461036

1047-
[builtins fixtures/dict.pyi]
10481037
-- Boolean operators
10491038
-- -----------------
10501039

0 commit comments

Comments
 (0)