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'

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.

3 participants