didn't work but I found get_origin!

This commit is contained in:
əlemi 2022-04-19 00:19:13 +02:00
parent e18c725a6b
commit 280d97afc8
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -5,7 +5,7 @@ import asyncio
import datetime import datetime
import uuid import uuid
from typing import List, Dict, Optional, Any, Type, get_args, get_type_hints, Set from typing import List, Dict, Optional, Any, Type, get_args, get_origin, get_type_hints, Set
from time import time from time import time
from dataclasses import dataclass, MISSING, fields from dataclasses import dataclass, MISSING, fields
from configparser import ConfigParser from configparser import ConfigParser
@ -26,20 +26,17 @@ def parse_with_hint(val:str, hint:Any) -> Any:
if val.lower() in ['1', 'true', 't', 'on', 'enabled']: if val.lower() in ['1', 'true', 't', 'on', 'enabled']:
return True return True
return False return False
if hint in (list, List): if hint is list or get_origin(hint) is list:
if get_args(hint): if get_args(hint):
return [ parse_with_hint(x, get_args(hint)[0]) for x in val.split() ] return [ parse_with_hint(x, get_args(hint)[0]) for x in val.split() ]
return val.split() return val.split()
if hint in (set, Set): if hint is set or get_origin(hint) is set:
if get_args(hint): if get_args(hint):
return set( parse_with_hint(x, get_args(hint)[0]) for x in val.split() ) return set( parse_with_hint(x, get_args(hint)[0]) for x in val.split() )
return set(val.split()) return set(val.split())
if hint in (dict, Dict): if hint is dict or get_origin(hint) is dict:
return json.loads(val) return json.loads(val)
if isinstance(hint, type): # try to instantiate with type directly return (get_origin(hint) or hint)(val) # try to instantiate directly
return hint(val)
logging.warning("Could not parse %s with %s", val, str(hint))
return val # fallback
class ConfigObject: class ConfigObject:
def __getitem__(self, key: str) -> Any: def __getitem__(self, key: str) -> Any: