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

Add support for env_prefix config setting #29

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/settings_doc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _model_fields(settings: Set[Type[BaseSettings]]) -> Iterator[Tuple[str, Fiel
else:
LOGGER.error(f"Unsupported validation alias type '{type(model_field.validation_alias)}'.")
else:
yield field_name, model_field
yield cls.model_config["env_prefix"] + field_name, model_field


@app.command()
Expand Down
8 changes: 7 additions & 1 deletion tests/fixtures/valid_settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Literal

from pydantic import AliasChoices, AliasPath, Field
from pydantic_settings import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict

SETTINGS_ATTR = "logging_level"
SETTINGS_MARKDOWN_FIRST_LINE = f"# `{SETTINGS_ATTR}`\n"
Expand Down Expand Up @@ -64,3 +64,9 @@ class ValidationAliasPathSettings(BaseSettings):

class ValidationAliasChoicesSettings(BaseSettings):
logging_level: str = Field(..., validation_alias=AliasChoices("logging", "level"))


class EnvPrefixSettings(BaseSettings):
logging_level: str

model_config = SettingsConfigDict(env_prefix="PREFIX_")
3 changes: 3 additions & 0 deletions tests/integration/generate/test_import_module_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tests.fixtures.module_with_single_settings_class import SingleSettingsInModule
from tests.fixtures.valid_settings import (
EmptySettings,
EnvPrefixSettings,
ExamplesSettings,
FullSettings,
MultipleSettings,
Expand Down Expand Up @@ -44,6 +45,7 @@ class TestImportModulePath:
ValidationAliasPathSettings,
ValidationAliasChoicesSettings,
ExamplesSettings,
EnvPrefixSettings,
},
id="for a module with multiple matching classes",
),
Expand All @@ -60,6 +62,7 @@ class TestImportModulePath:
ValidationAliasPathSettings,
ValidationAliasChoicesSettings,
ExamplesSettings,
EnvPrefixSettings,
},
id="for multiple modules with multiple matching classes",
),
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from tests.fixtures.valid_settings import (
SETTINGS_MARKDOWN_FIRST_LINE,
EmptySettings,
EnvPrefixSettings,
ExamplesSettings,
FullSettings,
MultipleSettings,
Expand Down Expand Up @@ -199,3 +200,8 @@ def should_put_empty_line_before_second_header(runner: CliRunner, mocker: Mocker
def should_end_with_a_single_empty_line(runner: CliRunner, mocker: MockerFixture):
stdout = run_app_with_settings(mocker, runner, MultipleSettings)
assert not stdout.endswith("`\n\n"), f"'{stdout}' ends with empty line"

@staticmethod
def should_include_env_prefix(runner: CliRunner, mocker: MockerFixture):
expected_string = "# `prefix_logging_level`\n\n**required**\n\n"
assert run_app_with_settings(mocker, runner, EnvPrefixSettings) == expected_string