Skip to content
Closed
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
13 changes: 12 additions & 1 deletion sdks/python/apache_beam/transforms/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1423,15 +1423,21 @@ def _check_fn_use_yield_and_return(fn):
source_code = _get_function_body_without_inners(fn)
has_yield = False
has_return = False
if len(source_code)==0:
return None
Comment on lines +1426 to +1427
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of this check?

for line in source_code.split("\n"):
if line.lstrip().startswith("yield ") or line.lstrip().startswith(
"yield("):
has_yield = True
if line.lstrip().startswith("return ") or line.lstrip().startswith(
"return("):
has_return = True
if line.lstrip().startswith("return None"):
Copy link
Contributor

Choose a reason for hiding this comment

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

The warn could also be placed here instead, eliminating the need for the elif block.

return None
if has_yield and has_return:
return True
if not has_return and not has_yield:
return None
return False
except Exception as e:
_LOGGER.debug(str(e))
Expand Down Expand Up @@ -1484,8 +1490,13 @@ def __init__(self, fn, *args, **kwargs):
'Using yield and return in the process method '
'of %s can lead to unexpected behavior, see:'
'https://github.com/apache/beam/issues/22969.',
self.fn.__class__)
self.fn.__class__)

elif _check_fn_use_yield_and_return(self.fn.process) is None:
_LOGGER.warning(
'no iterator is returned by the process method in %s',
self.fn.__class__)

Comment on lines +1495 to +1499
Copy link
Contributor

Choose a reason for hiding this comment

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

Mechanically you probably don't want to re-run _check_fn_use_yield_and_return() here. Consider capturing the output value the first time and using that here instead

# Validate the DoFn by creating a DoFnSignature
from apache_beam.runners.common import DoFnSignature
self._signature = DoFnSignature(self.fn)
Expand Down