Skip to content

Commit

Permalink
chore(merge): Merge branch 'release/v2' into 'feature/keep-yaml-data'
Browse files Browse the repository at this point in the history
  • Loading branch information
acederberg committed Jul 18, 2024
2 parents 0c425bd + f79d131 commit 5f422b1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ maintainers = [
"Helio Chissini de Castro <[email protected]>"
]
name = "yaml-settings-pydantic"
version = "2.3.1"
version = "2.3.2"
description = "A tool to easily load (many) JSON/YAML files as pydantic settings."
readme = "README.rst"
keywords = [
Expand Down Expand Up @@ -197,7 +197,7 @@ pytest-mypy = "^0.10.3"


[tool.bumpver]
current_version = "2.3.1"
current_version = "2.3.2"
version_pattern = "MAJOR.MINOR.PATCH"
commit_message = "chore(version): Bump version {old_version} -> {new_version}"
commit = true
Expand Down
5 changes: 5 additions & 0 deletions tests/extra/whatever.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
whatever:
the: heck
should: be
here: dude

34 changes: 34 additions & 0 deletions tests/test_extra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

from pathlib import Path

from yaml_settings_pydantic import (
BaseYamlSettings,
YamlFileConfigDict,
YamlSettingsConfigDict,
)

WHATEVER = Path(__file__).parent / "extra" / "whatever.yaml"


def test_yaml_files_dict_string_keys() -> None:
"""For now, make sure that this does not fail."""

class Settings(BaseYamlSettings):
the: str
should: str
here: str

model_config = YamlSettingsConfigDict(
yaml_files={
WHATEVER: YamlFileConfigDict(
subpath="whatever",
required=True,
)
}
)

settings = Settings.model_validate({})
assert settings.the == "heck"
assert settings.should == "be"
assert settings.here == "dude"
1 change: 1 addition & 0 deletions yaml_settings_pydantic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from yaml_settings_pydantic.settings import BaseYamlSettings, CreateYamlSettings

__version__ = "2.3.1"
__version__ = "2.3.2"


__all__ = (
Expand Down
23 changes: 9 additions & 14 deletions yaml_settings_pydantic/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ def validate_yaml_settings_config_files(

# NOTE: If its a string/``Path``, make it into a tuple. If it is anything
# else just leave it.
values: (
tuple[Path, ...]
| dict[str, YamlFileConfigDict]
| dict[Path, YamlFileConfigDict]
| set[Path]
| set[str]
)
values: tuple[Path, ...] | dict[Path, YamlFileConfigDict] | set[Path]
if isinstance(found_value, Path):
# logger.debug(f"`{item}` was a PosixPath.")
values = (found_value,)
elif isinstance(found_value, str):
# logger.debug(f"`{item}` was a String.")
values = (Path(found_value),)
elif isinstance(found_value, dict) and any(
isinstance(item, str) for item in found_value
):
values = {
k if isinstance(k, Path) else Path(k): v for k, v in found_value.items()
}
else:
values = found_value
values = found_value # type: ignore

keys_invalid = {item for item in values if not isinstance(item, Path)}
if len(keys_invalid):
Expand All @@ -161,12 +161,7 @@ def validate_yaml_settings_config_files(
# NOTE: Create dictionary if the sequence is not a dictionary.
files: dict[Path, YamlFileConfigDict]
if not isinstance(values, dict):
files = {
(
k if isinstance(k, Path) else Path(k)
): DEFAULT_YAML_FILE_CONFIG_DICT.copy()
for k in values
}
files = {k: DEFAULT_YAML_FILE_CONFIG_DICT.copy() for k in values}
elif any(not isinstance(v, dict) for v in values.values()):
raise ValueError(f"`{item}` values must have type `dict`.")
elif not len(values):
Expand Down

0 comments on commit 5f422b1

Please sign in to comment.