-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: better support for ad-hoc configs
properly reload/unload the relevant modules so hopefully no more weird hacks should be required relevant - karlicoss/promnesia#340 - #46
- Loading branch information
Showing
3 changed files
with
97 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
''' | ||
Just a demo module for testing and documentation purposes | ||
''' | ||
from dataclasses import dataclass | ||
from typing import Iterator | ||
|
||
from my.core import make_config | ||
|
||
from my.config import simple as user_config | ||
|
||
|
||
@dataclass | ||
class simple(user_config): | ||
count: int | ||
|
||
|
||
config = make_config(simple) | ||
|
||
|
||
def items() -> Iterator[int]: | ||
yield from range(config.count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pathlib import Path | ||
import tempfile | ||
|
||
from my.core.cfg import tmp_config | ||
|
||
import pytest | ||
|
||
|
||
def _init_default_config(): | ||
import my.config | ||
class default_config: | ||
count = 5 | ||
my.config.simple = default_config # type: ignore[attr-defined] | ||
_init_default_config() | ||
|
||
|
||
from my.simple import items | ||
|
||
|
||
def test_tmp_config() -> None: | ||
assert len(list(items())) == 5 | ||
|
||
class config: | ||
class simple: | ||
count = 3 | ||
|
||
with tmp_config(modules='my.simple', config=config): | ||
assert len(list(items())) == 3 | ||
|
||
assert len(list(items())) == 5 |