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

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

Open
Kamforka opened this issue Oct 7, 2024 · 0 comments

Comments

@Kamforka
Copy link
Collaborator

Kamforka commented Oct 7, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant