GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge `typing.Union` and `types.UnionType` · Issue #105499 · python/cpython

Merge typing.Union and types.UnionType #105499

New issue
Copy link
New issue
Copy link
Closed
Closed
Merge typing.Union and types.UnionType#105499
Copy link
Assignees
JelleZijlstra
Labels
3.13only security fixesonly security fixestopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Description

@JelleZijlstra
JelleZijlstra
opened on Jun 8, 2023
Issue body actions

Currently, unions created through typing.Union[A, B] and through the PEP-604 syntax A | B are at runtime instances of completely different types, and they differ in exactly what elements they accept. This is confusing and makes it harder for users to detect unions at runtime.

I propose to proceed in two steps:

  1. Make typing.Union an alias for types.UnionType and make it so types.UnionType[A, B] works, accepting the same types Union accepts now.
  2. Loosen the rules for what the | operator accepts to accept more types that are commonly used in unions.

Linked PRs

  • gh-105499: Merge typing.Union and types.UnionType #105511
  • gh-105499: Defer "import warnings" in typing #132061
  • gh-105499: Avoid using functools.reduce() to reconstruct Union objects #155280
Reacted by Alex Waygood, James Hilton-Balfe, Danipulok, Carl Meyer, Taylor, T-256, George Sakkis, Qi Zhang, Feuermurmel, Brian Pugh and 16 more

Activity

  1. JelleZijlstra
    added
    type-featureA feature request or enhancementA feature request or enhancement
    topic-typing
    3.13only security fixesonly security fixes
    on Jun 8, 2023
  2. JelleZijlstra
    self-assigned this
    on Jun 8, 2023
  3. JelleZijlstra commented on Jun 8, 2023

    @JelleZijlstra
    JelleZijlstra
    on Jun 8, 2023
    MemberAuthor
    More actions

    I put up a first implementation at #105511. Some thoughts on the details:

    • I implemented it by making typing.Union just a re-export of types.UnionType, but I feel it might actually be more intuitive if typing.Union was the canonical name of the object, and types.UnionType was an alias. We could change the implementation to set the module and name differently.
    • The repr() of all unions changes to use the | syntax.
    • I added dummy __name__, __qualname__, and __origin__ fields to types.UnionType to satisfy some test_typing tests.
    • We no longer support writing to a union's __args__ attribute. A test relied on this, but nobody should have been assigning to __args__ anyway, so I'm fine with this change.
    • There's a couple of behavior changes around issubclass() and subclassing because types.UnionType is (unlike typing.Union) an actual type.
  4. tibbe commented on Jun 14, 2023

    @tibbe
    tibbe
    on Jun 14, 2023
    More actions

    This broken an AST walker I had written for Python 3.9, after I upgraded all my Union[X, Y] to X | Y, something is expected to be purely syntactical change.

    Reacted by Simon-Martin Schröder
  5. samsonvd commented on Sep 28, 2024

    @samsonvd
    samsonvd
    on Sep 28, 2024
    More actions

    Will this also fix the issue of unsupported operand type(s) for |: 'str' and ... when using a string reference of a type?

    i.e.

    class Foo:
        def get_self(self) -> "Foo" | None:
            pass
    
    # Traceback (most recent call last):
    #   File "<stdin>", line 1, in <module>
    #   File "<stdin>", line 2, in Foo
    # TypeError: unsupported operand type(s) for |: 'str' and 'NoneType'
    
    from typing import Union
    class Foo:
        def get_self(self) -> Union["Foo", None]:
            pass
    
    # no issues
  6. JelleZijlstra commented on Sep 28, 2024

    @JelleZijlstra
    JelleZijlstra
    on Sep 28, 2024
    MemberAuthor
    More actions

    @marscapone, no, but other changes in Python 3.14 (PEP-649) will fix that case.

  7. hauntsaninja commented on Sep 28, 2024

    @hauntsaninja
    hauntsaninja
    on Sep 28, 2024
    Contributor
    More actions

    See also https://mypy.readthedocs.io/en/stable/runtime_troubles.html and from __future__ import annotations, for mostly solutions in Python 3.7 onwards

  8. JelleZijlstra
    added a commit that references this issue on Mar 4, 2025

    gh-105499: Merge typing.Union and types.UnionType (#105511)

    dc6d66f
  9. JelleZijlstra
    closed this as completedon Mar 7, 2025
  10. kepler
    added 3 commits that reference this issue on Mar 12, 2025

    Fix multiple issues with converting type hints to JSON Schema

    6932ba0

    Fix multiple issues with converting type hints to JSON Schema

    2b06f8e
  11. dirkbrnd
    added a commit that references this issue on Mar 19, 2025

    Fix multiple issues with converting type hints to JSON Schema (#2379)

    696ad5e
  12. layday
    added 2 commits that reference this issue on Mar 29, 2025

    Drop deprecated `_UnionGenericAlias` on Python 3.14 and up

    76f8739

    Drop deprecated `_UnionGenericAlias` on Python 3.14 and up

    60fefab
  13. JelleZijlstra
    added 3 commits that reference this issue on Mar 31, 2025

    gh-105499: typing: Remove an unused function (#131946)

    0f511d8

    pythongh-105499: Defer "import warnings" in typing

    8681c97

    gh-105499: Defer "import warnings" in typing (#132061)

    d1db43c
  14. randolf-scholz
    added 3 commits that reference this issue on Jul 21, 2025

    changed test_legacy_union to test_typing_union. Changed behavior due to

    914fadc

    changed test_legacy_union to test_typing_union. Changed behavior due to

    eeef8cd

    changed test_legacy_union to test_typing_union. Changed behavior due to

    4829f23
  15. serhiy-storchaka commented on Jul 24, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on Jul 24, 2025
    Member
    More actions

    I think this was a mistake. types.UnionType was intentionally named so to avoid confusion with typing.Union (see #88895
    ). types.UnionType should not be subscriptable because it is not generic type (and if it was a generic type, subscription would have different semantic than for typing.Union). types.UnionType corresponds to typing._UnionGenericAlias, not typing.Union.

  16. serhiy-storchaka
    added 2 commits that reference this issue on Jul 24, 2025

    pythongh-137065: Unmerge types.UnionType and typing.Union

    e57751f

    pythongh-137065: Unmerge types.UnionType and typing.Union

    fb8bc3e
  17. serhiy-storchaka
    added 2 commits that reference this issue on Sep 16, 2025

    pythongh-137065: Revert "pythongh-105499: Merge typing.Union and type…

    efdd228

    pythongh-137065: Revert "pythongh-105499: Merge typing.Union and type…

    5cd4a72
  18. JelleZijlstra
    added a commit that references this issue on Aug 7, 2026

    gh-105499: Avoid using `functools.reduce()` to reconstruct `Union` ob…

    cfcbfe4
  19. mbeijen
    added a commit that references this issue on Aug 14, 2026

    pythongh-105499: Avoid using `functools.reduce()` to reconstruct `Uni…

    2692f36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @JelleZijlstra
    JelleZijlstra

Labels

3.13only security fixesonly security fixestopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.