Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SyntaxWarning when await an unawaitable constant.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires better phrasing. This does not explain anything.

28 changes: 28 additions & 0 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -3748,6 +3748,33 @@ check_index(compiler *c, expr_ty e, expr_ty s)
}
}

static int
check_awaitable(compiler* c, expr_ty e)
{
/* Emit a warning when awaiting obvious non-awaitable literal objects. */
switch (e->kind) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to swap the kinds and only consider kinds that are awaitable. Otherwise, we need to update that list whenever we add a new kind that is not awaitable.

case Constant_kind:
case Tuple_kind:
case List_kind:
case ListComp_kind:
case Dict_kind:
case DictComp_kind:
case Set_kind:
case SetComp_kind:
case GeneratorExp_kind:
case JoinedStr_kind:
case TemplateStr_kind:
case FormattedValue_kind:
case Interpolation_kind: {
location loc = LOC(e);
return _PyCompile_Warn(c, loc, "'%.200s' object can't be awaited",
infer_type(e)->tp_name);
}
default:
return SUCCESS;
}
}

static int
is_import_originated(compiler *c, expr_ty e)
{
Expand Down Expand Up @@ -5295,6 +5322,7 @@ codegen_visit_expr(compiler *c, expr_ty e)
ADD_YIELD_FROM(c, loc, 0);
break;
case Await_kind:
RETURN_IF_ERROR(check_awaitable(c, e->v.Await.value));
VISIT(c, expr, e->v.Await.value);
ADDOP_I(c, loc, GET_AWAITABLE, 0);
ADDOP_LOAD_CONST(c, loc, Py_None);
Expand Down
Loading