Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing.Annotated does not flatten through typing.TypeAliasType #130117

Open
vberlier opened this issue Feb 14, 2025 · 2 comments
Open

typing.Annotated does not flatten through typing.TypeAliasType #130117

vberlier opened this issue Feb 14, 2025 · 2 comments
Labels
docs Documentation in the Doc dir topic-typing

Comments

@vberlier
Copy link

vberlier commented Feb 14, 2025

Bug report

Bug description:

As described in the documentation, nested Annotated types are supposed to be flattened. This works well with legacy type aliases but not with the type statement introduced in python 3.12:

from typing import Annotated, TypeAlias

print(Annotated[Annotated[int, "A"], "B"] == Annotated[int, "A", "B"]) # True

A: TypeAlias = Annotated[int, "A"]
print(Annotated[A, "B"] == Annotated[int, "A", "B"]) # True

type A = Annotated[int, "A"]
print(Annotated[A, "B"] == Annotated[int, "A", "B"]) # False

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@vberlier vberlier added the type-bug An unexpected behavior, bug, or error label Feb 14, 2025
@JelleZijlstra
Copy link
Member

This behavior is correct and we will not change it. Flattening through TypeAliasType may force evaluation of the type alias and lead to confusing errors.

I'd welcome a documentation PR clarifying this.

@Viicos
Copy link
Contributor

Viicos commented Mar 24, 2025

Note: The typing-inspection library provides utilities to help in this cases:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-typing
Projects
Status: Todo
Development

No branches or pull requests

4 participants