Skip to content

Commit

Permalink
fix: suppress pydantic deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
percevalw committed Oct 22, 2024
1 parent fba9049 commit d21cef6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Aborting a script will now show the traceback

### Fixed

- Confit should no longer cause pydantic v1 deprecation warnings

## v0.6.0 (2024-09-13)

### Fixed
Expand Down
9 changes: 5 additions & 4 deletions confit/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from confit.utils.xjson import Reference

Loc = Tuple[Union[int, str]]
PYDANTIC_V1 = pydantic.VERSION.split(".")[0] == "1"


class MissingReference(Exception):
Expand Down Expand Up @@ -243,11 +244,11 @@ def patch_errors(
# field_model.vd.model, pydantic.BaseModel
# ):
# field_model = field_model.vd.model
if hasattr(field_model, "model_fields"):
field_model = field_model.model_fields[part]
else:
if PYDANTIC_V1:
field_model = field_model.__fields__[part]
if hasattr(field_model, "type_"):
else:
field_model = field_model.model_fields[part]
if PYDANTIC_V1:
field_model = field_model.type_
else:
field_model = field_model.annotation
Expand Down
23 changes: 13 additions & 10 deletions confit/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ def invoked(kw):
# "self" must be passed as a positional argument
if use_self:
kw = {**kw, self_name: resolved}
extras = [
key
for key in kw
if key not in pydantic_func.model.__fields__ and key != extras_name
]
fields = (
pydantic_func.model.__fields__
if PYDANTIC_V1
else pydantic_func.model.model_fields
)
extras = [key for key in kw if key not in fields and key != extras_name]
try:
model_instance = pydantic_func.model(
**{
Expand Down Expand Up @@ -183,10 +184,10 @@ def validate(_func: Callable) -> Callable:
else:
vd = ValidatedFunction(_func.__init__, config)
vd.model.__name__ = _func.__name__
if hasattr(vd.model, "model_fields"):
vd.model.model_fields["self"].default = None
else:
if PYDANTIC_V1:
vd.model.__fields__["self"].default = None
else:
vd.model.model_fields["self"].default = None

# This function is called by Pydantic when asked to cast
# a value (most likely a dict) as a Model (most often during
Expand Down Expand Up @@ -293,8 +294,10 @@ def wrapper_function(*args: Any, **kwargs: Any) -> Any:
raise e.with_traceback(remove_lib_from_traceback(e.__traceback__))

_func.vd = vd
_func.__get_validators__ = __get_validators__
_func.__get_pydantic_core_schema__ = __get_pydantic_core_schema__
if PYDANTIC_V1:
_func.__get_validators__ = __get_validators__
else:
_func.__get_pydantic_core_schema__ = __get_pydantic_core_schema__
# _func.model = vd.model
# _func.model.type_ = _func
_func.__init__ = wrapper_function
Expand Down

0 comments on commit d21cef6

Please sign in to comment.