python / python/mypy

Support for default types in TypedDict

Open
#6,131 17 comments 36 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low topic-typed-dict
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue describes extending TypedDict with a default value type for otherwise unknown keys, using the WSGI environ example as motivation. Start by reviewing mypy’s existing TypedDict support and its tests; done means the proposed syntax is defined, arbitrary keys receive the default type, explicit keys retain their types, and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.