Skip to content

Treat varargs as legal context in default lambda argument #2770

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

Merged
merged 1 commit into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,7 @@ def infer_lambda_type_using_context(self, e: FuncExpr) -> Optional[CallableType]

if ARG_STAR in arg_kinds or ARG_STAR2 in arg_kinds:
# TODO treat this case appropriately
return None
return callable_ctx
if callable_ctx.arg_kinds != arg_kinds:
# Incompatible context; cannot use it to infer types.
self.chk.fail(messages.CANNOT_INFER_LAMBDA_TYPE, e)
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,18 @@ b = lambda: None # type: Callable[[], None]
f(b)
g(b) # E: Argument 1 to "g" has incompatible type Callable[[], None]; expected Callable[[], int]

[case testLambdaDefaultContext]
# flags: --strict-optional
from typing import Callable
def f(a: Callable[..., None] = lambda *a, **k: None):
pass

def g(a: Callable[..., None] = lambda *a, **k: 1): # E: Incompatible return value type (got "int", expected None)
pass
[out]
main:6: error: Incompatible types in assignment (expression has type Callable[[StarArg(Any), KwArg(Any)], int], variable has type Callable[..., None])

[builtins fixtures/dict.pyi]
-- Boolean operators
-- -----------------

Expand Down