Skip to content

Commit c1db105

Browse files
committed
chore: ruff migrations
1 parent 0a7d451 commit c1db105

19 files changed

Lines changed: 76 additions & 103 deletions

scim2_models/base.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
from inspect import isclass
55
from typing import Annotated
66
from typing import Any
7-
from typing import Dict
87
from typing import Generic
9-
from typing import List
108
from typing import Optional
11-
from typing import Tuple
12-
from typing import Type
139
from typing import TypeVar
1410
from typing import Union
1511
from typing import get_args
@@ -41,7 +37,7 @@
4137
ExternalReference = NewType("ExternalReference", str)
4238

4339

44-
def validate_model_attribute(model: Type, attribute_base: str) -> None:
40+
def validate_model_attribute(model: type["BaseModel"], attribute_base: str) -> None:
4541
"""Validate that an attribute name or a sub-attribute path exist for a
4642
given model."""
4743

@@ -68,7 +64,7 @@ def validate_model_attribute(model: Type, attribute_base: str) -> None:
6864
validate_model_attribute(attribute_type, sub_attribute_base)
6965

7066

71-
def extract_schema_and_attribute_base(attribute_urn: str) -> Tuple[str, str]:
67+
def extract_schema_and_attribute_base(attribute_urn: str) -> tuple[str, str]:
7268
# Extract the schema urn part and the attribute name part from attribute
7369
# name, as defined in :rfc:`RFC7644 §3.10 <7644#section-3.10>`.
7470

@@ -79,8 +75,8 @@ def extract_schema_and_attribute_base(attribute_urn: str) -> Tuple[str, str]:
7975

8076
def validate_attribute_urn(
8177
attribute_name: str,
82-
default_resource: Optional[Type] = None,
83-
resource_types: Optional[List[Type]] = None,
78+
default_resource: Optional[type["BaseModel"]] = None,
79+
resource_types: Optional[list[type["BaseModel"]]] = None,
8480
) -> str:
8581
"""Validate that an attribute urn is valid or not.
8682
@@ -120,7 +116,7 @@ def validate_attribute_urn(
120116
return f"{schema}:{attribute_base}"
121117

122118

123-
def contains_attribute_or_subattributes(attribute_urns: List[str], attribute_urn: str):
119+
def contains_attribute_or_subattributes(attribute_urns: list[str], attribute_urn: str):
124120
return attribute_urn in attribute_urns or any(
125121
item.startswith(f"{attribute_urn}.") or item.startswith(f"{attribute_urn}:")
126122
for item in attribute_urns
@@ -424,7 +420,7 @@ class BaseModel(PydanticBaseModel):
424420
)
425421

426422
@classmethod
427-
def get_field_annotation(cls, field_name: str, annotation_type: Type) -> Any:
423+
def get_field_annotation(cls, field_name: str, annotation_type: type) -> Any:
428424
"""Return the annotation of type 'annotation_type' of the field
429425
'field_name'."""
430426
field_metadata = cls.model_fields[field_name].metadata
@@ -440,7 +436,7 @@ def annotation_type_filter(item):
440436
return field_annotation
441437

442438
@classmethod
443-
def get_field_root_type(cls, attribute_name: str) -> Type:
439+
def get_field_root_type(cls, attribute_name: str) -> type:
444440
"""Extract the root type from a model field.
445441
446442
For example, return 'GroupMember' for
@@ -455,7 +451,7 @@ def get_field_root_type(cls, attribute_name: str) -> Type:
455451

456452
# extract 'x' from 'List[x]'
457453
if isclass(get_origin(attribute_type)) and issubclass(
458-
get_origin(attribute_type), List
454+
get_origin(attribute_type), list
459455
):
460456
attribute_type = get_args(attribute_type)[0]
461457

@@ -727,7 +723,7 @@ def scim_response_serializer(self, value: Any, info: SerializationInfo) -> Any:
727723
@model_serializer(mode="wrap")
728724
def model_serializer_exclude_none(
729725
self, handler, info: SerializationInfo
730-
) -> Dict[str, Any]:
726+
) -> dict[str, Any]:
731727
"""Remove `None` values inserted by the
732728
:meth:`~scim2_models.base.BaseModel.scim_serializer`."""
733729

@@ -749,8 +745,8 @@ def model_dump(
749745
self,
750746
*args,
751747
scim_ctx: Optional[Context] = Context.DEFAULT,
752-
attributes: Optional[List[str]] = None,
753-
excluded_attributes: Optional[List[str]] = None,
748+
attributes: Optional[list[str]] = None,
749+
excluded_attributes: Optional[list[str]] = None,
754750
**kwargs,
755751
):
756752
"""Create a model representation that can be included in SCIM messages
@@ -834,4 +830,4 @@ def is_complex_attribute(type) -> bool:
834830
)
835831

836832

837-
BaseModelType: Type = type(BaseModel)
833+
BaseModelType: type = type(BaseModel)

scim2_models/rfc7643/enterprise_user.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Annotated
2-
from typing import List
32
from typing import Literal
43
from typing import Optional
54

@@ -27,7 +26,7 @@ class Manager(ComplexAttribute):
2726

2827

2928
class EnterpriseUser(Extension):
30-
schemas: List[str] = ["urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"]
29+
schemas: list[str] = ["urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"]
3130

3231
employee_number: Optional[str] = None
3332
"""Numeric or alphanumeric identifier assigned to a person, typically based

scim2_models/rfc7643/group.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Annotated
2-
from typing import List
32
from typing import Literal
43
from typing import Optional
54
from typing import Union
@@ -32,10 +31,10 @@ class GroupMember(MultiValuedComplexAttribute):
3231

3332

3433
class Group(Resource):
35-
schemas: List[str] = ["urn:ietf:params:scim:schemas:core:2.0:Group"]
34+
schemas: list[str] = ["urn:ietf:params:scim:schemas:core:2.0:Group"]
3635

3736
display_name: Optional[str] = None
3837
"""A human-readable name for the Group."""
3938

40-
members: Optional[List[GroupMember]] = None
39+
members: Optional[list[GroupMember]] = None
4140
"""A list of members of the Group."""

scim2_models/rfc7643/resource.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from datetime import datetime
22
from typing import Annotated
33
from typing import Any
4-
from typing import Dict
54
from typing import Generic
6-
from typing import List
75
from typing import Optional
8-
from typing import Type
96
from typing import TypeVar
107
from typing import Union
118
from typing import get_args
@@ -105,7 +102,7 @@ def from_schema(cls, schema) -> "Extension":
105102
AnyExtension = TypeVar("AnyExtension", bound="Extension")
106103

107104

108-
def extension_serializer(value: Any, handler, info) -> Optional[Dict[str, Any]]:
105+
def extension_serializer(value: Any, handler, info) -> Optional[dict[str, Any]]:
109106
"""Exclude the Resource attributes from the extension dump.
110107
111108
For instance, attributes 'meta', 'id' or 'schemas' should not be
@@ -149,7 +146,7 @@ def __new__(cls, name, bases, attrs, **kwargs):
149146

150147

151148
class Resource(BaseModel, Generic[AnyExtension], metaclass=ResourceMetaclass):
152-
schemas: List[str]
149+
schemas: list[str]
153150
"""The "schemas" attribute is a REQUIRED attribute and is an array of
154151
Strings containing URIs that are used to indicate the namespaces of the
155152
SCIM schemas that define the attributes present in the current JSON
@@ -190,7 +187,7 @@ def __setitem__(self, item: Any, value: "Resource"):
190187
setattr(self, item.__name__, value)
191188

192189
@classmethod
193-
def get_extension_models(cls) -> Dict[str, Type]:
190+
def get_extension_models(cls) -> dict[str, type]:
194191
"""Return extension a dict associating extension models with their
195192
schemas."""
196193

@@ -208,8 +205,8 @@ def get_extension_models(cls) -> Dict[str, Type]:
208205

209206
@staticmethod
210207
def get_by_schema(
211-
resource_types: List[Type], schema: str, with_extensions=True
212-
) -> Optional[Type]:
208+
resource_types: list[type[BaseModel]], schema: str, with_extensions=True
209+
) -> Optional[type]:
213210
"""Given a resource type list and a schema, find the matching resource
214211
type."""
215212

@@ -229,7 +226,7 @@ def get_by_schema(
229226
return by_schema.get(schema.lower())
230227

231228
@staticmethod
232-
def get_by_payload(resource_types: List[Type], payload: Dict, **kwargs):
229+
def get_by_payload(resource_types: list[type], payload: dict, **kwargs):
233230
"""Given a resource type list and a payload, find the matching resource
234231
type."""
235232

@@ -240,7 +237,7 @@ def get_by_payload(resource_types: List[Type], payload: Dict, **kwargs):
240237
return Resource.get_by_schema(resource_types, schema, **kwargs)
241238

242239
@field_serializer("schemas")
243-
def set_extension_schemas(self, schemas: List[str]):
240+
def set_extension_schemas(self, schemas: list[str]):
244241
"""Add model extension ids to the 'schemas' attribute."""
245242

246243
extension_schemas = self.get_extension_models().keys()
@@ -299,7 +296,7 @@ def compare_field_infos(fi1, fi2):
299296
return field_infos
300297

301298

302-
def model_to_schema(model: Type):
299+
def model_to_schema(model: type[BaseModel]):
303300
from scim2_models.rfc7643.schema import Schema
304301

305302
schema_urn = model.model_fields["schemas"].default[0]
@@ -318,7 +315,7 @@ def model_to_schema(model: Type):
318315
return schema
319316

320317

321-
def get_reference_types(type) -> List[str]:
318+
def get_reference_types(type) -> list[str]:
322319
first_arg = get_args(type)[0]
323320
types = get_args(first_arg) if get_origin(first_arg) == Union else [first_arg]
324321

scim2_models/rfc7643/resource_type.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Annotated
2-
from typing import List
32
from typing import Optional
43

54
from pydantic import Field
@@ -35,7 +34,7 @@ class SchemaExtension(ComplexAttribute):
3534

3635

3736
class ResourceType(Resource):
38-
schemas: List[str] = ["urn:ietf:params:scim:schemas:core:2.0:ResourceType"]
37+
schemas: list[str] = ["urn:ietf:params:scim:schemas:core:2.0:ResourceType"]
3938

4039
name: Annotated[Optional[str], Mutability.read_only, Required.true] = None
4140
"""The resource type name.
@@ -71,6 +70,6 @@ class ResourceType(Resource):
7170
"""The resource type's primary/base schema URI."""
7271

7372
schema_extensions: Annotated[
74-
Optional[List[SchemaExtension]], Mutability.read_only, Required.true
73+
Optional[list[SchemaExtension]], Mutability.read_only, Required.true
7574
] = None
7675
"""A list of URIs of the resource type's schema extensions."""

scim2_models/rfc7643/schema.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
from enum import Enum
44
from typing import Annotated
55
from typing import Any
6-
from typing import List
76
from typing import Literal
87
from typing import Optional
9-
from typing import Tuple
10-
from typing import Type
118
from typing import Union
129
from typing import get_origin
1310

@@ -47,7 +44,7 @@ def make_python_identifier(identifier: str) -> str:
4744

4845

4946
def make_python_model(
50-
obj: Union["Schema", "Attribute"], base: Optional[Type] = None, multiple=False
47+
obj: Union["Schema", "Attribute"], base: Optional[type] = None, multiple=False
5148
) -> "Resource":
5249
"""Build a Python model from a Schema or an Attribute object."""
5350

@@ -66,7 +63,7 @@ def make_python_model(
6663
if attr.name
6764
}
6865
pydantic_attributes["schemas"] = (
69-
Optional[List[str]],
66+
Optional[list[str]],
7067
Field(default=[obj.id]),
7168
)
7269

@@ -97,8 +94,8 @@ class Type(str, Enum):
9794
def to_python(
9895
self,
9996
multiple=False,
100-
reference_types: Optional[List[str]] = None,
101-
) -> Type:
97+
reference_types: Optional[list[str]] = None,
98+
) -> type:
10299
if self.value == self.reference and reference_types is not None:
103100
if reference_types == ["external"]:
104101
return Reference[ExternalReference]
@@ -166,7 +163,7 @@ def from_python(cls, pytype) -> str:
166163
required."""
167164

168165
canonical_values: Annotated[
169-
Optional[List[str]], Mutability.read_only, CaseExact.true
166+
Optional[list[str]], Mutability.read_only, CaseExact.true
170167
] = None
171168
"""A collection of suggested canonical values that MAY be used (e.g.,
172169
"work" and "home")."""
@@ -197,16 +194,16 @@ def from_python(cls, pytype) -> str:
197194
uniqueness of attribute values."""
198195

199196
reference_types: Annotated[
200-
Optional[List[str]], Mutability.read_only, Required.false, CaseExact.true
197+
Optional[list[str]], Mutability.read_only, Required.false, CaseExact.true
201198
] = None
202199
"""A multi-valued array of JSON strings that indicate the SCIM resource
203200
types that may be referenced."""
204201

205-
sub_attributes: Annotated[Optional[List["Attribute"]], Mutability.read_only] = None
202+
sub_attributes: Annotated[Optional[list["Attribute"]], Mutability.read_only] = None
206203
"""When an attribute is of type "complex", "subAttributes" defines a set of
207204
sub-attributes."""
208205

209-
def to_python(self) -> Optional[Tuple[Any, Field]]:
206+
def to_python(self) -> Optional[tuple[Any, Field]]:
210207
"""Build tuple suited to be passed to pydantic 'create_model'."""
211208

212209
if not self.name:
@@ -218,7 +215,7 @@ def to_python(self) -> Optional[Tuple[Any, Field]]:
218215
attr_type = make_python_model(obj=self, multiple=self.multi_valued)
219216

220217
if self.multi_valued:
221-
attr_type = List[attr_type] # type: ignore
218+
attr_type = list[attr_type] # type: ignore
222219

223220
annotation = Annotated[
224221
Optional[attr_type], # type: ignore
@@ -241,7 +238,7 @@ def to_python(self) -> Optional[Tuple[Any, Field]]:
241238

242239

243240
class Schema(Resource):
244-
schemas: List[str] = ["urn:ietf:params:scim:schemas:core:2.0:Schema"]
241+
schemas: list[str] = ["urn:ietf:params:scim:schemas:core:2.0:Schema"]
245242

246243
id: Annotated[Optional[str], Mutability.read_only, Required.true] = None
247244
"""The unique URI of the schema."""
@@ -255,7 +252,7 @@ class Schema(Resource):
255252
"""The schema's human-readable description."""
256253

257254
attributes: Annotated[
258-
Optional[List[Attribute]], Mutability.read_only, Required.true
255+
Optional[list[Attribute]], Mutability.read_only, Required.true
259256
] = None
260257
"""A complex type that defines service provider attributes and their
261258
qualities via the following set of sub-attributes."""

scim2_models/rfc7643/service_provider_config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from enum import Enum
22
from typing import Annotated
3-
from typing import List
43
from typing import Optional
54

65
from pydantic import Field
@@ -95,7 +94,7 @@ class Type(str, Enum):
9594

9695

9796
class ServiceProviderConfig(Resource):
98-
schemas: List[str] = ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"]
97+
schemas: list[str] = ["urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig"]
9998

10099
id: Annotated[
101100
Optional[str], Mutability.read_only, Returned.default, Uniqueness.global_
@@ -135,7 +134,7 @@ class ServiceProviderConfig(Resource):
135134
"""A complex type that specifies ETag configuration options."""
136135

137136
authentication_schemes: Annotated[
138-
Optional[List[AuthenticationScheme]], Mutability.read_only, Required.true
137+
Optional[list[AuthenticationScheme]], Mutability.read_only, Required.true
139138
] = None
140139
"""A complex type that specifies supported authentication scheme
141140
properties."""

0 commit comments

Comments
 (0)