Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrate more deprecated pydantic calls #324

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
26 changes: 16 additions & 10 deletions src/uiprotect/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ def from_unifi_dict(
data.pop("api", None)
return cls(api=api, **data)

return cls.construct(**data)
return cls.model_construct(**data)

@classmethod
def construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self:
def model_construct(
cls, _fields_set: set[str] | None = None, **values: Any
) -> Self:
api: ProtectApiClient | None = values.pop("api", None)
(
unifi_objs,
Expand All @@ -152,19 +154,21 @@ def construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self:
) = cls._get_protect_model()
for key, value in values.items():
if has_unifi_objs and key in unifi_objs and isinstance(value, dict):
values[key] = unifi_objs[key].construct(**value)
values[key] = unifi_objs[key].model_construct(**value)
elif has_unifi_lists and key in unifi_lists and isinstance(value, list):
values[key] = [
unifi_lists[key].construct(**v) if isinstance(v, dict) else v
unifi_lists[key].model_construct(**v) if isinstance(v, dict) else v
for v in value
]
elif has_unifi_dicts and key in unifi_dicts and isinstance(value, dict):
values[key] = {
k: unifi_dicts[key].construct(**v) if isinstance(v, dict) else v
k: unifi_dicts[key].model_construct(**v)
if isinstance(v, dict)
else v
for k, v in value.items()
}

obj = super().construct(_fields_set=_fields_set, **values)
obj = super().model_construct(_fields_set=_fields_set, **values)
if api is not None:
obj._api = api

Expand Down Expand Up @@ -369,7 +373,7 @@ def _unifi_dict_protect_obj(
if isinstance(value, ProtectBaseObject):
value = value.unifi_dict()
elif isinstance(value, dict):
value = klass.construct({}).unifi_dict(data=value) # type: ignore[arg-type]
value = klass.model_construct({}).unifi_dict(data=value) # type: ignore[arg-type]

return value

Expand All @@ -390,7 +394,7 @@ def _unifi_dict_protect_obj_list(
return [
item.unifi_dict()
if isinstance(item, ProtectBaseObject)
else klass.construct({}).unifi_dict(data=item) # type: ignore[arg-type]
else klass.model_construct({}).unifi_dict(data=item) # type: ignore[arg-type]
for item in value
]

Expand Down Expand Up @@ -587,9 +591,11 @@ def __init__(self, **data: Any) -> None:
self._update_sync = update_sync or UpdateSynchronization()

@classmethod
def construct(cls, _fields_set: set[str] | None = None, **values: Any) -> Self:
def model_construct(
cls, _fields_set: set[str] | None = None, **values: Any
) -> Self:
update_sync = values.pop("update_sync", None)
obj = super().construct(_fields_set=_fields_set, **values)
obj = super().model_construct(_fields_set=_fields_set, **values)
obj._update_sync = update_sync or UpdateSynchronization()
return obj

Expand Down
6 changes: 3 additions & 3 deletions src/uiprotect/data/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
if updated_obj is None:
return None

old_obj = updated_obj.copy()
old_obj = updated_obj.model_copy()
updated_data = {to_snake_case(k): v for k, v in data.items()}
updated_obj.update_from_dict(updated_data)

Expand Down Expand Up @@ -461,7 +461,7 @@
if not (data := self.nvr.unifi_dict_to_dict(data)):
return None

old_nvr = self.nvr.copy()
old_nvr = self.nvr.model_copy()

Check warning on line 464 in src/uiprotect/data/bootstrap.py

View check run for this annotation

Codecov / codecov/patch

src/uiprotect/data/bootstrap.py#L464

Added line #L464 was not covered by tests
self.nvr = self.nvr.update_from_dict(data)

return WSSubscriptionMessage(
Expand Down Expand Up @@ -517,7 +517,7 @@
# nothing left to process
return None

old_obj = obj.copy()
old_obj = obj.model_copy()
obj = obj.update_from_dict(data)

if model_type is ModelType.EVENT:
Expand Down
2 changes: 1 addition & 1 deletion src/uiprotect/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@
def log_event(event: Event) -> None:
from uiprotect.data import EventType

_LOGGER.debug("event WS msg: %s", event.dict())
_LOGGER.debug("event WS msg: %s", event.model_dump())

Check warning on line 575 in src/uiprotect/utils.py

View check run for this annotation

Codecov / codecov/patch

src/uiprotect/utils.py#L575

Added line #L575 was not covered by tests
if "smart" not in event.type.value:
return

Expand Down
Loading