Skip to content

Commit dd0fb51

Browse files
committed
gh-131933: Document UnionType/Union merger in What's New
1 parent 1e3ec33 commit dd0fb51

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Doc/whatsnew/3.14.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,42 @@ turtle
959959
(Contributed by Marie Roald and Yngve Mardal Moe in :gh:`126350`.)
960960

961961

962+
types
963+
-----
964+
965+
* :class:`types.UnionType` is now an alias for :class:`typing.Union`
966+
See :ref:`below <whatsnew314-typing-union>` for more details.
967+
(Contributed by Jelle Zijlstra in :gh:`105499`.)
968+
969+
typing
970+
------
971+
972+
.. _whatsnew314-typing-union:
973+
974+
* :class:`types.UnionType` and :class:`typing.Union` are now aliases for each other,
975+
meaning that both old-style unions (created with ``Union[int, str]``) and new-style
976+
unions (``int | str``) now create instances of the same runtime type. This unifies
977+
the behavior between the two syntaxes, but leads to some differences in behavior that
978+
may affect users who introspect types at runtime:
979+
980+
- Both syntaxes for creating a union now produce the same string representation in
981+
``repr()``. For example, ``repr(Union[int, str])``
982+
is now ``"int | str"`` instead of ``"typing.Union[int, str]"``.
983+
- Unions created using the old syntax are no longer cached. Previously, running
984+
``Union[int, str]`` multiple times would return the same object
985+
(``Union[int, str] is Union[int, str]`` would be ``True``), but now it will
986+
return two different objects. Users should use ``==`` to compare unions for equality, not
987+
``is``. New-style unions already worked this way in earlier versions.
988+
- Previously, old-style unions were implemented using the private class
989+
``typing._UnionGenericAlias``. This class is no longer needed for the implementation,
990+
but it has been retained for backward compatibility, with removal scheduled for Python
991+
3.17. Users should use documented introspection helpers like :func:`typing.get_origin`
992+
and :func:`typing.get_args` instead of relying on private implementation details.
993+
- It is now possible to use :class:`typing.Union` in :func:`isinstance` checks.
994+
- The ``__args__`` attribute of :class:`typing.Union` objects is no longer writable.
995+
996+
(Contributed by Jelle Zijlstra in :gh:`105499`.)
997+
962998
unicodedata
963999
-----------
9641000

0 commit comments

Comments
 (0)