Skip to content

Forward reference does not work when using union type operator in type alias #126121

@maxvfischer

Description

@maxvfischer

Bug report

Bug description:

When creating a type alias, it seems like forward referencing does not work as expected when using union type operator.

This works fine

from typing import Union
ABC = dict[str, Union[list[str], "ABC"]]

But this does not

ABC = dict[str, list[str] | "ABC"]

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'types.GenericAlias' and 'str'

On python:3.14, I also tried to use the new PEP 649:

ABC = dict[str, list[str] | ABC]

Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    ABC = dict[str, list[str] | ABC]
                                ^^^
NameError: name 'ABC' is not defined

This is the same when using Union

from typing import Union
ABC = dict[str, Union[list[str], ABC]]

NameError: name 'ABC' is not defined

CPython versions tested on:

3.11, 3.12, 3.13, 3.14

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions