Skip to content

Commit 581fb01

Browse files
committed
@sanderegg review: test origin of UserWarning
1 parent 92579b2 commit 581fb01

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

packages/settings-library/setup.cfg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ universal = 1
1414
# Define setup.py command aliases here
1515
test = pytest
1616

17-
# NOTE: uncomment when pytest-asyncio is added in requirements
18-
# [tool:pytest]
19-
# asyncio_mode = auto
17+
[tool:pytest]
18+
# SEE https://docs.pytest.org/en/stable/how-to/capture-warnings.html
19+
filterwarnings =
20+
error

packages/settings-library/tests/test__pydantic_settings.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
1313
"""
1414

15+
from typing import Annotated
16+
17+
import pytest
18+
from common_library.basic_types import LogLevel
1519
from common_library.pydantic_fields_extension import is_nullable
16-
from pydantic import ValidationInfo, field_validator
20+
from pydantic import AliasChoices, Field, ValidationInfo, field_validator
1721
from pydantic_core import PydanticUndefined
1822
from pydantic_settings import BaseSettings
1923
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
24+
from settings_library.application import BaseApplicationSettings
2025

2126

2227
def assert_field_specs(
@@ -168,3 +173,28 @@ def test_construct(monkeypatch):
168173
assert settings_from_both == settings_from_init.model_copy(
169174
update={"VALUE_NULLABLE_REQUIRED": 3}
170175
)
176+
177+
178+
class _TestSettings(BaseApplicationSettings):
179+
APP_LOGLEVEL: Annotated[
180+
LogLevel,
181+
Field(
182+
validation_alias=AliasChoices("APP_LOGLEVEL", "LOG_LEVEL"),
183+
),
184+
] = LogLevel.WARNING
185+
186+
187+
@pytest.mark.filterwarnings("error")
188+
def test_pydantic_serialization_user_warning(monkeypatch: pytest.MonkeyPatch):
189+
# This test is exploring the reason for `UserWarning`
190+
#
191+
# /python3.11/site-packages/pydantic/main.py:477: UserWarning: Pydantic serializer warnings:
192+
# Expected `enum` but got `str` with value `'WARNING'` - serialized value may not be as expected
193+
# return self.__pydantic_serializer__.to_json(
194+
#
195+
# NOTE: it seems settings.model_dump_json(warnings='none') is not the cause here of `UserWarning`
196+
monkeypatch.setenv("LOG_LEVEL", "DEBUG")
197+
198+
settings = _TestSettings.create_from_envs()
199+
assert settings.APP_LOGLEVEL == LogLevel.DEBUG
200+
assert settings.model_dump_json(indent=2)

packages/settings-library/tests/test_twilio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_twilio_settings_within_envdevel(
2020
},
2121
)
2222
settings = TwilioSettings.create_from_envs()
23-
print(settings.model_dump_json(indent=2, warnings="none"))
23+
print(settings.model_dump_json(indent=2))
2424
assert settings
2525

2626

packages/settings-library/tests/test_utils_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _v(cls, value: str) -> str:
4242
assert settings.LOG_LEVEL == "DEBUG"
4343

4444
assert (
45-
settings.model_dump_json(warnings="none")
45+
settings.model_dump_json()
4646
== '{"SC_BOOT_MODE":null,"LOG_LEVEL":"DEBUG","APPNAME_DEBUG":false}'
4747
)
4848

0 commit comments

Comments
 (0)