Skip to content

cannot use | union syntax in recursive types #100325

Closed as not planned
Closed as not planned
@ShayHill

Description

@ShayHill

Bug report

Python 11.1 accepts recurring type definition with the Union[] syntax

>>> from typing import Union
>>> NestedList = list[Union[str, "NestedList"]]
>>> NestedList
list[typing.Union[str, ForwardRef('NestedList')]]

However, the more recent | union syntax fails

>>> NestedList = list[str | "NestedList"]
TypeError: unsupported operand type(s) for |: 'type' and 'str'

Your environment

  • CPython versions tested on:
    Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:37:59) [MSC v.1933 32 bit (Intel)] on win32
    Python 3.11.0 (main, Oct 24 2022, 18:13:38) [MSC v.1933 32 bit (Intel)] on win32
    Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win3

  • Operating system and architecture:
    Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz 2.30 GHz

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions