fix some deprecation warnings

This commit is contained in:
Michael Genson 2025-06-23 20:50:42 +00:00
commit 779f12dbb8
2 changed files with 15 additions and 11 deletions

View file

@ -80,7 +80,7 @@ class MealieModel(BaseModel):
Adds UTC timezone information to all datetimes in the model. Adds UTC timezone information to all datetimes in the model.
The server stores everything in UTC without timezone info. The server stores everything in UTC without timezone info.
""" """
for field in self.model_fields: for field in self.__class__.model_fields:
val = getattr(self, field) val = getattr(self, field)
if not isinstance(val, datetime): if not isinstance(val, datetime):
continue continue
@ -94,7 +94,9 @@ class MealieModel(BaseModel):
Cast the current model to another with additional arguments. Useful for Cast the current model to another with additional arguments. Useful for
transforming DTOs into models that are saved to a database transforming DTOs into models that are saved to a database
""" """
create_data = {field: getattr(self, field) for field in self.model_fields if field in cls.model_fields} create_data = {
field: getattr(self, field) for field in self.__class__.model_fields if field in cls.model_fields
}
create_data.update(kwargs or {}) create_data.update(kwargs or {})
return cls(**create_data) return cls(**create_data)
@ -104,8 +106,8 @@ class MealieModel(BaseModel):
for method chaining. for method chaining.
""" """
for field in self.model_fields: for field in self.__class__.model_fields:
if field in dest.model_fields: if field in dest.__class__.model_fields:
setattr(dest, field, getattr(self, field)) setattr(dest, field, getattr(self, field))
return dest return dest
@ -115,8 +117,8 @@ class MealieModel(BaseModel):
Map matching values from another model to the current model. Map matching values from another model to the current model.
""" """
for field in src.model_fields: for field in src.__class__.model_fields:
if field in self.model_fields: if field in self.__class__.model_fields:
setattr(self, field, getattr(src, field)) setattr(self, field, getattr(src, field))
def merge[T: BaseModel](self, src: T, replace_null=False): def merge[T: BaseModel](self, src: T, replace_null=False):
@ -124,9 +126,9 @@ class MealieModel(BaseModel):
Replace matching values from another instance to the current instance. Replace matching values from another instance to the current instance.
""" """
for field in src.model_fields: for field in src.__class__.model_fields:
val = getattr(src, field) val = getattr(src, field)
if field in self.model_fields and (val is not None or replace_null): if field in self.__class__.model_fields and (val is not None or replace_null):
setattr(self, field, val) setattr(self, field, val)
@classmethod @classmethod

View file

@ -6,14 +6,16 @@ def mapper[U: BaseModel, T: BaseModel](source: U, dest: T, **_) -> T:
Map a source model to a destination model. Only top-level fields are mapped. Map a source model to a destination model. Only top-level fields are mapped.
""" """
for field in source.model_fields: for field in source.__class__.model_fields:
if field in dest.model_fields: if field in dest.__class__.model_fields:
setattr(dest, field, getattr(source, field)) setattr(dest, field, getattr(source, field))
return dest return dest
def cast[U: BaseModel, T: BaseModel](source: U, dest: type[T], **kwargs) -> T: def cast[U: BaseModel, T: BaseModel](source: U, dest: type[T], **kwargs) -> T:
create_data = {field: getattr(source, field) for field in source.model_fields if field in dest.model_fields} create_data = {
field: getattr(source, field) for field in source.__class__.model_fields if field in dest.model_fields
}
create_data.update(kwargs or {}) create_data.update(kwargs or {})
return dest(**create_data) return dest(**create_data)