44import pickle
55import uuid
66from threading import Lock
7- from typing import Dict
8- from typing import List
9- from typing import Optional
10- from typing import Tuple
117from typing import Union
128
139from scim2_filter_parser import lexer
@@ -35,10 +31,10 @@ class Backend:
3531 """The base class for a SCIM provider backend."""
3632
3733 def __init__ (self ):
38- self .schemas : Dict [str , Schema ] = {}
39- self .resource_types : Dict [str , ResourceType ] = {}
40- self .resource_types_by_endpoint : Dict [str , ResourceType ] = {}
41- self .models_dict : Dict [str , BaseModel ] = {}
34+ self .schemas : dict [str , Schema ] = {}
35+ self .resource_types : dict [str , ResourceType ] = {}
36+ self .resource_types_by_endpoint : dict [str , ResourceType ] = {}
37+ self .models_dict : dict [str , BaseModel ] = {}
4238
4339 def __enter__ (self ):
4440 """Allows the backend to be used as a context manager.
@@ -59,7 +55,7 @@ def get_schemas(self):
5955 """Returns all schemas registered with the backend."""
6056 return self .schemas .values ()
6157
62- def get_schema (self , schema_id : str ) -> Optional [ Schema ] :
58+ def get_schema (self , schema_id : str ) -> Schema | None :
6359 """Gets a schema by its id."""
6460 return self .schemas .get (schema_id )
6561
@@ -93,15 +89,15 @@ def get_resource_types(self):
9389 """Returns all resource types registered with the backend."""
9490 return self .resource_types .values ()
9591
96- def get_resource_type (self , resource_type_id : str ) -> Optional [ ResourceType ] :
92+ def get_resource_type (self , resource_type_id : str ) -> ResourceType | None :
9793 """Returns the resource type by its id."""
9894 return self .resource_types .get (resource_type_id )
9995
100- def get_resource_type_by_endpoint (self , endpoint : str ) -> Optional [ ResourceType ] :
96+ def get_resource_type_by_endpoint (self , endpoint : str ) -> ResourceType | None :
10197 """Returns the resource type by its endpoint."""
10298 return self .resource_types_by_endpoint .get (endpoint .lower ())
10399
104- def get_model (self , resource_type_id : str ) -> Optional [ BaseModel ] :
100+ def get_model (self , resource_type_id : str ) -> BaseModel | None :
105101 """Returns the Pydantic Python model for a given resource type."""
106102 return self .models_dict .get (resource_type_id )
107103
@@ -112,8 +108,8 @@ def get_models(self):
112108 def query_resources (
113109 self ,
114110 search_request : SearchRequest ,
115- resource_type_id : Optional [ str ] = None ,
116- ) -> Tuple [int , List [Resource ]]:
111+ resource_type_id : str | None = None ,
112+ ) -> tuple [int , list [Resource ]]:
117113 """Queries the backend for a set of resources.
118114
119115 :param search_request: SearchRequest instance describing the
@@ -131,7 +127,7 @@ def query_resources(
131127 """
132128 raise NotImplementedError
133129
134- def get_resource (self , resource_type_id : str , object_id : str ) -> Optional [ Resource ] :
130+ def get_resource (self , resource_type_id : str , object_id : str ) -> Resource | None :
135131 """Queries the backend for a resources by its ID.
136132
137133 :param resource_type_id: ID of the resource type to get the
@@ -155,7 +151,7 @@ def delete_resource(self, resource_type_id: str, object_id: str) -> bool:
155151
156152 def create_resource (
157153 self , resource_type_id : str , resource : Resource
158- ) -> Optional [ Resource ] :
154+ ) -> Resource | None :
159155 """Creates a resource.
160156
161157 :param resource_type_id: ID of the resource type to create.
@@ -168,7 +164,7 @@ def create_resource(
168164
169165 def update_resource (
170166 self , resource_type_id : str , resource : Resource
171- ) -> Optional [ Resource ] :
167+ ) -> Resource | None :
172168 """Updates a resource. The resource is identified by its ID.
173169
174170 :param resource_type_id: ID of the resource type to update.
@@ -193,7 +189,7 @@ class InMemoryBackend(Backend):
193189 class UniquenessDescriptor :
194190 """Used to mimic uniqueness constraints e.g. from a SQL database."""
195191
196- schema : Optional [ str ]
192+ schema : str | None
197193 attribute_name : str
198194 case_exact : bool
199195
@@ -207,8 +203,8 @@ def get_attribute(self, resource: Resource):
207203
208204 @classmethod
209205 def collect_unique_attrs (
210- cls , attributes : List [Attribute ], schema : Optional [ str ]
211- ) -> List [UniquenessDescriptor ]:
206+ cls , attributes : list [Attribute ], schema : str | None
207+ ) -> list [UniquenessDescriptor ]:
212208 ret = []
213209 for attr in attributes :
214210 if attr .uniqueness != Uniqueness .none :
@@ -221,8 +217,8 @@ def collect_unique_attrs(
221217
222218 @classmethod
223219 def collect_resource_unique_attrs (
224- cls , resource_type : ResourceType , schemas : Dict [str , Schema ]
225- ) -> List [ List [UniquenessDescriptor ]]:
220+ cls , resource_type : ResourceType , schemas : dict [str , Schema ]
221+ ) -> list [ list [UniquenessDescriptor ]]:
226222 ret = cls .collect_unique_attrs (schemas [resource_type .schema_ ].attributes , None )
227223 for extension in resource_type .schema_extensions or []:
228224 ret .extend (
@@ -234,8 +230,8 @@ def collect_resource_unique_attrs(
234230
235231 def __init__ (self ):
236232 super ().__init__ ()
237- self .resources : List [Resource ] = []
238- self .unique_attributes : Dict [str , List [ List [str ]]] = {}
233+ self .resources : list [Resource ] = []
234+ self .unique_attributes : dict [str , list [ list [str ]]] = {}
239235 self .lock : Lock = Lock ()
240236
241237 def __enter__ (self ):
@@ -261,8 +257,8 @@ def register_resource_type(self, resource_type: ResourceType):
261257 def query_resources (
262258 self ,
263259 search_request : SearchRequest ,
264- resource_type_id : Optional [ str ] = None ,
265- ) -> Tuple [int , List [Resource ]]:
260+ resource_type_id : str | None = None ,
261+ ) -> tuple [int , list [Resource ]]:
266262 start_index = (search_request .start_index or 1 ) - 1
267263
268264 tree = None
@@ -304,7 +300,7 @@ def query_resources(
304300 found_resources = found_resources [: search_request .count ]
305301 return len (found_resources ), found_resources
306302
307- def _get_resource_idx (self , resource_type_id : str , object_id : str ) -> Optional [ int ] :
303+ def _get_resource_idx (self , resource_type_id : str , object_id : str ) -> int | None :
308304 return next (
309305 (
310306 idx
@@ -314,7 +310,7 @@ def _get_resource_idx(self, resource_type_id: str, object_id: str) -> Optional[i
314310 None ,
315311 )
316312
317- def get_resource (self , resource_type_id : str , object_id : str ) -> Optional [ Resource ] :
313+ def get_resource (self , resource_type_id : str , object_id : str ) -> Resource | None :
318314 resource_dict_idx = self ._get_resource_idx (resource_type_id , object_id )
319315 if resource_dict_idx is not None :
320316 return self .resources [resource_dict_idx ].model_copy (deep = True )
@@ -333,7 +329,7 @@ def delete_resource(self, resource_type_id: str, object_id: str) -> bool:
333329
334330 def create_resource (
335331 self , resource_type_id : str , resource : Resource
336- ) -> Optional [ Resource ] :
332+ ) -> Resource | None :
337333 resource = resource .model_copy (deep = True )
338334 resource .id = uuid .uuid4 ().hex
339335 utcnow = datetime .datetime .now (datetime .UTC )
@@ -372,7 +368,7 @@ def _touch_resource(resource: Resource, last_modified: datetime.datetime):
372368
373369 def update_resource (
374370 self , resource_type_id : str , resource : Resource
375- ) -> Optional [ Resource ] :
371+ ) -> Resource | None :
376372 found_res_idx = self ._get_resource_idx (resource_type_id , resource .id )
377373 if found_res_idx is not None :
378374 updated_resource = self .models_dict [resource_type_id ].model_validate (
0 commit comments