Skip to content

Commit 93b3fa8

Browse files
committed
chore: fix Pydantic warning
PydanticDeprecatedSince211: Accessing this attribute on the instance is deprecated, and will be removed in Pydantic V3. Instead, you should access this attribute from the model class. Deprecated in Pydantic V2.11 to be removed in V3.0.
1 parent 49efa99 commit 93b3fa8

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

scim2_server/utils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import re
55
import sys
6+
from inspect import isclass
67
from typing import Any
78

89
from pydantic import EmailStr
@@ -71,7 +72,9 @@ def merge_resources(target: Resource, updates: BaseModel):
7172
setattr(target, set_attribute, new_value)
7273

7374

74-
def get_by_alias(r: BaseModel, scim_name: str, allow_none: bool = False) -> str | None:
75+
def get_by_alias(
76+
resource: BaseModel, scim_name: str, allow_none: bool = False
77+
) -> str | None:
7578
"""Return the pydantic attribute name for a BaseModel and given SCIM attribute name.
7679
7780
:param r: BaseModel
@@ -81,16 +84,17 @@ def get_by_alias(r: BaseModel, scim_name: str, allow_none: bool = False) -> str
8184
:raises SCIMException: If no attribute is found and allow_none is
8285
False
8386
"""
87+
klass = resource.__class__ if not isclass(resource) else resource
8488
try:
8589
return next(
86-
k
87-
for k, v in r.model_fields.items()
88-
if v.serialization_alias.lower() == scim_name.lower()
90+
key
91+
for key, value in klass.model_fields.items()
92+
if value.serialization_alias.lower() == scim_name.lower()
8993
)
90-
except StopIteration as e:
94+
except StopIteration as exc:
9195
if allow_none:
9296
return None
93-
raise SCIMException(Error.make_no_target_error()) from e
97+
raise SCIMException(Error.make_no_target_error()) from exc
9498

9599

96100
def get_schemas(resource: Resource) -> list[str]:
@@ -99,7 +103,7 @@ def get_schemas(resource: Resource) -> list[str]:
99103
Note that this may include schemas the resource does not currently
100104
have (such as missing optional schema extensions).
101105
"""
102-
return resource.model_fields["schemas"].default
106+
return resource.__class__.model_fields["schemas"].default
103107

104108

105109
def get_or_create(

0 commit comments

Comments
 (0)