---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
---
UP028 [*] Replace `yield` over `for` loop with `yield from`
 --> UP028_0.py:2:5
  |
1 |   def f():
2 | /     for x in y:
3 | |         yield x
  | |_______________^
  |
help: Replace with `yield from`
  |
1 | def f():
  -     for x in y:
  -         yield x
2 +     yield from y
3 |
  |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
 --> UP028_0.py:7:5
  |
6 |   def g():
7 | /     for x, y in z:
8 | |         yield (x, y)
  | |____________________^
  |
help: Replace with `yield from`
  |
6 | def g():
  -     for x, y in z:
  -         yield (x, y)
7 +     yield from z
8 |
  |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:12:5
   |
11 |   def h():
12 | /     for x in [1, 2, 3]:
13 | |         yield x
   | |_______________^
   |
help: Replace with `yield from`
   |
11 | def h():
   -     for x in [1, 2, 3]:
   -         yield x
12 +     yield from [1, 2, 3]
13 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:17:5
   |
16 |   def i():
17 | /     for x in {x for x in y}:
18 | |         yield x
   | |_______________^
   |
help: Replace with `yield from`
   |
16 | def i():
   -     for x in {x for x in y}:
   -         yield x
17 +     yield from {x for x in y}
18 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:22:5
   |
21 |   def j():
22 | /     for x in (1, 2, 3):
23 | |         yield x
   | |_______________^
   |
help: Replace with `yield from`
   |
21 | def j():
   -     for x in (1, 2, 3):
   -         yield x
22 +     yield from (1, 2, 3)
23 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:27:5
   |
26 |   def k():
27 | /     for x, y in {3: "x", 6: "y"}:
28 | |         yield x, y
   | |__________________^
   |
help: Replace with `yield from`
   |
26 | def k():
   -     for x, y in {3: "x", 6: "y"}:
   -         yield x, y
27 +     yield from {3: "x", 6: "y"}
28 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:33:5
   |
31 |   def f():  # Comment one\n'
32 |       # Comment two\n'
33 | /     for x, y in {  # Comment three\n'
34 | |         3: "x",  # Comment four\n'
35 | |         # Comment five\n'
36 | |         6: "y",  # Comment six\n'
37 | |     }:  # Comment seven\n'
38 | |         # Comment eight\n'
39 | |         yield x, y  # Comment nine\n'
   | |__________________^
40 |           # Comment ten',
   |
help: Replace with `yield from`
   |
32 |     # Comment two\n'
   -     for x, y in {  # Comment three\n'
33 +     yield from {  # Comment three\n'
34 |         3: "x",  # Comment four\n'
35 |         # Comment five\n'
36 |         6: "y",  # Comment six\n'
   -     }:  # Comment seven\n'
   -         # Comment eight\n'
   -         yield x, y  # Comment nine\n'
37 +     }  # Comment nine\n'
38 |         # Comment ten',
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:44:5
   |
43 |   def f():
44 | /     for x, y in [{3: (3, [44, "long ss"]), 6: "y"}]:
45 | |         yield x, y
   | |__________________^
   |
help: Replace with `yield from`
   |
43 | def f():
   -     for x, y in [{3: (3, [44, "long ss"]), 6: "y"}]:
   -         yield x, y
44 +     yield from [{3: (3, [44, "long ss"]), 6: "y"}]
45 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:49:5
   |
48 |   def f():
49 | /     for x, y in z():
50 | |         yield x, y
   | |__________________^
51 |
52 |   def f():
   |
help: Replace with `yield from`
   |
48 | def f():
   -     for x, y in z():
   -         yield x, y
49 +     yield from z()
50 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:55:9
   |
53 |       def func():
54 |           # This comment is preserved\n'
55 | /         for x, y in z():  # Comment one\n'
56 | |             # Comment two\n'
57 | |             yield x, y  # Comment three\n'
   | |______________________^
58 |               # Comment four\n'
59 |   # Comment\n'
   |
help: Replace with `yield from`
   |
54 |         # This comment is preserved\n'
   -         for x, y in z():  # Comment one\n'
   -             # Comment two\n'
   -             yield x, y  # Comment three\n'
55 +         yield from z()  # Comment three\n'
56 |             # Comment four\n'
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:67:5
   |
65 |       for x in y:
66 |           yield x
67 | /     for z in x:
68 | |         yield z
   | |_______________^
   |
