GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix Union type merging to use types.UnionType instead of typing.Union by tangyuan0821 · Pull Request #137068 · python/cpython

Conversation

@tangyuan0821

@tangyuan0821 tangyuan0821 commented Jul 24, 2025

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tangyuan0821 and others added 2 commits July 24, 2025 15:55
@tangyuan0821
Refactor the typing module to remove C extension support for Union.
78e8785
@tangyuan0821
Merge branch 'python:main' into main
770aa75
@tangyuan0821
tangyuan0821 requested review from AlexWaygood and JelleZijlstra as code owners July 24, 2025 08:05
@bedevere-app bedevere-app Bot added the awaiting review label Jul 24, 2025
@python-cla-bot

python-cla-bot Bot commented Jul 24, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Jul 24, 2025

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland
StanFromIreland requested a review from serhiy-storchaka July 24, 2025 08:43
@StanFromIreland

StanFromIreland commented Jul 24, 2025

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tangyuan0821

tangyuan0821 commented Jul 24, 2025

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tangyuan0821 tangyuan0821 closed this Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JelleZijlstra JelleZijlstra Awaiting requested review from JelleZijlstra JelleZijlstra is a code owner
@AlexWaygood AlexWaygood Awaiting requested review from AlexWaygood AlexWaygood is a code owner
@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Merge 'types.UnionType' with typing._UnionGenericAlias, not typing.Union

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@tangyuan0821 @StanFromIreland
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.