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
See below. The idea is to use __post_init__
and check the annotations of z
. Uncomment #z: int
and see how it works.
from dataclasses import dataclass
from typing import Optional
@dataclass
class Params:
x: int
y: int
z: Optional[int] = None
#z: int
def __post_init__(self):
print('post init')
if self.z is None:
z_annotation = self.__annotations__['z']
none_is_ok = False
args = z_annotation.__dict__.get('__args__')
if args is not None:
for entry in args:
none_is_ok = entry == type(None)
if none_is_ok:
break
if not none_is_ok:
raise ValueError('z can not be None')
p: Params = Params(3, 5, None)
print(p)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.