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

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

Closed
tmke8 opened this issue May 14, 2024 · 1 comment · Fixed by #11530
Closed

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

tmke8 opened this issue May 14, 2024 · 1 comment · Fixed by #11530
Labels
rule Implementing or modifying a lint rule

Comments

@tmke8
Copy link
Contributor

tmke8 commented May 14, 2024

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.

@zanieb zanieb added the rule Implementing or modifying a lint rule label May 14, 2024
@zanieb
Copy link
Member

zanieb commented May 14, 2024

Makes sense to me!

charliermarsh pushed a commit that referenced this issue May 26, 2024
## Summary
Lint `TypeAliasType` in UP040.

Fixes #11422 

## Test Plan

cargo test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants