Skip to content

UP: Rule for rewriting typing_extensions.TypeAliasType as type ... on Python 3.12 #11422

@tmke8

Description

@tmke8

On Python 3.12, the new type alias syntax, type X = Y, produces a typing.TypeAliasType object, which has been backported to typing_extensions.

And pydantic makes use of this typing_extensions.TypeAliasType: https://docs.pydantic.dev/latest/concepts/types/#named-type-aliases

From the pydantic docs:

from typing import List, TypeVar

from annotated_types import Gt
from typing_extensions import Annotated, TypeAliasType

T = TypeVar('T')  # or a `bound=SupportGt`

PositiveList = TypeAliasType(
    'PositiveList', List[Annotated[T, Gt(0)]], type_params=(T,)
)

(Why aren't they just using TypeAlias? Because then pydantic can't access the name of the alias. And there are also problems with recursive aliases.)

It would be nice if this could automatically be rewritten to

from annotated_types import Gt
from typing import Annotated

type PositiveList[T] = list[Annotated[T, Gt(0)]]

on Python 3.12.

This should be an entirely safe fix, because it's just different syntax to produce the same result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions