Skip to content

Conversation

@JelleZijlstra
Copy link
Member

@JelleZijlstra JelleZijlstra commented Oct 20, 2025

This restores the 3.13 behavior where Union[A, B] | ... was almost always allowed, rejecting only things that typing._type_check rejects.

It also necessarily means that something like int | str | "float" now works. And if a third-party type implements __ror__ on union objects and does something other than create a union, it may no longer work.

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

I havent heavily been involved in unionobject.c or genericaliasobject.c in about 4 years. But from what I recall, LGTM.

@efimov-mikhail
Copy link
Member

efimov-mikhail commented Oct 21, 2025

CI is red, this PR breaks some tests.
I've prepared slighly changed variant of this:
https://github.com/efimov-mikhail/cpython/tree/issue-140383-union-nb-or

@JelleZijlstra
Copy link
Member Author

Oops, pushed a new version that does not impact other types.

@efimov-mikhail
Copy link
Member

Is it okay that now we have two variants of __or__ method? For example, will GenericAlias and str objects work together as expected?

@JelleZijlstra
Copy link
Member Author

They don't, and maybe they should, but the behavior is the same in 3.13 and 3.14, so it's not part of the current issue. This PR focuses only on fixing the regression in 3.14.

$ uvx python3.13 -c 'list[int] | "str"'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    list[int] | "str"
    ~~~~~~~~~~^~~~~~~
TypeError: unsupported operand type(s) for |: 'types.GenericAlias' and 'str'
$ uvx python3.14 -c 'list[int] | "str"'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    list[int] | "str"
    ~~~~~~~~~~^~~~~~~
TypeError: unsupported operand type(s) for |: 'types.GenericAlias' and 'str'

@JelleZijlstra JelleZijlstra added the needs backport to 3.14 bugs and security fixes label Nov 3, 2025
@JelleZijlstra JelleZijlstra merged commit 7a9437d into python:main Nov 3, 2025
47 checks passed
@miss-islington-app
Copy link

Thanks @JelleZijlstra for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 3, 2025
@bedevere-app
Copy link

bedevere-app bot commented Nov 3, 2025

GH-140948 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Nov 3, 2025
JelleZijlstra added a commit that referenced this pull request Nov 3, 2025
…) (#140948)

gh-140348: Fix using | on unusual objects plus Unions (GH-140383)
(cherry picked from commit 7a9437d)

Co-authored-by: Jelle Zijlstra <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants