try to parse Union hints

This commit is contained in:
əlemi 2022-04-23 14:33:30 +02:00
parent 6f53778fc9
commit 5a652f1710
No known key found for this signature in database
GPG key ID: F239DD0985A7A5E9

View file

@ -6,7 +6,7 @@ import datetime
import uuid
import pkg_resources
from typing import List, Dict, Optional, Any, Type, get_args, get_origin, get_type_hints, Set, Callable
from typing import List, Dict, Optional, Union, Any, Type, get_args, get_origin, get_type_hints, Set, Callable
from time import time
from dataclasses import dataclass, MISSING, fields
from configparser import ConfigParser
@ -36,6 +36,12 @@ def parse_with_hint(val:str, hint:Any) -> Any:
return set(val.split())
if hint is dict or get_origin(hint) is dict:
return json.loads(val)
if hint is Union or get_origin(hint) is Union:
for t in get_args(hint):
try:
return t(val)
except ValueError:
pass
return (get_origin(hint) or hint)(val) # try to instantiate directly
class ConfigObject: