Skip to content

Commit 4c0fedb

Browse files
committed
chore: use 'Any' type instead of 'any'
1 parent 40be5ec commit 4c0fedb

2 files changed

Lines changed: 27 additions & 37 deletions

File tree

scim2_server/operators.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import re
2-
from typing import Dict
3-
from typing import List
4-
from typing import Optional
5-
from typing import Tuple
6-
from typing import Type
2+
from typing import Any
73

84
from scim2_filter_parser.lexer import SCIMLexer
95
from scim2_filter_parser.parser import SCIMParser
@@ -43,7 +39,7 @@ def patch_resource(resource: Resource, operation: PatchOperation):
4339
operator(resource)
4440

4541

46-
def parse_attribute_path(attribute_path: Optional[str]) -> Optional[Dict[str, any]]:
42+
def parse_attribute_path(attribute_path: str | None) -> dict[str, Any] | None:
4743
"""Parses an attribute path and returns a dictionary of attributes.
4844
4945
The attributes are the named captures in the regex
@@ -72,13 +68,13 @@ class Operator:
7268
REQUIRES_VALUE = True # Whether the operator modifies the resource or not
7369
RETURNS_VALUE = False # Whether the operator returns a result or not
7470

75-
def __init__(self, path: Optional[str], value: Optional[any]):
71+
def __init__(self, path: str | None, value: Any | None):
7672
self.path = path
7773
self.value = value
7874

7975
@classmethod
8076
def init_return(
81-
cls, model: BaseModel, attribute: str, sub_attribute: Optional[str], value: any
77+
cls, model: BaseModel, attribute: str, sub_attribute: str | None, value: Any
8278
):
8379
"""Initializes the return value if the operator returns something."""
8480
pass
@@ -89,7 +85,7 @@ def do_return(self):
8985

9086
@classmethod
9187
def operation(
92-
cls, model: BaseModel, attribute: str, value: any, index: Optional[int] = None
88+
cls, model: BaseModel, attribute: str, value: Any, index: int | None = None
9389
):
9490
"""Performs the actual operation of the operator."""
9591
raise NotImplementedError
@@ -224,7 +220,7 @@ class AddOperator(Operator):
224220
"""The implementation for the PATCH "add" operator."""
225221

226222
@classmethod
227-
def operation(cls, model: BaseModel, attribute: str, value: any):
223+
def operation(cls, model: BaseModel, attribute: str, value: Any):
228224
alias = get_by_alias(model, attribute)
229225
if is_multi_valued(model, alias) and isinstance(value, list):
230226
for v in value:
@@ -262,7 +258,7 @@ class RemoveOperator(Operator):
262258
REQUIRES_VALUE = False
263259

264260
@classmethod
265-
def operation(cls, model: BaseModel, attribute: str, value: any):
261+
def operation(cls, model: BaseModel, attribute: str, value: Any):
266262
alias = get_by_alias(model, attribute)
267263
existing_value = getattr(model, alias)
268264
if not existing_value:
@@ -284,7 +280,7 @@ class ReplaceOperator(Operator):
284280
"""The implementation for the PATCH "replace" operator."""
285281

286282
@classmethod
287-
def operation(cls, model: BaseModel, attribute: str, value: any):
283+
def operation(cls, model: BaseModel, attribute: str, value: Any):
288284
alias = get_by_alias(model, attribute)
289285
if is_multi_valued(model, alias) and not isinstance(value, list):
290286
raise SCIMException(Error.make_invalid_value_error())
@@ -328,13 +324,13 @@ def add_result_index(self, model: BaseModel, attribute_name: str, index: int):
328324
"""
329325
self.records.append((model, attribute_name, index))
330326

331-
def _evaluate_result(self, record: Tuple[str, str] | Tuple[str, str, int]):
327+
def _evaluate_result(self, record: tuple[str, str] | tuple[str, str, int]):
332328
if len(record) == 2:
333329
return getattr(*record)
334330
else:
335331
return getattr(record[0], record[1])[record[2]]
336332

337-
def get_field_annotation(self, annotation_type: Type):
333+
def get_field_annotation(self, annotation_type: type):
338334
if not self.model:
339335
return None
340336
return self.model.get_field_annotation(self.attribute, annotation_type)
@@ -353,7 +349,7 @@ class ResolveOperator(Operator):
353349
REQUIRES_VALUE = False
354350
RETURNS_VALUE = True
355351

356-
def __init__(self, path: Optional[str]):
352+
def __init__(self, path: str | None):
357353
super().__init__(path, ResolveResult())
358354

359355
def do_return(self):
@@ -366,7 +362,7 @@ def init_return(
366362
cls,
367363
model: BaseModel,
368364
attribute: str,
369-
sub_attribute: Optional[str],
365+
sub_attribute: str | None,
370366
value: ResolveResult,
371367
):
372368
alias = get_by_alias(model, attribute)
@@ -381,7 +377,7 @@ def init_return(
381377

382378
@classmethod
383379
def operation(
384-
cls, model: BaseModel, attribute: str, value: any, index: Optional[int] = None
380+
cls, model: BaseModel, attribute: str, value: Any, index: int | None = None
385381
):
386382
alias = get_by_alias(model, attribute)
387383
if index is None:
@@ -405,17 +401,17 @@ class ResolveSortOperator(ResolveOperator):
405401
is not set.
406402
"""
407403

408-
def __init__(self, path: Optional[str]):
404+
def __init__(self, path: str | None):
409405
super().__init__(path)
410406

411-
def alias_forbidden(self, model: BaseModel, alias: Optional[str]) -> bool:
407+
def alias_forbidden(self, model: BaseModel, alias: str | None) -> bool:
412408
return (
413409
not alias
414410
or model.get_field_annotation(alias, Mutability) == Mutability.write_only
415411
or model.get_field_annotation(alias, Returned) == Returned.never
416412
)
417413

418-
def set_value_case_exact(self, value: any, case_exact: CaseExact):
414+
def set_value_case_exact(self, value: Any, case_exact: CaseExact):
419415
if isinstance(value, str) and case_exact == CaseExact.false:
420416
value = value.lower()
421417
self.value = value
@@ -467,7 +463,7 @@ def __call__(self, model: BaseModel):
467463
self.set_value_case_exact(attribute_value, case_exact)
468464
return self.value
469465

470-
def select_candidate(self, values: List[any]) -> Tuple[Optional[any], int]:
466+
def select_candidate(self, values: list[Any]) -> tuple[Any | None, int]:
471467
"""Selects a viable candidate from a list of possible values."""
472468
for value in values:
473469
primary = getattr(value, "primary", False)

scim2_server/utils.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import datetime
22
import importlib
33
import json
4-
from typing import Dict
5-
from typing import List
6-
from typing import Optional
7-
from typing import Tuple
8-
from typing import Type
4+
from typing import Any
95
from typing import Union
106
from typing import get_args
117
from typing import get_origin
@@ -29,7 +25,7 @@ def __init__(self, scim_error: Error):
2925
self.scim_error = scim_error
3026

3127

32-
def load_json_resource(json_name: str) -> List:
28+
def load_json_resource(json_name: str) -> list:
3329
"""Loads a JSON document from the scim2_server package resources."""
3430
fp = importlib.resources.files("scim2_server") / "resources" / json_name
3531
with open(fp) as f:
@@ -47,12 +43,12 @@ def load_scim_resource(json_name: str, type_: type[Resource]):
4743
return ret
4844

4945

50-
def load_default_schemas() -> Dict[str, Schema]:
46+
def load_default_schemas() -> dict[str, Schema]:
5147
"""Loads the default schemas from RFC 7643."""
5248
return load_scim_resource("default-schemas.json", Schema)
5349

5450

55-
def load_default_resource_types() -> Dict[str, ResourceType]:
51+
def load_default_resource_types() -> dict[str, ResourceType]:
5652
"""Loads the default resource types from RFC 7643."""
5753
return load_scim_resource("default-resource-types.json", ResourceType)
5854

@@ -79,9 +75,7 @@ def merge_resources(target: Resource, updates: BaseModel):
7975
setattr(target, set_attribute, new_value)
8076

8177

82-
def get_by_alias(
83-
r: BaseModel, scim_name: str, allow_none: bool = False
84-
) -> Optional[str]:
78+
def get_by_alias(r: BaseModel, scim_name: str, allow_none: bool = False) -> str | None:
8579
"""Returns the pydantic attribute name for a BaseModel and given SCIM
8680
attribute name.
8781
@@ -112,10 +106,10 @@ def is_multi_valued(model: BaseModel, attribute_name: str) -> bool:
112106
attribute_type = get_args(attribute_type)[0]
113107

114108
origin = get_origin(attribute_type)
115-
return isinstance(origin, Type) and issubclass(origin, List)
109+
return isinstance(origin, type) and issubclass(origin, list)
116110

117111

118-
def get_schemas(resource: Resource) -> List[str]:
112+
def get_schemas(resource: Resource) -> list[str]:
119113
"""Returns a list of all schemas possible for a given resource.
120114
121115
Note that this may include schemas the resource does not currently
@@ -155,7 +149,7 @@ def get_or_create(
155149
return ret
156150

157151

158-
def handle_extension(resource: Resource, scim_name: str) -> Tuple[BaseModel, str]:
152+
def handle_extension(resource: Resource, scim_name: str) -> tuple[BaseModel, str]:
159153
default_schema = get_schemas(resource)[0].lower()
160154
if scim_name.lower().startswith(default_schema):
161155
scim_name = scim_name[len(default_schema) :].lstrip(":")
@@ -174,7 +168,7 @@ def handle_extension(resource: Resource, scim_name: str) -> Tuple[BaseModel, str
174168
return resource, scim_name
175169

176170

177-
def model_validate_from_dict(field_root_type: BaseModel, value: dict) -> any:
171+
def model_validate_from_dict(field_root_type: BaseModel, value: dict) -> Any:
178172
"""Workaround for some of the "special" requirements for MS Entra, mixing
179173
display and displayName in some cases."""
180174
if (
@@ -187,7 +181,7 @@ def model_validate_from_dict(field_root_type: BaseModel, value: dict) -> any:
187181
return field_root_type.model_validate(value)
188182

189183

190-
def parse_new_value(model: BaseModel, attribute_name: str, value: any) -> any:
184+
def parse_new_value(model: BaseModel, attribute_name: str, value: Any) -> Any:
191185
"""Given a model and attribute name, attempt to parse a new value so that
192186
the type matches the type expected by the model.
193187

0 commit comments

Comments
 (0)