fix: config, give up on typing Config.get
It is not possible to have a dynamic return type based on what would come out of a dict AND an optional parameter.
This commit is contained in:
parent
5a59eb1d4e
commit
3391a44206
1 changed files with 12 additions and 16 deletions
|
@ -19,7 +19,7 @@ import pkg_resources
|
||||||
from configparser import RawConfigParser, NoOptionError, NoSectionError
|
from configparser import RawConfigParser, NoOptionError, NoSectionError
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from shutil import copy2
|
from shutil import copy2
|
||||||
from typing import Callable, Dict, List, Optional, Union, Tuple, cast, TypeVar
|
from typing import Callable, Dict, List, Optional, Union, Tuple, cast, Any
|
||||||
|
|
||||||
from poezio.args import parse_args
|
from poezio.args import parse_args
|
||||||
from poezio import xdg
|
from poezio import xdg
|
||||||
|
@ -158,8 +158,6 @@ DEFAULT_CONFIG: ConfigDict = {
|
||||||
'muc_colors': {}
|
'muc_colors': {}
|
||||||
}
|
}
|
||||||
|
|
||||||
T = TypeVar('T', bool, int, float, str)
|
|
||||||
|
|
||||||
|
|
||||||
class PoezioConfigParser(RawConfigParser):
|
class PoezioConfigParser(RawConfigParser):
|
||||||
def optionxform(self, value) -> str:
|
def optionxform(self, value) -> str:
|
||||||
|
@ -196,8 +194,8 @@ class Config:
|
||||||
|
|
||||||
def get(self,
|
def get(self,
|
||||||
option: str,
|
option: str,
|
||||||
default: Optional[T] = None,
|
default: Optional[ConfigValue] = None,
|
||||||
section=DEFSECTION) -> Optional[T]:
|
section: str = DEFSECTION) -> Any:
|
||||||
"""
|
"""
|
||||||
get a value from the config but return
|
get a value from the config but return
|
||||||
a default value if it is not found
|
a default value if it is not found
|
||||||
|
@ -205,20 +203,18 @@ class Config:
|
||||||
returned
|
returned
|
||||||
"""
|
"""
|
||||||
if default is None:
|
if default is None:
|
||||||
section = self.default.get('section')
|
section_val = self.default.get(section)
|
||||||
if section is not None:
|
if section_val is not None:
|
||||||
option = section.get(option)
|
default = section_val.get(option)
|
||||||
if option is not None:
|
|
||||||
default = cast(T, option)
|
|
||||||
|
|
||||||
res: T
|
res: Optional[ConfigValue]
|
||||||
try:
|
try:
|
||||||
if isinstance(default, bool):
|
if isinstance(default, bool):
|
||||||
res = self.configparser.getboolean(section, option)
|
res = self.configparser.getboolean(section, option)
|
||||||
elif isinstance(default, float):
|
|
||||||
res = self.configparser.getfloat(section, option)
|
|
||||||
elif isinstance(default, int):
|
elif isinstance(default, int):
|
||||||
res = self.configparser.getint(section, option)
|
res = self.configparser.getint(section, option)
|
||||||
|
elif isinstance(default, float):
|
||||||
|
res = self.configparser.getfloat(section, option)
|
||||||
else:
|
else:
|
||||||
res = self.configparser.get(section, option)
|
res = self.configparser.get(section, option)
|
||||||
except (NoOptionError, NoSectionError, ValueError, AttributeError):
|
except (NoOptionError, NoSectionError, ValueError, AttributeError):
|
||||||
|
@ -732,11 +728,11 @@ LOGGING_CONFIG = {
|
||||||
# some help in the info buffer
|
# some help in the info buffer
|
||||||
firstrun = False
|
firstrun = False
|
||||||
|
|
||||||
# Global config object. Is setup in poezio.py
|
# Global config object. Is setup for real in poezio.py
|
||||||
config = None # type: Config
|
config = Config(Path('/dev/null'))
|
||||||
|
|
||||||
# The logger object for this module
|
# The logger object for this module
|
||||||
log = logging.getLogger(__name__) # type: logging.Logger
|
log: logging.Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# The command-line options
|
# The command-line options
|
||||||
options = None
|
options = None
|
||||||
|
|
Loading…
Reference in a new issue