Skip to content

Commit

Permalink
use config_schema instead of schema
Browse files Browse the repository at this point in the history
- more explicit and avoids clashes with `pydantic`
  • Loading branch information
dbatten5 committed Oct 28, 2021
1 parent d8e5817 commit 5e5d1d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Then inject the schema when instantiating a `ProjectConfig`:
```python
from maison import ProjectConfig

config = ProjectConfig(project_name="acme", schema=MySchema)
config = ProjectConfig(project_name="acme", config_schema=MySchema)
```

To validate the config, simply run `validate()` on the config instance:
Expand Down Expand Up @@ -130,7 +130,7 @@ class MySchema(ConfigSchema):
Running the config through validation will render the following:

```python
config = ProjectConfig(project_name="acme", schema=MySchema)
config = ProjectConfig(project_name="acme", config_schema=MySchema)

config.to_dict() # {"foo": 1}

Expand All @@ -143,5 +143,5 @@ add a `use_schema_values=False` argument to the `validate` method.

## Schema precedence

The `validate` method also accepts a `schema` is an argument. If one is provided here,
The `validate` method also accepts a `config_schema` is an argument. If one is provided here,
it will be used instead of a schema passed as an init argument.
20 changes: 10 additions & 10 deletions src/maison/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(
project_name: str,
starting_path: Optional[Path] = None,
source_files: Optional[List[str]] = None,
schema: Optional[Type[ConfigSchema]] = None,
config_schema: Optional[Type[ConfigSchema]] = None,
) -> None:
"""Initialize the config.
Expand All @@ -29,7 +29,7 @@ def __init__(
file
source_files: an optional list of source config filenames to search for. If
none is provided then `pyproject.toml` will be used
schema: an optional `pydantic` model to define the config schema
config_schema: an optional `pydantic` model to define the config schema
"""
self.source_files = source_files or ["pyproject.toml"]
config_path, config_dict = _find_config(
Expand All @@ -39,7 +39,7 @@ def __init__(
)
self._config_dict: Dict[str, Any] = config_dict or {}
self.config_path: Optional[Path] = config_path
self.schema = schema
self.config_schema = config_schema

def __repr__(self) -> str:
"""Return the __repr__.
Expand Down Expand Up @@ -67,7 +67,7 @@ def to_dict(self) -> Dict[str, Any]:

def validate(
self,
schema: Optional[Type[ConfigSchema]] = None,
config_schema: Optional[Type[ConfigSchema]] = None,
use_schema_values: bool = True,
) -> None:
"""Validate the configuration.
Expand All @@ -87,19 +87,19 @@ class Schema(ConfigSchema):
{"foo": "1"}
Args:
schema: an optional `pydantic` base model to define the schema. This takes
precedence over a schema provided at object instantiation.
config_schema: an optional `pydantic` base model to define the schema. This
takes precedence over a schema provided at object instantiation.
use_schema_values: an optional boolean to indicate whether the result
of passing the config through the schema should overwrite the existing
config values, meaning values are cast to types defined in the schema as
described above, and default values defined in the schema are used.
"""
validated_schema: Optional[ConfigSchema] = None

if schema:
validated_schema = schema(**self._config_dict)
elif self.schema:
validated_schema = self.schema(**self._config_dict)
if config_schema:
validated_schema = config_schema(**self._config_dict)
elif self.config_schema:
validated_schema = self.config_schema(**self._config_dict)

if validated_schema and use_schema_values:
self._config_dict = validated_schema.dict()
Expand Down

0 comments on commit 5e5d1d3

Please sign in to comment.