Skip to content

Refactor *Required types with TypedDict Required and NotRequired properties #351

@Kamforka

Description

@Kamforka

PEP-655 introduced the Required and NotRequired type hints for TypedDicts, this can be used as a simplification in the *Required type hints like.

Example:

class InputAlertRequired(TypedDict):
    title: str
    description: str

class InputAlert(InputAlertRequired, total=False):
    tags: list[str]

can be simplified into a single object like:

class InputAlert(TypedDict, total=False):
    title: Required[str]
    description: Required[str]
    tags: list[str]

or alternatively:

class InputAlert(TypedDict):
    title: str
    description: str
    tags: NotRequired[list[str]]

However these new type hints are not supported by older python versions so it makes sense to introduce typing_extensions as a dependency to the project to retrofit the typehints to older versions.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions