In the basic case, one can easily map a dictionary to the parameters. The below shows the basic example.
def func1(x: int, y: int):
return x+y
input = {
"x": 1,
"y": 2,
}
## This Works
sum = func1(**input)
# sum = 3
Does Python provide any other types of shortcuts which would enable this type of behavior for nested classes?
from dataclasses import dataclass
@dataclass
class X:
x: int
@dataclass
class Y:
y: int
def func2(x: X, y: Y):
return x.x + y.y
input_2 = {
"X": {
"x": 1,
},
"Y": {
"y": 1,
},
}
sum = func2(**input_2)
# TypeError: func2() got an unexpected keyword argument 'X'
I have tried other approach's. This is an example fo something that works, but is not very general.
sum = func2(X(input_2[X][x]),Y(input_2[Y][y])
Also failed with pydantic
from pydantic import BaseModel
class X(BaseModel):
x: int
class Y(BaseModel):
y: int
def func2(x: X, y: Y):
return x.x + y.y
input_2 = {
"X": {
"x": 1,
},
"Y": {
"y": 1,
},
}
sum = func2(**input_2)
TypeError
because the keys of the dictionary must map to the names of the arguments of the function. Since you have a key"X"
, python expects an argument namedX
. Instead you have one namedx
with typeX
. I don't know of any way to map the elements of the dictionary to arguments based on the type annotation. – Code-Apprentice