-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
223 additions
and
31 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "settings-doc" | ||
version = "4.2.0" | ||
version = "4.3.0" | ||
description = "A command line tool for generating Markdown documentation and .env files from pydantic BaseSettings." | ||
authors = ["Radek Lát <[email protected]>"] | ||
license = "MIT License" | ||
|
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,56 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from enum import Enum, EnumMeta, IntEnum | ||
from typing import Any, Callable | ||
from typing import Iterable as IterableCollection | ||
from typing import Literal | ||
|
||
import click | ||
from pydantic.fields import FieldInfo | ||
from pydantic_core import PydanticUndefined | ||
|
||
|
||
def _has_default_value(field: FieldInfo) -> bool: | ||
return field.default is not PydanticUndefined | ||
|
||
|
||
def _is_values_with_descriptions(value: Any) -> bool: | ||
if not isinstance(value, IterableCollection): | ||
click.secho(f"`examples` must be iterable but `{value}` used.", fg="red") | ||
raise click.Abort() | ||
|
||
return all(list(map(lambda item: isinstance(item, list) and 2 >= len(item) >= 1, value))) | ||
|
||
|
||
def _is_typing_literal(field: FieldInfo) -> bool: | ||
if sys.version_info < (3, 9) and field.annotation is not None and hasattr(field.annotation, "__origin__"): | ||
return field.annotation.__origin__ is Literal | ||
|
||
# The class doesn't exist in Python 3.8 and below | ||
return field.annotation.__class__.__name__ == "_LiteralGenericAlias" | ||
|
||
|
||
def _fix_str_enum_value(value: Any) -> Any: | ||
"""Fixes the value of an enum that subclasses `str`. | ||
In Python 3.10 and below, str + Enum can be used to create a StrEnum available in Python 3.11+. | ||
However, the value of the enum is not a string but an instance of the enum. | ||
""" | ||
if (isinstance(value, str) and isinstance(value, Enum)) or isinstance(value, IntEnum): | ||
return value.value | ||
|
||
return value | ||
|
||
|
||
def _is_enum(field: FieldInfo) -> bool: | ||
return isinstance(field.annotation, EnumMeta) | ||
|
||
|
||
JINJA_ENV_GLOBALS: dict[str, Callable] = { | ||
"has_default_value": _has_default_value, | ||
"is_values_with_descriptions": _is_values_with_descriptions, | ||
"is_typing_literal": _is_typing_literal, | ||
"fix_str_enum_value": _fix_str_enum_value, | ||
"is_enum": _is_enum, | ||
} |
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
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,48 @@ | ||
import enum | ||
import sys | ||
from typing import Type | ||
|
||
import pytest | ||
from pydantic_settings import BaseSettings | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def str_enum_settings() -> Type[BaseSettings]: | ||
if sys.version_info < (3, 11): | ||
StrEnum = enum.Enum # pylint: disable=invalid-name | ||
pytest.fail("StrEnum is not available in Python 3.10 and below") | ||
else: | ||
from enum import StrEnum # pylint: disable=import-outside-toplevel | ||
|
||
class LoggingLevelEnum(StrEnum): # type: ignore[valid-type, misc, unused-ignore] | ||
DEBUG = "debug" | ||
INFO = "info" | ||
|
||
class StrEnumSettings(BaseSettings): | ||
logging_level: LoggingLevelEnum = LoggingLevelEnum.DEBUG | ||
|
||
return StrEnumSettings | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def str_enum_subclass_settings() -> Type[BaseSettings]: | ||
class LoggingLevelEnum(str, enum.Enum): | ||
DEBUG = "debug" | ||
INFO = "info" | ||
|
||
class StrEnumSubclassSettings(BaseSettings): | ||
logging_level: LoggingLevelEnum = LoggingLevelEnum.DEBUG | ||
|
||
return StrEnumSubclassSettings | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def int_enum_settings() -> Type[BaseSettings]: | ||
class LoggingLevelEnum(enum.IntEnum): | ||
DEBUG = 10 | ||
INFO = 20 | ||
|
||
class IntEnumSettings(BaseSettings): | ||
logging_level: LoggingLevelEnum = LoggingLevelEnum.DEBUG | ||
|
||
return IntEnumSettings |
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