Skip to content

Commit 5e33f47

Browse files
author
Ilya Priven
committed
AlexWaygood feedback
1 parent 7871b8d commit 5e33f47

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

mypy/checker.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1064,20 +1064,6 @@ def enter_attribute_inference_context(self) -> Iterator[None]:
10641064
yield None
10651065
self.inferred_attribute_types = old_types
10661066

1067-
def _is_empty_generator(self, func: FuncItem) -> bool:
1068-
"""
1069-
Checks whether a function's body is 'return; yield' (the yield being added only
1070-
to promote the function into a generator).
1071-
"""
1072-
return (
1073-
len(body := func.body.body) == 2
1074-
and isinstance(ret_stmt := body[0], ReturnStmt)
1075-
and (ret_stmt.expr is None or is_literal_none(ret_stmt.expr))
1076-
and isinstance(expr_stmt := body[1], ExpressionStmt)
1077-
and isinstance(yield_expr := expr_stmt.expr, YieldExpr)
1078-
and (yield_expr.expr is None or is_literal_none(yield_expr.expr))
1079-
)
1080-
10811067
def check_func_def(
10821068
self, defn: FuncItem, typ: CallableType, name: str | None, allow_empty: bool = False
10831069
) -> None:
@@ -1255,7 +1241,10 @@ def check_func_def(
12551241
# have no good way of doing this.
12561242
#
12571243
# TODO: Find a way of working around this limitation
1258-
if len(expanded) >= 2 or self._is_empty_generator(item):
1244+
#
1245+
# We suppress reachability warnings for empty generators (return; yield), since there's
1246+
# no way to promote a function into a generator except by adding an "unreachable" yield.
1247+
if len(expanded) >= 2 or _is_empty_generator(item):
12591248
self.binder.suppress_unreachable_warnings()
12601249
self.accept(item.body)
12611250
unreachable = self.binder.is_unreachable()
@@ -6976,6 +6965,21 @@ def is_literal_not_implemented(n: Expression) -> bool:
69766965
return isinstance(n, NameExpr) and n.fullname == "builtins.NotImplemented"
69776966

69786967

6968+
def _is_empty_generator(func: FuncItem) -> bool:
6969+
"""
6970+
Checks whether a function's body is 'return; yield' (the yield being added only
6971+
to promote the function into a generator).
6972+
"""
6973+
return (
6974+
len(body := func.body.body) == 2
6975+
and isinstance(ret_stmt := body[0], ReturnStmt)
6976+
and (ret_stmt.expr is None or is_literal_none(ret_stmt.expr))
6977+
and isinstance(expr_stmt := body[1], ExpressionStmt)
6978+
and isinstance(yield_expr := expr_stmt.expr, YieldExpr)
6979+
and (yield_expr.expr is None or is_literal_none(yield_expr.expr))
6980+
)
6981+
6982+
69796983
def builtin_item_type(tp: Type) -> Type | None:
69806984
"""Get the item type of a builtin container.
69816985

0 commit comments

Comments
 (0)