Skip to content

Commit 292fba7

Browse files
committed
Replace deprecated functions with new pydantic v2 functions
1 parent 38ad2c4 commit 292fba7

File tree

15 files changed

+53
-42
lines changed

15 files changed

+53
-42
lines changed

bot/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
from enum import Enum
1010

11-
from pydantic import BaseModel, root_validator
11+
from pydantic import BaseModel, model_validator
1212
from pydantic_settings import BaseSettings
1313

1414

@@ -311,7 +311,7 @@ class _Colours(EnvConfig, env_prefix="colours_"):
311311
white: int = 0xfffffe
312312
yellow: int = 0xffd241
313313

314-
@root_validator(pre=True)
314+
@model_validator(mode="before")
315315
def parse_hex_values(cls, values: dict) -> dict: # noqa: N805
316316
"""Convert hex strings to ints."""
317317
for key, value in values.items():

bot/exts/filtering/_filter_lists/antispam.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async def actions_for(
9393
current_actions.pop("ping", None)
9494
current_actions.pop("send_alert", None)
9595

96-
new_infraction = current_actions[InfractionAndNotification.name].copy()
96+
new_infraction = current_actions[InfractionAndNotification.name].model_copy()
9797
# Smaller infraction value => higher in hierarchy.
9898
if not current_infraction or new_infraction.infraction_type.value < current_infraction.value:
9999
# Pick the first triggered filter for the reason, there's no good way to decide between them.

bot/exts/filtering/_filters/filter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, filter_data: dict, defaults: Defaults | None = None):
3131
self.updated_at = arrow.get(filter_data["updated_at"])
3232
self.actions, self.validations = create_settings(filter_data["settings"], defaults=defaults)
3333
if self.extra_fields_type:
34-
self.extra_fields = self.extra_fields_type.parse_obj(filter_data["additional_settings"])
34+
self.extra_fields = self.extra_fields_type.model_validate(filter_data["additional_settings"])
3535
else:
3636
self.extra_fields = None
3737

@@ -46,7 +46,7 @@ def overrides(self) -> tuple[dict[str, Any], dict[str, Any]]:
4646

4747
filter_settings = {}
4848
if self.extra_fields:
49-
filter_settings = self.extra_fields.dict(exclude_unset=True)
49+
filter_settings = self.extra_fields.model_dump(exclude_unset=True)
5050

5151
return settings, filter_settings
5252

bot/exts/filtering/_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,5 @@ def dict(self) -> dict[str, Any]:
227227
"""Return a dict representation of the stored fields across all entries."""
228228
dict_ = {}
229229
for settings in self:
230-
dict_ = reduce(operator.or_, (entry.dict() for entry in settings.values()), dict_)
230+
dict_ = reduce(operator.or_, (entry.model_dump() for entry in settings.values()), dict_)
231231
return dict_

bot/exts/filtering/_settings_types/actions/infraction_and_notification.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dateutil.relativedelta import relativedelta
77
from discord import Colour, Embed, Member, User
88
from discord.errors import Forbidden
9-
from pydantic import validator
9+
from pydantic import field_validator
1010
from pydis_core.utils.logging import get_logger
1111
from pydis_core.utils.members import get_or_fetch_member
1212

@@ -151,7 +151,7 @@ class InfractionAndNotification(ActionEntry):
151151
infraction_duration: InfractionDuration
152152
infraction_channel: int
153153

154-
@validator("infraction_type", pre=True)
154+
@field_validator("infraction_type", mode="before")
155155
@classmethod
156156
def convert_infraction_name(cls, infr_type: str | Infraction) -> Infraction:
157157
"""Convert the string to an Infraction by name."""
@@ -221,24 +221,24 @@ def union(self, other: Self) -> Self:
221221
"""
222222
# Lower number -> higher in the hierarchy
223223
if self.infraction_type is None:
224-
return other.copy()
224+
return other.model_copy()
225225
if other.infraction_type is None:
226-
return self.copy()
226+
return self.model_copy()
227227

228228
if self.infraction_type.value < other.infraction_type.value:
229-
result = self.copy()
229+
result = self.model_copy()
230230
elif self.infraction_type.value > other.infraction_type.value:
231-
result = other.copy()
231+
result = other.model_copy()
232232
other = self
233233
else:
234234
now = arrow.utcnow().datetime
235235
if self.infraction_duration is None or (
236236
other.infraction_duration is not None
237237
and now + self.infraction_duration.value > now + other.infraction_duration.value
238238
):
239-
result = self.copy()
239+
result = self.model_copy()
240240
else:
241-
result = other.copy()
241+
result = other.model_copy()
242242
other = self
243243

244244
# If the winner has no message but the loser does, copy the message to the winner.

bot/exts/filtering/_settings_types/actions/ping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import ClassVar, Self
22

3-
from pydantic import validator
3+
from pydantic import field_validator
44

55
from bot.exts.filtering._filter_context import FilterContext
66
from bot.exts.filtering._settings_types.settings_entry import ActionEntry
@@ -25,7 +25,7 @@ class Ping(ActionEntry):
2525
guild_pings: set[str]
2626
dm_pings: set[str]
2727

28-
@validator("*", pre=True)
28+
@field_validator("*", mode="before")
2929
@classmethod
3030
def init_sequence_if_none(cls, pings: list[str] | None) -> list[str]:
3131
"""Initialize an empty sequence if the value is None."""

bot/exts/filtering/_settings_types/settings_entry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SettingsEntry(BaseModel, FieldRequiring):
2828
def __init__(self, defaults: SettingsEntry | None = None, /, **data):
2929
overrides = set()
3030
if defaults:
31-
defaults_dict = defaults.dict()
31+
defaults_dict = defaults.model_dump()
3232
for field_name, field_value in list(data.items()):
3333
if field_value is None:
3434
data[field_name] = defaults_dict[field_name]

bot/exts/filtering/_settings_types/validations/channel_scope.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import ClassVar, Union
22

3-
from pydantic import validator
3+
from pydantic import field_validator
44

55
from bot.exts.filtering._filter_context import FilterContext
66
from bot.exts.filtering._settings_types.settings_entry import ValidationEntry
@@ -36,7 +36,7 @@ class ChannelScope(ValidationEntry):
3636
enabled_channels: set[Union[int, str]] # noqa: UP007
3737
enabled_categories: set[Union[int, str]] # noqa: UP007
3838

39-
@validator("*", pre=True)
39+
@field_validator("*", mode="before")
4040
@classmethod
4141
def init_if_sequence_none(cls, sequence: list[str] | None) -> list[str]:
4242
"""Initialize an empty sequence if the value is None."""

bot/exts/filtering/_ui/filter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def build_filter_repr_dict(
3434
default_setting_values = {}
3535
for settings_group in filter_list[list_type].defaults:
3636
for _, setting in settings_group.items():
37-
default_setting_values.update(to_serializable(setting.dict(), ui_repr=True))
37+
default_setting_values.update(to_serializable(setting.model_dump(), ui_repr=True))
3838

3939
# Add overrides. It's done in this way to preserve field order, since the filter won't have all settings.
4040
total_values = {}
@@ -47,7 +47,7 @@ def build_filter_repr_dict(
4747
# Add the filter-specific settings.
4848
if filter_type.extra_fields_type:
4949
# This iterates over the default values of the extra fields model.
50-
for name, value in filter_type.extra_fields_type().dict().items():
50+
for name, value in filter_type.extra_fields_type().model_dump().items():
5151
if name not in extra_fields_overrides or repr_equals(extra_fields_overrides[name], value):
5252
total_values[f"{filter_type.name}/{name}"] = value
5353
else:
@@ -287,7 +287,7 @@ async def update_embed(
287287
if "/" in setting_name:
288288
filter_name, setting_name = setting_name.split("/", maxsplit=1)
289289
dict_to_edit = self.filter_settings_overrides
290-
default_value = self.filter_type.extra_fields_type().dict()[setting_name]
290+
default_value = self.filter_type.extra_fields_type().model_dump()[setting_name]
291291
else:
292292
dict_to_edit = self.settings_overrides
293293
default_value = self.filter_list[self.list_type].default(setting_name)

bot/exts/filtering/_ui/filter_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def build_filterlist_repr_dict(filter_list: FilterList, list_type: ListType, new
5050
default_setting_values = {}
5151
for settings_group in filter_list[list_type].defaults:
5252
for _, setting in settings_group.items():
53-
default_setting_values.update(to_serializable(setting.dict(), ui_repr=True))
53+
default_setting_values.update(to_serializable(setting.model_dump(), ui_repr=True))
5454

5555
# Add new values. It's done in this way to preserve field order, since the new_values won't have all settings.
5656
total_values = {}

0 commit comments

Comments
 (0)