Skip to content

Commit 7fa547b

Browse files
nickdrozdPCManticore
authored andcommitted
Cut generator flags
This simplifies the code, and may slightly improve performance.
1 parent 04856ec commit 7fa547b

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

astroid/decorators.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,31 @@ def wrapped(node, context=None, _func=func, **kwargs):
111111

112112
@wrapt.decorator
113113
def yes_if_nothing_inferred(func, instance, args, kwargs):
114-
inferred = False
115-
for node in func(*args, **kwargs):
116-
inferred = True
117-
yield node
118-
if not inferred:
114+
generator = func(*args, **kwargs)
115+
116+
try:
117+
yield next(generator)
118+
except StopIteration:
119+
# generator is empty
119120
yield util.Uninferable
121+
return
122+
123+
yield from generator
120124

121125

122126
@wrapt.decorator
123127
def raise_if_nothing_inferred(func, instance, args, kwargs):
124-
inferred = False
128+
generator = func(*args, **kwargs)
129+
125130
try:
126-
generator = func(*args, **kwargs)
127-
while True:
128-
yield next(generator)
129-
inferred = True
131+
yield next(generator)
130132
except StopIteration as error:
131-
if not inferred:
132-
if error.args:
133-
# pylint: disable=not-a-mapping
134-
raise exceptions.InferenceError(**error.args[0])
135-
raise exceptions.InferenceError(
136-
"StopIteration raised without any error information."
137-
)
133+
# generator is empty
134+
if error.args:
135+
# pylint: disable=not-a-mapping
136+
raise exceptions.InferenceError(**error.args[0])
137+
raise exceptions.InferenceError(
138+
"StopIteration raised without any error information."
139+
)
140+
141+
yield from generator

0 commit comments

Comments
 (0)