Allow use of `Required` and `NotRequired` to make an existing typed dict total or optional
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- python
- Domain
- developer-experience
Research direction
Start by reviewing the proposed TypedDict, Required, and NotRequired semantics and the examples in the issue, including inheritance and inline annotations. Define the behavior and identify the relevant typing specification and conformance tests; done means the requested transformations are specified and validated consistently.
Written by the indexing model from the issue text.
Description
I think the ability of indicating that a typed dict requires all keys or that all are optional would be very useful.
For example to allow defining default values for a particular dict or to return a dict that has been completely assigned from a function
A simple example
class A(TypedDict):
a: int
b: int
ADefault = {'a': 1, 'b': 42}
def optionalA(v: NotRequired[A]) -> A:
return ADefault | v
optionalA({'a': 11}) # ok since all keys of NotRequired[A] are optional
class X(TypedDict, total=False):
x: str
y: str
XDefault: X = {"x": "xv", "y": "yv"}
def withXDefault(v: X) -> Required[X]:
return XDefault | v
withXDefault({"x": "foo"})["y"] # ok since all keys of Required[X] are required
This idea is inspired by the analogous types in typescript Required and Partial that are in general very convenient.
I think it makes sense having the same behaviour of the typescript counterparts:
NotRequired[X]is a no op, since X is defined astotal=False. Same forRequired[A].Required/NotRequiredwould include all keys of the typed dict and all its superclasses. This differs from howtotalworks. Example:class TD1(TypedDict): a: int class TD2(TypedDict, total=False): b: int TDR = Required[TD2] # b becomes required TDNR = NotRequired[TD2] # a becomes not requiredRequired/NotRequiredused inline in the TypedDict are not taken into consideration, so the end result is the same independently of how the typed dict is defined
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
learningequality/ricecooker#747 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
BSData/horus-heresy-3rd-edition#3171 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
run-llama/llama_index#23199 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
KhronosGroup/glTF-Blender-IO#2769 ·