Skip to content

Commit

Permalink
Merge pull request #313 from dbatten5/number-coerce
Browse files Browse the repository at this point in the history
Clearer docs on `coerce_numbers_to_str`
  • Loading branch information
dbatten5 authored Aug 19, 2024
2 parents 2c2fc2f + 7161604 commit 8a5f4a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 5 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
```
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
from pydantic import BaseModel
from pydantic import ConfigDict
from pydantic import ValidationError

from maison.config import UserConfig
Expand Down Expand Up @@ -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"

Expand All @@ -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"

Expand Down

0 comments on commit 8a5f4a8

Please sign in to comment.