Skip to content

Commit cbd51eb

Browse files
warning when await an unawaitable constant
1 parent f2b5c20 commit cbd51eb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Python/codegen.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,6 +3748,32 @@ check_index(compiler *c, expr_ty e, expr_ty s)
37483748
}
37493749
}
37503750

3751+
check_awaitable(compiler* c, expr_ty e)
3752+
{
3753+
/* Emit a warning when awaiting obvious non-awaitable literal objects. */
3754+
switch (e->kind) {
3755+
case Constant_kind:
3756+
case Tuple_kind:
3757+
case List_kind:
3758+
case ListComp_kind:
3759+
case Dict_kind:
3760+
case DictComp_kind:
3761+
case Set_kind:
3762+
case SetComp_kind:
3763+
case GeneratorExp_kind:
3764+
case JoinedStr_kind:
3765+
case TemplateStr_kind:
3766+
case FormattedValue_kind:
3767+
case Interpolation_kind: {
3768+
location loc = LOC(e);
3769+
return _PyCompile_Warn(c, loc, "'%.200s' object can't be awaited",
3770+
infer_type(e)->tp_name);
3771+
}
3772+
default:
3773+
return SUCCESS;
3774+
}
3775+
}
3776+
37513777
static int
37523778
is_import_originated(compiler *c, expr_ty e)
37533779
{
@@ -5295,6 +5321,7 @@ codegen_visit_expr(compiler *c, expr_ty e)
52955321
ADD_YIELD_FROM(c, loc, 0);
52965322
break;
52975323
case Await_kind:
5324+
RETURN_IF_ERROR(check_awaitable(c, e->v.Await.value));
52985325
VISIT(c, expr, e->v.Await.value);
52995326
ADDOP_I(c, loc, GET_AWAITABLE, 0);
53005327
ADDOP_LOAD_CONST(c, loc, Py_None);

0 commit comments

Comments
 (0)