Skip to content

Commit

Permalink
fix ConfigProtocol checking
Browse files Browse the repository at this point in the history
Blocking issue resolved by this update to mypy
python/mypy#6568
python/mypy#8578

we can now check that a DataClass object conforms to our ConfigProtocol
Protocol, allowing us to pass ally mypy checks.
  • Loading branch information
lundybernard committed Jan 21, 2022
1 parent a502ebc commit c878084
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions batconf/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .source import SourceList


@typing.runtime_checkable
class ConfigProtocol(typing.Protocol):
__dataclass_fields__: dict
__module__: str
Expand Down
13 changes: 7 additions & 6 deletions batconf/sources/dataclass.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from dataclasses import (
dataclass,
fields,
is_dataclass,
_MISSING_TYPE,
)

from typing import Any

from ..source import SourceInterface
from ..manager import ConfigProtocol


class DataclassConfig(SourceInterface):

def __init__(self, ConfigClass: dataclass):
def __init__(self, ConfigClass: ConfigProtocol):
'''Extract default values from the Config dataclass
Properties without defaults are set to None
'''
self._root = ConfigClass.__module__
self._data = dict()
self._data: dict = dict()

for field in fields(ConfigClass):
if is_dataclass(field.type):
if isinstance(field.type, ConfigProtocol):
self._data[field.name] = DataclassConfig(field.type)
elif type(field.default) is _MISSING_TYPE:
self._data[field.name] = None
Expand All @@ -35,7 +36,7 @@ def get(self, key: str, module: str = None):
else:
path = key.split('.')

conf = self._data
conf: Any = self._data
for k in path:
if not (conf := conf.get(k)):
return conf
Expand Down
3 changes: 2 additions & 1 deletion batconf/sources/tests/dataclass_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import TestCase
from dataclasses import dataclass

from ..dataclass import DataclassConfig, dataclass
from ..dataclass import DataclassConfig


class TestDataclassConfig(TestCase):
Expand Down

0 comments on commit c878084

Please sign in to comment.