Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,42 @@ turtle
(Contributed by Marie Roald and Yngve Mardal Moe in :gh:`126350`.)


types
-----

* :class:`types.UnionType` is now an alias for :class:`typing.Union`
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
See :ref:`below <whatsnew314-typing-union>` for more details.
(Contributed by Jelle Zijlstra in :gh:`105499`.)

typing
------
Comment thread
JelleZijlstra marked this conversation as resolved.

.. _whatsnew314-typing-union:

* :class:`types.UnionType` and :class:`typing.Union` are now aliases for each other,
meaning that both old-style unions (created with ``Union[int, str]``) and new-style
unions (``int | str``) now create instances of the same runtime type. This unifies
the behavior between the two syntaxes, but leads to some differences in behavior that
may affect users who introspect types at runtime:
Comment thread
picnixz marked this conversation as resolved.

- Both syntaxes for creating a union now produce the same string representation in
``repr()``. For example, ``repr(Union[int, str])``
is now ``"int | str"`` instead of ``"typing.Union[int, str]"``.
- Unions created using the old syntax are no longer cached. Previously, running
``Union[int, str]`` multiple times would return the same object
(``Union[int, str] is Union[int, str]`` would be ``True``), but now it will
return two different objects. Users should use ``==`` to compare unions for equality, not
``is``. New-style unions already worked this way in earlier versions.
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
- Previously, old-style unions were implemented using the private class
``typing._UnionGenericAlias``. This class is no longer needed for the implementation,
but it has been retained for backward compatibility, with removal scheduled for Python
3.17. Users should use documented introspection helpers like :func:`typing.get_origin`
and :func:`typing.get_args` instead of relying on private implementation details.
- It is now possible to use :class:`typing.Union` in :func:`isinstance` checks.
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
- The ``__args__`` attribute of :class:`typing.Union` objects is no longer writable.

(Contributed by Jelle Zijlstra in :gh:`105499`.)

unicodedata
Comment thread
JelleZijlstra marked this conversation as resolved.
-----------

Expand Down
Loading