Skip to content

Support for default types in TypedDict #6131

@rra

Description

@rra

This problem arose from trying to type the environ argument for a WSGI application. There are specific keys of the environ dict that have non-str types, and then all the other keys are HTTP_ variables (or server-defined variables, which can be ignored for this purpose) with type str (or Text, depending on the version of Python).

I would like to get stronger typing on environ than Dict[str, Any], but there isn't a way to do that with TypedDict currently because the set of possible keys is unbounded (the client can send any HTTP header).

Ideally, I'd like to be able to do something like:

EnvironDict = TypedDict("EnvironDict", {
    "wsgi.version": Tuple[int, int],
    "wsgi.input": IO[str],
# ...
}, default_type=str, total=False)

and have this mean that any key is allowed in the dictionary and all unknown keys get a value type of str.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions