Skip to content

Fix Union type merging to use types.UnionType instead of typing.Union #137068

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

Closed
wants to merge 2 commits into from

Conversation

tangyuan0821
Copy link

Fixes #137065

Summary
This PR fixes the Union type merging issue where types.UnionType was incorrectly merged with typing.Union instead of typing._UnionGenericAlias. Now typing.Union[int, str] properly returns a types.UnionType object.

What Changed
C Module (_typingmodule.c)
Removed Union export from the C module
Union is now handled entirely in Python
Python Module (typing.py)
Changed Union to a @_SpecialForm decorator
Added _UnionGenericAlias class that creates types.UnionType objects
Added fallback handling for edge cases
Fixed some forward reference issues in IO classes
How It Works
When you call Union[int, str]:

The @_SpecialForm function processes the arguments
Creates a _UnionGenericAlias that uses the | operator
Returns a types.UnionType object
Falls back gracefully if something goes wrong
Results
Testing
All existing doctests still pass (43/43)
typing module imports fine
No syntax errors
Backward compatibility maintained
Why This Matters
Before this fix, typing.Union and types.UnionType were inconsistent. Now they work together properly, which helps type checkers and runtime tools give better results.

The change is safe - existing code keeps working the same way, but now with the correct underlying types.

@python-cla-bot
Copy link

python-cla-bot bot commented Jul 24, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jul 24, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland
Copy link
Member

Thank you for your initiative, however, Serhiy has created #137069.

@tangyuan0821
Copy link
Author

Thank you for your initiative, however, Serhiy has created #137069.
Thank you!

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.

Merge 'types.UnionType' with typing._UnionGenericAlias, not typing.Union
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy