Is it possible to check the parameter type hint at run time? I would like to use the Python dataclasses in a similar fashion to a Scala case class with pureconfig
and HOCON config file. That is, I want to have some optional parameters but would need to check them in another class constructor. However, I dont see how I can get if a param is optional.
from dataclasses import dataclass
from typing import Optional
@dataclass
class Params:
x: int
y: int
z: Optional[int] = None
params = Params(x=2, y=3)
Let's say I want to use the dataclass Params
somewhere. If I check z
, I will get a NoneType
. Maybe I can by pass any errors if I know this by design.
# just some example code
if z is None and z is not Optional (not sure if or how to check this):
raise ValueError("z must be specified as an integer")
if z is None and z is optional:
return x ** 2 + y ** 2
elif z is not None:
return x ** 2 + y ** 2 + z ** 2