help: Replace with `yield from`
   |
66 |         yield x
   -     for z in x:
   -         yield z
67 +     yield from x
68 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:72:5
   |
71 |   def f():
72 | /     for x, y in z():
73 | |         yield x, y
   | |__________________^
74 |       x = 1
   |
help: Replace with `yield from`
   |
71 | def f():
   -     for x, y in z():
   -         yield x, y
72 +     yield from z()
73 |     x = 1
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
  --> UP028_0.py:79:5
   |
77 |   # Regression test for: https://github.com/astral-sh/ruff/issues/7103
78 |   def _serve_method(fn):
79 | /     for h in (
80 | |         TaggedText.from_file(args.input)
81 | |             .markup(highlight=args.region)
82 | |     ):
83 | |         yield h
   | |_______________^
   |
help: Replace with `yield from`
   |
78 | def _serve_method(fn):
   -     for h in (
79 +     yield from (
80 |         TaggedText.from_file(args.input)
81 |             .markup(highlight=args.region)
   -     ):
   -         yield h
82 +     )
83 |
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
   --> UP028_0.py:97:5
    |
 95 |   # UP028: The exception binding is not a reference to the loop variable
 96 |   def f():
 97 | /     for x in (1, 2, 3):
 98 | |         yield x
    | |_______________^
 99 |       # Shadowing with an `except`
100 |       try:
    |
help: Replace with `yield from`
   |
96 | def f():
   -     for x in (1, 2, 3):
   -         yield x
97 +     yield from (1, 2, 3)
98 |     # Shadowing with an `except`
   |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
   --> UP028_0.py:108:5
    |
106 |   # UP028: The context binding is not a reference to the loop variable
107 |   def f():
108 | /     for x in (1, 2, 3):
109 | |         yield x
    | |_______________^
110 |       # Shadowing with `with`
111 |       with contextlib.nullcontext() as x:
    |
help: Replace with `yield from`
    |
107 | def f():
    -     for x in (1, 2, 3):
    -         yield x
108 +     yield from (1, 2, 3)
109 |     # Shadowing with `with`
    |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
   --> UP028_0.py:118:5
    |
116 |   # UP028: The type annotation binding is not a reference to the loop variable
117 |   def f():
118 | /     for x in (1, 2, 3):
119 | |         yield x
    | |_______________^
120 |       # Shadowing with a type annotation
121 |       x: int
    |
help: Replace with `yield from`
    |
117 | def f():
    -     for x in (1, 2, 3):
    -         yield x
118 +     yield from (1, 2, 3)
119 |     # Shadowing with a type annotation
    |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
   --> UP028_0.py:134:5
    |
132 |   # UP028: The exception bindings are not a reference to the loop variable
133 |   def f():
134 | /     for x in (1, 2, 3):
135 | |         yield x
    | |_______________^
136 |       # Shadowing with multiple `except` blocks
137 |       try:
    |
help: Replace with `yield from`
    |
133 | def f():
    -     for x in (1, 2, 3):
    -         yield x
134 +     yield from (1, 2, 3)
135 |     # Shadowing with multiple `except` blocks
    |
note: This is an unsafe fix and may change runtime behavior

UP028 [*] Replace `yield` over `for` loop with `yield from`
   --> UP028_0.py:170:5
    |
168 |   # https://github.com/astral-sh/ruff/issues/15540
169 |   def f():
170 | /     for a in 1,:
171 | |         yield a
    | |_______________^
    |
help: Replace with `yield from`
    |
169 | def f():
    -     for a in 1,:
    -         yield a
170 +     yield from (1,)
171 |
    |
note: This is an unsafe fix and may change runtime behavior

UP028 Replace `yield` over `for` loop with `yield from`
   --> UP028_0.py:179:5
    |
177 |       global SOME_GLOBAL
178 |
179 | /     for SOME_GLOBAL in iterable:
180 | |         yield SOME_GLOBAL
    | |_________________________^
181 |
182 |       some_non_local = None
    |
help: Replace with `yield from`

UP028 Replace `yield` over `for` loop with `yield from`
   --> UP028_0.py:187:9
    |
185 |           nonlocal some_non_local
186 |
187 | /         for some_non_local in iterable:
188 | |             yield some_non_local
    | |________________________________^
    |
help: Replace with `yield from`
