You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue - If we pass dict directly from terminal, it will be coming as string to us.
Fix: We can use pydantic Json with given dict type and use pydantic TypeAdapter to validate data and get the actual value
Usage
importjsonfrompydanticimportTypeAdapterfrompydantic.typesimportJsondict_type=dict[str, int] #type from method annotationsdata='{"key": 1, "key2": 2}'#input from terminal - json datatype_adapter=TypeAdapter(Json[dict_type])
t2=type_adapter.validate_python(data, strict=True)
print(t2) #pass this value to function
Usage with typer
defmain(data: dict[str, int]):
print(f"data is {data}")
$ typer main.py run {"key": 1, "key2": 2}data is {"key": 1, "key2": 2}
NOTE: User should provide json data as input
@tiangolo If this is ok, we can follow the same for classes as types, let user provide data as dict and pydantic will handle the validation
My CLI app can use a profile key, which is used to load the appropriate config:
However, this errors with:
The solution you would like
The ability to support Dict as a type, when returned from a callback function.
The text was updated successfully, but these errors were encountered: