Skip to content

TypingDict Default Arguments #12980

Closed as not planned
Closed as not planned
@adam-grant-hendry

Description

@adam-grant-hendry

Feature

Issue #6131 request default types be added to TypedDict. To separate concerns, I am interested in being able to specify default arguments to TypedDict (as suggested in one of the comments).

Pitch

Default arguments are a feature of python. Currently, there's no support for default arguments in TypedDict. This would be helpful both for new and existing codebases.

Bonus: A common beginner blunder is to use mutable default arguments in functions and methods. AFAIK, linters (flake8, pylint) do not yet warn about this. It would be interesting if mypy (and other static type checkers) could detect this and provide a useful warning.

Syntax Idea

Borrowing from the comment suggestion, the following is intuitively what is trying to be achieved (though I don't know how this would be implemented):

class DefaultArgs(TypedDict):
    arg1: int = 1
    arg2: bool = True

The closest I am able to achieve currently is by using Annotated types, but it doesn't seem the intended use case for Annotated and I would prefer a more intuitive name like "Default":

from __future__ import annotations

from typing import Annotated

class DefaultArgs(TypedDict):
    arg1: Annotated[int, 1]
    arg2: Annotated[bool, True]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions