Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions confection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,11 @@ def _fill(
field_type = EmptySchema
if key in schema.__fields__:
field = schema.__fields__[key]
field_type = field.type_
if not isinstance(field.type_, ModelMetaclass):
if hasattr(field, "type_"):
field_type = field.type_
else:
field_type = field.annotation
if not isinstance(field_type, ModelMetaclass):
# If we don't have a pydantic schema and just a type
field_type = EmptySchema
filled[key], validation[v_key], final[key] = cls._fill(
Expand Down
33 changes: 32 additions & 1 deletion confection/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import pytest

try:
from pydantic import BaseModel as BaseModelv2
from pydantic.v1 import BaseModel, PositiveInt, StrictFloat, constr
from pydantic.v1.types import StrictBool
except ImportError:
from pydantic import BaseModel, StrictFloat, PositiveInt, constr # type: ignore
from pydantic import BaseModel, PositiveInt, StrictFloat, constr # type: ignore
from pydantic.types import StrictBool # type: ignore

BaseModelv2 = None # type: ignore

from confection import Config, ConfigValidationError
from confection.tests.util import Cat, make_tempdir, my_registry
from confection.util import Generator, partial
Expand Down Expand Up @@ -1431,3 +1434,31 @@ def test_parse_strings_interpretable_as_ints():
)
assert cfg["a"]["foo"] == [3, "003", "y"]
assert cfg["b"]["bar"] == 3


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_field_type():
if BaseModelv2 is not None:

class MyValue:
def validate(cls, value):
return value

@classmethod
def __get_validators__(cls):
yield cls.validate

class SectionSchema(BaseModelv2):
value: MyValue

class MainSchema(BaseModelv2):
section: SectionSchema

cfg = Config().from_str(
"""
[section]
value = 1
"""
)

my_registry.fill(cfg, schema=MainSchema)