Skip to content
Open
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
11 changes: 10 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6181,7 +6181,16 @@ def visit_await_expr(self, e: AwaitExpr, allow_none_return: bool = False) -> Typ
expected_type = self.type_context[-1]
if expected_type is not None:
expected_type = self.chk.named_generic_type("typing.Awaitable", [expected_type])
actual_type = get_proper_type(self.accept(e.expr, expected_type))

# For async function calls, don't propagate the expected type to avoid
# incorrect type inference affecting argument types
if isinstance(e.expr, CallExpr):
# Don't use expected type for function calls inside await to prevent
# incorrect constraint inference affecting argument types
actual_type = get_proper_type(self.accept(e.expr, None))
else:
actual_type = get_proper_type(self.accept(e.expr, expected_type))

if isinstance(actual_type, AnyType):
return AnyType(TypeOfAny.from_another_any, source_any=actual_type)
ret = self.check_awaitable_expr(
Expand Down
26 changes: 25 additions & 1 deletion test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def f(x: T) -> T:
return x
[typing fixtures/typing-async.pyi]
[out]
main:5: error: Argument 1 to "f" has incompatible type "T"; expected "int"
main:5: error: Incompatible types in assignment (expression has type "T", variable has type "int")
main:6: note: Revealed type is "builtins.int"

[case testAwaitGeneratorError]
Expand Down Expand Up @@ -1080,3 +1080,27 @@ class Launcher(P):

[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]

[case testAwaitCallExprTypeInference]
async def func(x: int) -> str:
return str(x)

async def test_await() -> str:
# This should not cause incorrect type inference for the argument
result = await func(42)
return result

# Test that the expected type doesn't propagate incorrectly to call arguments
async def test_nested_await() -> str:
def inner() -> int:
return 123

# The await should not affect type inference of inner function call
value = inner()
result = await func(value)
return result

[builtins fixtures/async_await.pyi]
[typing fixtures/typing-async.pyi]
[out]
main:2: error: Too many arguments for "str"
Loading