typeddict Questions
4
I'm working on code bases with extensive type hints, checked by mypy. There's several instances where we have a mapping from an enum.Enum or other small finite set of statically known values (typin...
Sinister asked 27/4, 2022 at 1:49
1
I have a dataclass let's say:
from dataclasses import dataclass
@dataclass
class Foo:
bar: int
baz: int
I have a function that is called from an API receiving json and loading it as a Dict:
def...
Bemoan asked 12/10, 2022 at 12:50
1
Solved
This answer tells how to validated a TypedDict at runtime, but it won't catch the case of an extra key being present.
For example, this doesn't throw. How can I make it throw?
from typing import An...
2
Solved
I am trying to create a TypedDict for better code completion and am running into an issue.
I want to have a fixed set of keys (an Enum) and the values to match a specific list of objects depending ...
Curd asked 4/6, 2023 at 5:38
6
I'm working in a Python 3.8+ Django/Rest-Framework environment enforcing types in new code but built on a lot of untyped legacy code and data. We are using TypedDicts extensively for ensuring that ...
Nicki asked 17/3, 2021 at 0:18
2
I wanted to have a schema validation using pydantic, and also TypedDict to define the part of a nested dict schema. However, I realised that the Optional does not work if it is specified within the...
1
In typescript we have Partial type, so we can do this:
interface Foo {
x:number
y:number
}
const foo:Partial<Foo> = {x: 1}
(With this we can make all properties of an interface optional)
...
Lithiasis asked 7/9, 2021 at 16:33
2
I am trying to create a TypedDict with optional keys but neither Optional neither NotRequired does not work. (I tried NotRequired based on this SO answer)
class MyTypedDict(TypedDict):
my_key1: Op...
Phyliciaphylis asked 21/5, 2022 at 5:9
2
Solved
I have an existing TypedDict containing multiple entries:
from typing import TypedDict
class Params(TypedDict):
param1:str
param2:str
param3:str
I want to create the exact same TypedDict but wi...
Fitzpatrick asked 24/3, 2022 at 9:41
1
I have a bunch of @dataclasses and a bunch of corresponding TypedDicts, and I want to facilitate smooth and type-checked conversion between them.
For example, consider
from dataclasses import datac...
Spithead asked 7/4, 2021 at 11:49
1
Solved
Here's an example:
from typing import TypedDict
class Foo(TypedDict):
a: str
b: int
foo = Foo(a='2', b=4)
for key in foo:
print(foo[key])
I get
main.py:10: note: Revealed type is 'Any'
main....
Ensue asked 15/6, 2021 at 17:51
1
Solved
I have a dict with of which the types of the keys and values are fixed. I want to define the types in a TypedDict as follows:
class MyTable(TypedDict):
caption: List[str]
header: List[str]
table...
1
© 2022 - 2024 — McMap. All rights reserved.