Skip to content

Comments

Fix type checking failure with Python 3.10+ union pipe syntax (int | None)#37645

Merged
jrmccluskey merged 3 commits intoapache:masterfrom
junaiddshaukat:fix-36592-union-type-pipe-operator
Feb 19, 2026
Merged

Fix type checking failure with Python 3.10+ union pipe syntax (int | None)#37645
jrmccluskey merged 3 commits intoapache:masterfrom
junaiddshaukat:fix-36592-union-type-pipe-operator

Conversation

@junaiddshaukat
Copy link
Contributor

Summary

Fixes #36592

Using Python 3.10+ pipe operator union types (e.g. int | None) in function type hints causes TypeError: issubclass() arg 1 must be a class when Beam performs type checking.

# This was crashing:
def func(x: int, y: int | None = None):
    return x

with beam.Pipeline() as p:
    (p | beam.Create([1]) | beam.Map(func))
# TypeError: issubclass() arg 1 must be a class

Root Cause

The normalize() function in typehints.py did not handle types.UnionType (Python 3.10+ pipe union syntax). On Python 3.10–3.13, int | None creates a types.UnionType whose module is not 'typing' and has no origin, so it passed through normalize() unchanged. This caused is_consistent_with() to fall through to issubclass() with a non-class argument.

Fix

Added a types.UnionType check in normalize() that routes pipe union types through convert_to_beam_type(), which already handles the conversion to Beam's UnionConstraint.

Tests Added

test_hint_helper_pipe_union — verifies is_consistent_with() works with pipe union types
test_normalize_pipe_union — verifies normalize() converts types.UnionType to UnionConstraint


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), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

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

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

…None)

The normalize() function did not handle types.UnionType, causing
is_consistent_with() to fall through to issubclass() with a non-class
argument. Route types.UnionType through convert_to_beam_type() which
already knows how to convert it to Beam's UnionConstraint.

Fixes apache#36592
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @junaiddshaukat, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a TypeError in Apache Beam's type checking that occurred when using Python 3.10+ union pipe syntax (e.g., int | None). The fix involves updating the normalize function to correctly process these new union types, ensuring compatibility and preventing runtime errors. New tests have been added to validate the correct handling of these type hints.

Highlights

  • Problem Addressed: Resolved a TypeError occurring in Apache Beam's type checking when Python 3.10+ union pipe syntax (e.g., int | None) was used in function type hints.
  • Root Cause Identified: The normalize() function in typehints.py did not recognize types.UnionType, leading to incorrect handling and subsequent TypeError when issubclass() was called with a non-class argument.
  • Solution Implemented: Modified the normalize() function to include a check for types.UnionType and route these types through native_type_compatibility.convert_to_beam_type(), which correctly converts them to Beam's UnionConstraint.
  • New Tests Added: Introduced test_hint_helper_pipe_union to verify is_consistent_with() works with pipe union types, and test_normalize_pipe_union to confirm normalize() correctly converts types.UnionType to UnionConstraint.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • sdks/python/apache_beam/typehints/typehints.py
    • Added a new elif condition in the normalize function to detect types.UnionType instances.
    • Integrated native_type_compatibility.convert_to_beam_type to handle the conversion of types.UnionType to Beam's internal type representation.
    • Included a fallback to Any if convert_to_beam_type returns the original type.
  • sdks/python/apache_beam/typehints/typehints_test.py
    • Introduced test_hint_helper_pipe_union to assert that is_consistent_with correctly evaluates Python 3.10+ pipe union types against various other types.
    • Added test_normalize_pipe_union to verify that the normalize function successfully transforms types.UnionType objects into typehints.UnionConstraint instances.
Activity
  • No specific activity (comments, reviews, etc.) was provided in the context beyond the initial pull request description.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@junaiddshaukat
Copy link
Contributor Author

assign set of reviewers

@github-actions
Copy link
Contributor

Assigning reviewers:

R: @damccorm for label python.

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).

@damccorm
Copy link
Contributor

@jrmccluskey could you please take a look?

self.assertFalse(is_consistent_with(Union[str, int], str))
self.assertFalse(is_consistent_with(str, NonBuiltInGeneric[str]))

def test_hint_helper_pipe_union(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Having the extra test coverage is nice but it's worth noting that this suite passes even without the changes to typehints.py

Comment on lines 1465 to 1470
elif isinstance(x, types.UnionType):
beam_type = native_type_compatibility.convert_to_beam_type(x)
if beam_type != x:
return beam_type
else:
return Any
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding the extra conditional here and repeating the same code as the other categories that get converted is clunky. Now that the list of checks is reasonably long, it may be worth splitting them out into a helper function for readability; otherwise, isinstance(x, types.UnionType) should be worked into the existing elif statement.

@junaiddshaukat
Copy link
Contributor Author

@jrmccluskey Thanks for the review! I've addressed all your feedback:

  1. Merged isinstance(x, types.UnionType) into the existing elif — no more duplicated code block, it now extends the existing condition.
  2. Removed the GH issue link comments from both test methods.

Formatting verified clean with yapf==0.43.0. Ready for another look!

Copy link
Contributor

@jrmccluskey jrmccluskey left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! The test failures are unrelated.

@jrmccluskey jrmccluskey merged commit b9d48fa into apache:master Feb 19, 2026
103 of 111 checks passed
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.

[Bug]: Type Checking Fails with Python Union Types with |

3 participants