Skip to content

Import rewrite issue with TypedDict #683

@cdce8p

Description

@cdce8p

In Python 3.11 TypedDict can inherit from Generic which hasn't been possible so far. To use it in prior versions, TypedDict can be imported from typing_extensions. I believe it would make sense to postpone import rewriting for it until 3.11.

Example from the Python docs

from typing import Generic, TypeVar
from typing_extensions import TypedDict

T = TypeVar("T")

class Group(TypedDict, Generic[T]):
    key: T
    group: list[T]

After pyupgrade --py38-plus

from typing import Generic, TypeVar
from typing import TypedDict
...

--
Besides TypedDict, NamedTuple will also be able to inherit from Generic in 3.11 or can be imported from typing_extensions. This isn't an issue yet though, since pyupgrade doesn't rewrite it.

from typing import Generic, TypeVar
from typing_extensions import NamedTuple

T = TypeVar("T")

class Group(NamedTuple, Generic[T]):
    key: T
    group: list[T]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions