Skip to content

convert-typed-dict-functional-to-class (UP013)

Derived from the pyupgrade linter.

Fix is sometimes available.

What it does

Checks for TypedDict declarations that use functional syntax.

Why is this bad?

TypedDict subclasses can be defined either through a functional syntax (Foo = TypedDict(...)) or a class syntax (class Foo(TypedDict): ...).

The class syntax is more readable and generally preferred over the functional syntax.

Example

from typing import TypedDict

Foo = TypedDict("Foo", {"a": int, "b": str})

Use instead:

from typing import TypedDict


class Foo(TypedDict):
    a: int
    b: str

Fix safety

This rule's fix is marked as unsafe if there are any comments within the range of the TypedDict definition, as these will be dropped by the autofix.

References