Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Ruff & use pydantic-settings for config #28

Merged
merged 9 commits into from
Sep 12, 2023

Conversation

ChrisLovering
Copy link
Member

This will require changes to the deployment.yaml, and secrets added to production before merging.

Comment on lines +11 to +49
def custom_decode_complex_value(
__: str,
___: fields.FieldInfo,
value: t.Any,
) -> t.Any:
"""Parse complex values as CSV if they cannot be parsed as JSON."""
try:
return json.loads(value)
except ValueError:
return value.split(",")


class EnvConfig(
BaseSettings,
env_file=".env",
env_file_encoding="utf-8",
env_nested_delimiter="__",
extra="ignore",
):
"""Our default configuration for models that should load from .env files."""

@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
"""Monkey patch default sources to have the custom CSV parser fallback."""
dotenv_settings.decode_complex_value = custom_decode_complex_value
env_settings.decode_complex_value = custom_decode_complex_value
return (
init_settings,
env_settings,
dotenv_settings,
file_secret_settings,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisLovering ChrisLovering merged commit 1561d04 into main Sep 12, 2023
4 checks passed
@ChrisLovering ChrisLovering deleted the ruff-pydantic-settings branch September 12, 2023 23:25
@ChrisLovering ChrisLovering temporarily deployed to github-pages September 12, 2023 23:25 — with GitHub Pages Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants