Skip to content

TypedDict in dynamic contexts #13940

Closed as not planned
Closed as not planned
@jzazo

Description

@jzazo

Feature

I would like to be able to instantiate TypedDicts in dynamic contexts, for example:

tdict: dict[str, type] = {}
tdict["foo"] = int
tdict["bar"] = bool
booOutput = TypedDict("booOutput", tdict)

def boo(**kwargs) -> booOutput:
    return booOutput(**kwargs)

boo(foo=2, bar=False)

Pitch

I want to declare dynamic classes where I don't know statically what a method will return. But when I build the class I know the specific output types, and I would like to declare them so that I can rely on inspect.get_annotations or equivalent.

If TypedDict is not an option, is there a way that I can specify a return annotation dynamically when I create the class (a mapping return annotation)? This issue might be related. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions