Skip to content

[Python] Accept int tags on Partition results applied to a list - #40681

Open
raams95 wants to merge 1 commit into
apache:masterfrom
raams95:fix-materialized-partition-int-tag
Open

raams95 wants to merge 1 commit into
apache:masterfrom
raams95:fix-materialized-partition-int-tag

Conversation

@raams95

@raams95 raams95 commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #19154

When beam.Partition is applied to a list instead of a PCollection, the result can't be indexed with an int:

>>> partitions = [0, 1, 2, 3, 4] | beam.Partition(lambda e, n: e % n, 2)
>>> partitions[0]
KeyError: "Tag 0 is not a defined output tag of <DoOutputsTuple main_tag=None tags=('0', '1') transform=<ParDo(PTransform) label=[ParDo(ApplyPartitionFnFn)]>>."

Inside a pipeline partitions[0] works, because DoOutputsTuple.__getitem__ converts int tags to strings so that partitions can be looked up with the same ints the partition function returns. The result of applying a transform to a list is a _MaterializedDoOutputsTuple, whose __getitem__ overrides that method without the conversion, so only partitions['0'] works there.

This change does the same int to string conversion in _MaterializedDoOutputsTuple.__getitem__.

I added test_partition_applied_to_list to ptransform_test.py. It fails with the KeyError above before the change and passes after it.

Testing:

  • pytest apache_beam/transforms/ptransform_test.py apache_beam/pvalue_test.py apache_beam/pipeline_test.py
  • yapf --diff and ruff check on the changed files

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123 or fixes #456).
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement. (Not needed, small change.)

See the Contributor Guide for more tips on how to make review process smoother.

Indexing the result of a Partition applied to a list with an int, such
as partitions[0], raised KeyError, because
_MaterializedDoOutputsTuple.__getitem__ did not convert int tags to
strings the way DoOutputsTuple.__getitem__ does for pipelines.

Fixes apache#19154

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

This PR resolves #19154 by converting integer tags to strings in _MaterializedDoOutputsTuple.__getitem__, matching the existing behavior of DoOutputsTuple.__getitem__. The implementation is clean, minimal, and properly covered by a unit test.

🔍 General Feedback

  • The change accurately aligns eager materialization output indexing with deferred pipeline DoOutputsTuple indexing.
  • The unit test in ptransform_test.py and the CHANGES.md entry are well-structured and complete.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.01%. Comparing base (76f6e6d) to head (ee413ab).
⚠️ Report is 242 commits behind head on master.

Additional details and impacted files
@@              Coverage Diff              @@
##             master   #40681       +/-   ##
=============================================
+ Coverage     56.16%   80.01%   +23.84%     
=============================================
  Files          1124      490      -634     
  Lines        178336    82813    -95523     
  Branches       1489        0     -1489     
=============================================
- Hits         100170    66261    -33909     
+ Misses        75645    16552    -59093     
+ Partials       2521        0     -2521     
Flag Coverage Δ
go ?
java ?
python 80.01% <100.00%> (+0.45%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @claudevdm for label python.
Backup reviewer: jrmccluskey (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_MaterializedDoOutputsTuple doesn't support __getitem__ by integer values

1 participant