Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix updating of annotations for explicitly named transforms. #30380

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions sdks/python/apache_beam/transforms/ptransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,9 @@ def __ror__(self, pvalueish, _unused=None):
def expand(self, pvalue):
raise RuntimeError("Should never be expanded directly.")

def annotations(self):
return self.transform.annotations()

def __getattr__(self, attr):
transform_attr = getattr(self.transform, attr)
if callable(transform_attr):
Expand All @@ -1128,6 +1131,12 @@ def wrapper(*args, **kwargs):
else:
return transform_attr

def __setattr__(self, attr, value):
if attr == 'annotations':
self.transform.annotations = value
else:
super().__setattr__(attr, value)


# Defined here to avoid circular import issues for Beam library transforms.
def annotate_yaml(constructor):
Expand Down
11 changes: 11 additions & 0 deletions sdks/python/apache_beam/transforms/ptransform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ def test_str(self):
"""inputs=('ci',) side_inputs=('cs',)>""",
str(inputs_tr))

def test_named_annotations(self):
t = beam.Impulse()
t.annotations = lambda: {'test': 'value'}
named_t = 'Name' >> t
self.assertEqual(named_t.annotations(), {'test': 'value'})
original_annotations = named_t.annotations()
named_t.annotations = lambda: {'another': 'value', **original_annotations}
# Verify this is reflected on the original transform,
# which is what gets used in apply.
self.assertEqual(t.annotations(), {'test': 'value', 'another': 'value'})

def test_do_with_do_fn(self):
class AddNDoFn(beam.DoFn):
def process(self, element, addon):
Expand Down
Loading