fixes in .configparse

This commit is contained in:
Yusur 2025-06-30 13:51:03 +02:00
parent 04ce86a43e
commit 96d25e0e85
3 changed files with 10 additions and 5 deletions

View file

@ -3,6 +3,7 @@
## 0.3.4 ## 0.3.4
- Bug fixes in `.flask_sqlalchemy` and `.sqlalchemy``require_auth()` is unusable before this point! - Bug fixes in `.flask_sqlalchemy` and `.sqlalchemy``require_auth()` is unusable before this point!
- Fixed a bug in `.configparse` dealing with unset values from multiple sources
## 0.3.3 ## 0.3.3

View file

@ -27,7 +27,7 @@ from .itertools import makelist, kwargs_prefix, ltuple, rtuple, additem
from .i18n import I18n, JsonI18n, TomlI18n from .i18n import I18n, JsonI18n, TomlI18n
from .snowflake import Snowflake, SnowflakeGen from .snowflake import Snowflake, SnowflakeGen
__version__ = "0.3.4.rc1" __version__ = "0.3.4.rc2"
__all__ = ( __all__ = (
'Siq', 'SiqCache', 'SiqType', 'SiqGen', 'StringCase', 'Siq', 'SiqCache', 'SiqType', 'SiqGen', 'StringCase',

View file

@ -20,7 +20,7 @@ from ast import TypeVar
from collections.abc import Mapping from collections.abc import Mapping
from configparser import ConfigParser as _ConfigParser from configparser import ConfigParser as _ConfigParser
import os import os
from typing import Any, Callable, Iterable, Iterator from typing import Any, Callable, Iterator
from collections import OrderedDict from collections import OrderedDict
from .functools import deprecated_alias from .functools import deprecated_alias
@ -78,6 +78,8 @@ class ConfigParserConfigSource(ConfigSource):
_cfp: _ConfigParser _cfp: _ConfigParser
def __init__(self, cfp: _ConfigParser): def __init__(self, cfp: _ConfigParser):
if not isinstance(cfp, _ConfigParser):
raise TypeError(f'a ConfigParser object is required (got {cfp.__class__.__name__!r})')
self._cfp = cfp self._cfp = cfp
def __getitem__(self, key: str, /) -> str: def __getitem__(self, key: str, /) -> str:
k1, _, k2 = key.partition('.') k1, _, k2 = key.partition('.')
@ -174,10 +176,12 @@ class ConfigValue:
owner.expose(self._pub_name, name) owner.expose(self._pub_name, name)
def __get__(self, obj: ConfigOptions, owner=False): def __get__(self, obj: ConfigOptions, owner=False):
if self._val is MISSING: if self._val is MISSING:
v = MISSING
for srckey, src in obj._srcs.items(): for srckey, src in obj._srcs.items():
v = src.get(self._srcs[srckey], MISSING) if srckey in self._srcs:
if self._required and not v: v = src.get(self._srcs[srckey], v)
raise MissingConfigError(f'required config {self._src} not set!') if self._required and (not v or v is MISSING):
raise MissingConfigError(f'required config {self._srcs['default']} not set!')
if v is MISSING: if v is MISSING:
v = self._default v = self._default
if callable(self._cast): if callable(self._cast):