@@ -2089,8 +2089,8 @@ turtle
2089
2089
types
2090
2090
-----
2091
2091
2092
- * :class: ` types.UnionType ` is now an alias for :class: ` typing.Union `.
2093
- See :ref: ` below < whatsnew314-typing-union > ` for more details .
2092
+ * Add read-only attributes :attr: ` !__name__ `, :attr: ` !__qualname__ ` and
2093
+ :attr: ` !__origin__ ` for :class: ` types.UnionType ` instances .
2094
2094
(Contributed by Jelle Zijlstra in :gh: `105499 `.)
2095
2095
2096
2096
@@ -2099,38 +2099,22 @@ typing
2099
2099
2100
2100
.. _whatsnew314-typing-union :
2101
2101
2102
- * :class: ` types.UnionType ` and :class: ` typing.Union ` are now aliases for each other,
2103
- meaning that both old-style unions (created with `` Union[ int, str] ``) and new-style
2104
- unions (`` int | str ``) now create instances of the same runtime type . This unifies
2102
+ * Both old-style unions (created with `` Union[int, str] ``) and new-style
2103
+ unions (`` int | str ``) now create instances of the same runtime type,
2104
+ :class: ` types.UnionType ` . This unifies
2105
2105
the behavior between the two syntaxes, but leads to some differences in behavior that
2106
2106
may affect users who introspect types at runtime:
2107
2107
2108
2108
- Both syntaxes for creating a union now produce the same string representation in
2109
2109
``repr() ``. For example, ``repr(Union[int, str]) ``
2110
2110
is now ``"int | str" `` instead of ``"typing.Union[int, str]" ``.
2111
- - Unions created using the old syntax are no longer cached. Previously, running
2112
- ``Union[int, str] `` multiple times would return the same object
2113
- (``Union[int, str] is Union[int, str] `` would be ``True ``), but now it will
2114
- return two different objects. Users should use ``== `` to compare unions for equality, not
2115
- ``is ``. New-style unions have never been cached this way.
2116
- This change could increase memory usage for some programs that use a large number of
2117
- unions created by subscripting ``typing.Union ``. However, several factors offset this cost:
2118
- unions used in annotations are no longer evaluated by default in Python 3.14
2119
- because of :pep: `649 `; an instance of :class: `types.UnionType ` is
2120
- itself much smaller than the object returned by ``Union[] `` was on prior Python
2121
- versions; and removing the cache also saves some space. It is therefore
2122
- unlikely that this change will cause a significant increase in memory usage for most
2123
- users.
2124
2111
- Previously, old-style unions were implemented using the private class
2125
2112
``typing._UnionGenericAlias ``. This class is no longer needed for the implementation,
2126
2113
but it has been retained for backward compatibility, with removal scheduled for Python
2127
2114
3.17. Users should use documented introspection helpers like :func: `typing.get_origin `
2128
2115
and :func: `typing.get_args ` instead of relying on private implementation details.
2129
- - It is now possible to use :class: `typing.Union ` itself in :func: `isinstance ` checks.
2130
- For example, ``isinstance(int | str, typing.Union) `` will return ``True ``; previously
2131
- this raised :exc: `TypeError `.
2132
- - The ``__args__ `` attribute of :class: `typing.Union ` objects is no longer writable.
2133
- - It is no longer possible to set any attributes on :class: `typing.Union ` objects.
2116
+ - The ``__args__ `` attribute of :data: `typing.Union ` objects is no longer writable.
2117
+ - It is no longer possible to set any attributes on the :data: `typing.Union ` objects.
2134
2118
This only ever worked for dunder attributes on previous versions, was never
2135
2119
documented to work, and was subtly broken in many cases.
2136
2120
@@ -2782,8 +2766,8 @@ Changes in the Python API
2782
2766
This temporary change affects other threads.
2783
2767
(Contributed by Serhiy Storchaka in :gh: `69998 `.)
2784
2768
2785
- * :class: ` types.UnionType ` is now an alias for :class: `typing.Union `,
2786
- causing changes in some behaviors.
2769
+ * Subscription of :data: ` typing.Union ` now returns a :class: `types.UnionType `
2770
+ instance, causing changes in some behaviors.
2787
2771
See :ref: `above <whatsnew314-typing-union >` for more details.
2788
2772
(Contributed by Jelle Zijlstra in :gh: `105499 `.)
2789
2773
0 commit comments