From 7161604925aa4ef846dcafa442a03b7cf215d1a1 Mon Sep 17 00:00:00 2001 From: Dom Batten Date: Mon, 19 Aug 2024 14:57:32 +0100 Subject: [PATCH] Clearer docs on `coerce_numbers_to_str` --- docs/usage.md | 6 +++++- tests/unit/test_config.py | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 817afe5..00bb4d7 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -174,7 +174,7 @@ be raised. A schema can be added after instantiation through a setter: config.schema = MySchema ``` -## Casting and default values +### Casting and default values By default, `maison` will replace the values in the config with whatever comes back from the validation. For example, for a config file that looks like this: @@ -187,7 +187,11 @@ foo = 1 And a schema that looks like this: ```python +from pydantic import BaseModel, ConfigDict + class MySchema(BaseModel): + model_config = ConfigDict(coerce_numbers_to_str=True) + foo: str bar: str = "my_default" ``` diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 3bb942f..9b02c6c 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -5,6 +5,7 @@ import pytest from pydantic import BaseModel +from pydantic import ConfigDict from pydantic import ValidationError from maison.config import UserConfig @@ -205,9 +206,11 @@ def test_use_schema_values( ) -> None: """Config values can be cast to the validated values.""" - class Schema(BaseModel, coerce_numbers_to_str=True): + class Schema(BaseModel): """Defines schema.""" + model_config = ConfigDict(coerce_numbers_to_str=True) + bar: str other: str = "hello" @@ -229,9 +232,11 @@ def test_not_use_schema_values( ) -> None: """If `use_schema_values` is set to False then don't use validated values.""" - class Schema(BaseModel, coerce_numbers_to_str=True): + class Schema(BaseModel): """Defines schema.""" + model_config = ConfigDict(coerce_numbers_to_str=True) + bar: str other: str = "hello"