Skip to content

Commit cfe2ce9

Browse files
committed
chore: use ruff instead of docformatter
1 parent 0ff2de1 commit cfe2ce9

25 files changed

+97
-289
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ repos:
1515
- id: end-of-file-fixer
1616
exclude: "\\.svg$|\\.map$|\\.min\\.css$|\\.min\\.js$|\\.po$|\\.pot$"
1717
- id: check-toml
18-
- repo: https://github.com/PyCQA/docformatter
19-
rev: v1.7.5
20-
hooks:
21-
- id: docformatter
2218
- repo: https://github.com/pre-commit/mirrors-mypy
2319
rev: v1.11.2
2420
hooks:

pyproject.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ exclude_lines = [
6868

6969
[tool.ruff.lint]
7070
select = [
71+
"D", # pydocstyle
7172
"E", # pycodestyle
7273
"F", # pyflakes
7374
"I", # isort
@@ -76,6 +77,16 @@ select = [
7677
ignore = [
7778
"E501", # line-too-long
7879
"E722", # bare-except
80+
"D100", # public module
81+
"D101", # public class
82+
"D102", # public method
83+
"D103", # public function
84+
"D104", # public package
85+
"D105", # magic method
86+
"D106", # nested class
87+
"D107", # public init
88+
"D203", # no-blank-line-before-class
89+
"D213", # multi-line-summary-second-line
7990
]
8091

8192
[tool.ruff.lint.isort]

scim2_models/base.py

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838

3939

4040
def validate_model_attribute(model: type["BaseModel"], attribute_base: str) -> None:
41-
"""Validate that an attribute name or a sub-attribute path exist for a
42-
given model."""
43-
41+
"""Validate that an attribute name or a sub-attribute path exist for a given model."""
4442
from scim2_models.base import BaseModel
4543

4644
attribute_name, *sub_attribute_blocks = attribute_base.split(".")
@@ -85,7 +83,6 @@ def validate_attribute_urn(
8583
:resource_types: The available resources in which to look for the attribute.
8684
:return: The normalized attribute URN.
8785
"""
88-
8986
from scim2_models.rfc7643.resource import Resource
9087

9188
if not resource_types:
@@ -133,6 +130,7 @@ class Reference(UserString, Generic[ReferenceTypes]):
133130
- :data:`~scim2_models.URIReference`
134131
135132
Examples
133+
--------
136134
137135
.. code-block:: python
138136
@@ -141,6 +139,7 @@ class Foobar(Resource):
141139
managers: Reference[Union["User", "Group"]]
142140
photo: Reference[ExternalReference]
143141
website: Reference[URIReference]
142+
144143
"""
145144

146145
@classmethod
@@ -159,8 +158,7 @@ def _validate(cls, input_value: str, /) -> str:
159158

160159

161160
class Context(Enum):
162-
"""Represent the different HTTP contexts detailed in :rfc:`RFC7644 §3.2
163-
<7644#section-3.2>`
161+
"""Represent the different HTTP contexts detailed in :rfc:`RFC7644 §3.2 <7644#section-3.2>`.
164162
165163
Contexts are intended to be used during model validation and serialization.
166164
For instance a client preparing a resource creation POST request can use
@@ -298,8 +296,7 @@ def is_response(cls, ctx: "Context") -> bool:
298296

299297

300298
class Mutability(str, Enum):
301-
"""A single keyword indicating the circumstances under which the value of
302-
the attribute can be (re)defined:"""
299+
"""A single keyword indicating the circumstances under which the value of the attribute can be (re)defined."""
303300

304301
read_only = "readOnly"
305302
"""The attribute SHALL NOT be modified."""
@@ -326,9 +323,7 @@ class Mutability(str, Enum):
326323

327324

328325
class Returned(str, Enum):
329-
"""A single keyword that indicates when an attribute and associated values
330-
are returned in response to a GET request or in response to a PUT, POST, or
331-
PATCH request."""
326+
"""A single keyword that indicates when an attribute and associated values are returned in response to a GET request or in response to a PUT, POST, or PATCH request."""
332327

333328
always = "always" # cannot be excluded
334329
"""The attribute is always returned, regardless of the contents of the
@@ -353,8 +348,7 @@ class Returned(str, Enum):
353348

354349

355350
class Uniqueness(str, Enum):
356-
"""A single keyword value that specifies how the service provider enforces
357-
uniqueness of attribute values."""
351+
"""A single keyword value that specifies how the service provider enforces uniqueness of attribute values."""
358352

359353
none = "none"
360354
"""The values are not intended to be unique in any way."""
@@ -393,8 +387,7 @@ def __bool__(self):
393387

394388

395389
class CaseExact(Enum):
396-
"""A Boolean value that specifies whether a string attribute is case-
397-
sensitive or not."""
390+
"""A Boolean value that specifies whether a string attribute is case- sensitive or not."""
398391

399392
true = True
400393
false = False
@@ -421,8 +414,7 @@ class BaseModel(PydanticBaseModel):
421414

422415
@classmethod
423416
def get_field_annotation(cls, field_name: str, annotation_type: type) -> Any:
424-
"""Return the annotation of type 'annotation_type' of the field
425-
'field_name'."""
417+
"""Return the annotation of type 'annotation_type' of the field 'field_name'."""
426418
field_metadata = cls.model_fields[field_name].metadata
427419

428420
default_value = getattr(annotation_type, "_default", None)
@@ -442,7 +434,6 @@ def get_field_root_type(cls, attribute_name: str) -> type:
442434
For example, return 'GroupMember' for
443435
'Optional[List[GroupMember]]'
444436
"""
445-
446437
attribute_type = cls.model_fields[attribute_name].annotation
447438

448439
# extract 'x' from 'Optional[x]'
@@ -462,10 +453,7 @@ def get_field_root_type(cls, attribute_name: str) -> type:
462453
def check_request_attributes_mutability(
463454
cls, value: Any, info: ValidationInfo
464455
) -> Any:
465-
"""Check and fix that the field mutability is expected according to the
466-
requests validation context, as defined in :rfc:`RFC7643 §7
467-
<7653#section-7>`."""
468-
456+
"""Check and fix that the field mutability is expected according to the requests validation context, as defined in :rfc:`RFC7643 §7 <7653#section-7>`."""
469457
if (
470458
not info.context
471459
or not info.context.get("scim")
@@ -534,10 +522,7 @@ def normalize_value(value: Any) -> Any:
534522
def check_response_attributes_returnability(
535523
cls, value: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
536524
) -> Self:
537-
"""Check that the fields returnability is expected according to the
538-
responses validation context, as defined in :rfc:`RFC7643 §7
539-
<7653#section-7>`."""
540-
525+
"""Check that the fields returnability is expected according to the responses validation context, as defined in :rfc:`RFC7643 §7 <7653#section-7>`."""
541526
value = handler(value)
542527

543528
if (
@@ -578,9 +563,7 @@ def check_response_attributes_returnability(
578563
def check_response_attributes_necessity(
579564
cls, value: Any, handler: ValidatorFunctionWrapHandler, info: ValidationInfo
580565
) -> Self:
581-
"""Check that the required attributes are present in creations and
582-
replacement requests."""
583-
566+
"""Check that the required attributes are present in creations and replacement requests."""
584567
value = handler(value)
585568

586569
if (
@@ -609,12 +592,7 @@ def check_response_attributes_necessity(
609592
return value
610593

611594
def mark_with_schema(self):
612-
"""Navigate through attributes and sub-attributes of type
613-
ComplexAttribute, and mark them with a '_schema' attribute.
614-
615-
'_schema' will later be used by 'get_attribute_urn'.
616-
"""
617-
595+
"""Navigate through attributes and sub-attributes of type ComplexAttribute, and mark them with a '_schema' attribute. '_schema' will later be used by 'get_attribute_urn'."""
618596
from scim2_models.rfc7643.resource import Resource
619597

620598
for field_name, field in self.model_fields.items():
@@ -644,9 +622,7 @@ def scim_serializer(
644622
handler: SerializerFunctionWrapHandler,
645623
info: SerializationInfo,
646624
) -> Any:
647-
"""Serialize the fields according to mutability indications passed in
648-
the serialization context."""
649-
625+
"""Serialize the fields according to mutability indications passed in the serialization context."""
650626
value = handler(value)
651627

652628
if info.context.get("scim") and Context.is_request(info.context["scim"]):
@@ -658,9 +634,7 @@ def scim_serializer(
658634
return value
659635

660636
def scim_request_serializer(self, value: Any, info: SerializationInfo) -> Any:
661-
"""Serialize the fields according to mutability indications passed in
662-
the serialization context."""
663-
637+
"""Serialize the fields according to mutability indications passed in the serialization context."""
664638
mutability = self.get_field_annotation(info.field_name, Mutability)
665639
context = info.context.get("scim")
666640

@@ -689,9 +663,7 @@ def scim_request_serializer(self, value: Any, info: SerializationInfo) -> Any:
689663
return value
690664

691665
def scim_response_serializer(self, value: Any, info: SerializationInfo) -> Any:
692-
"""Serialize the fields according to returnability indications passed
693-
in the serialization context."""
694-
666+
"""Serialize the fields according to returnability indications passed in the serialization context."""
695667
returnability = self.get_field_annotation(info.field_name, Returned)
696668
attribute_urn = self.get_attribute_urn(info.field_name)
697669
included_urns = info.context.get("scim_attributes", [])
@@ -724,9 +696,7 @@ def scim_response_serializer(self, value: Any, info: SerializationInfo) -> Any:
724696
def model_serializer_exclude_none(
725697
self, handler, info: SerializationInfo
726698
) -> dict[str, Any]:
727-
"""Remove `None` values inserted by the
728-
:meth:`~scim2_models.base.BaseModel.scim_serializer`."""
729-
699+
"""Remove `None` values inserted by the :meth:`~scim2_models.base.BaseModel.scim_serializer`."""
730700
self.mark_with_schema()
731701
result = handler(self)
732702
return {key: value for key, value in result.items() if value is not None}
@@ -735,9 +705,7 @@ def model_serializer_exclude_none(
735705
def model_validate(
736706
cls, *args, scim_ctx: Optional[Context] = Context.DEFAULT, **kwargs
737707
) -> "BaseModel":
738-
"""Validate SCIM payloads and generate model representation by using
739-
Pydantic :code:`BaseModel.model_validate`."""
740-
708+
"""Validate SCIM payloads and generate model representation by using Pydantic :code:`BaseModel.model_validate`."""
741709
kwargs.setdefault("context", {}).setdefault("scim", scim_ctx)
742710
return super().model_validate(*args, **kwargs)
743711

@@ -749,14 +717,12 @@ def model_dump(
749717
excluded_attributes: Optional[list[str]] = None,
750718
**kwargs,
751719
):
752-
"""Create a model representation that can be included in SCIM messages
753-
by using Pydantic :code:`BaseModel.model_dump`.
720+
"""Create a model representation that can be included in SCIM messages by using Pydantic :code:`BaseModel.model_dump`.
754721
755722
:param scim_ctx: If a SCIM context is passed, some default values of
756723
Pydantic :code:`BaseModel.model_dump` are tuned to generate valid SCIM
757724
messages. Pass :data:`None` to get the default Pydantic behavior.
758725
"""
759-
760726
kwargs.setdefault("context", {}).setdefault("scim", scim_ctx)
761727
kwargs["context"]["scim_attributes"] = [
762728
validate_attribute_urn(attribute, self.__class__)
@@ -788,8 +754,7 @@ def get_attribute_urn(self, field_name: str) -> str:
788754

789755

790756
class ComplexAttribute(BaseModel):
791-
"""A complex attribute as defined in :rfc:`RFC7643 §2.3.8
792-
<7643#section-2.3.8>`."""
757+
"""A complex attribute as defined in :rfc:`RFC7643 §2.3.8 <7643#section-2.3.8>`."""
793758

794759
_schema: Optional[str] = None
795760

scim2_models/rfc7643/resource.py

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,9 @@
2727

2828

2929
class Meta(ComplexAttribute):
30-
"""All "meta" sub-attributes are assigned by the service provider (have a
31-
"mutability" of "readOnly"), and all of these sub-attributes have a
32-
"returned" characteristic of "default".
30+
"""All "meta" sub-attributes are assigned by the service provider (have a "mutability" of "readOnly"), and all of these sub-attributes have a "returned" characteristic of "default".
3331
34-
This attribute SHALL be
35-
ignored when provided by clients. "meta" contains the following
36-
sub-attributes:
32+
This attribute SHALL be ignored when provided by clients. "meta" contains the following sub-attributes:
3733
"""
3834

3935
resource_type: Optional[str] = None
@@ -84,16 +80,12 @@ class Meta(ComplexAttribute):
8480
class Extension(BaseModel):
8581
@classmethod
8682
def to_schema(cls):
87-
"""Build a :class:`~scim2_models.Schema` from the current extension
88-
class."""
89-
83+
"""Build a :class:`~scim2_models.Schema` from the current extension class."""
9084
return model_to_schema(cls)
9185

9286
@classmethod
9387
def from_schema(cls, schema) -> "Extension":
94-
"""Build a :class:`~scim2_models.Extension` subclass from the schema
95-
definition."""
96-
88+
"""Build a :class:`~scim2_models.Extension` subclass from the schema definition."""
9789
from .schema import make_python_model
9890

9991
return make_python_model(schema, cls)
@@ -108,7 +100,6 @@ def extension_serializer(value: Any, handler, info) -> Optional[dict[str, Any]]:
108100
For instance, attributes 'meta', 'id' or 'schemas' should not be
109101
dumped when the model is used as an extension for another model.
110102
"""
111-
112103
partial_result = handler(value, info)
113104
result = {
114105
attr_name: value
@@ -121,7 +112,6 @@ def extension_serializer(value: Any, handler, info) -> Optional[dict[str, Any]]:
121112
class ResourceMetaclass(BaseModelType):
122113
def __new__(cls, name, bases, attrs, **kwargs):
123114
"""Dynamically add a field for each extension."""
124-
125115
if "__pydantic_generic_metadata__" in kwargs:
126116
extensions = kwargs["__pydantic_generic_metadata__"]["args"][0]
127117
extensions = (
@@ -188,9 +178,7 @@ def __setitem__(self, item: Any, value: "Resource"):
188178

189179
@classmethod
190180
def get_extension_models(cls) -> dict[str, type]:
191-
"""Return extension a dict associating extension models with their
192-
schemas."""
193-
181+
"""Return extension a dict associating extension models with their schemas."""
194182
extension_models = cls.__pydantic_generic_metadata__.get("args", [])
195183
extension_models = (
196184
get_args(extension_models[0])
@@ -207,9 +195,7 @@ def get_extension_models(cls) -> dict[str, type]:
207195
def get_by_schema(
208196
resource_types: list[type[BaseModel]], schema: str, with_extensions=True
209197
) -> Optional[type]:
210-
"""Given a resource type list and a schema, find the matching resource
211-
type."""
212-
198+
"""Given a resource type list and a schema, find the matching resource type."""
213199
by_schema = {
214200
resource_type.model_fields["schemas"].default[0].lower(): resource_type
215201
for resource_type in (resource_types or [])
@@ -227,9 +213,7 @@ def get_by_schema(
227213

228214
@staticmethod
229215
def get_by_payload(resource_types: list[type], payload: dict, **kwargs):
230-
"""Given a resource type list and a payload, find the matching resource
231-
type."""
232-
216+
"""Given a resource type list and a payload, find the matching resource type."""
233217
if not payload or not payload.get("schemas"):
234218
return None
235219

@@ -239,7 +223,6 @@ def get_by_payload(resource_types: list[type], payload: dict, **kwargs):
239223
@field_serializer("schemas")
240224
def set_extension_schemas(self, schemas: list[str]):
241225
"""Add model extension ids to the 'schemas' attribute."""
242-
243226
extension_schemas = self.get_extension_models().keys()
244227
schemas = self.schemas + [
245228
schema for schema in extension_schemas if schema not in self.schemas
@@ -248,16 +231,12 @@ def set_extension_schemas(self, schemas: list[str]):
248231

249232
@classmethod
250233
def to_schema(cls):
251-
"""Build a :class:`~scim2_models.Schema` from the current resource
252-
class."""
253-
234+
"""Build a :class:`~scim2_models.Schema` from the current resource class."""
254235
return model_to_schema(cls)
255236

256237
@classmethod
257238
def from_schema(cls, schema) -> "Resource":
258-
"""Build a :class:`scim2_models.Resource` subclass from the schema
259-
definition."""
260-
239+
"""Build a :class:`scim2_models.Resource` subclass from the schema definition."""
261240
from .schema import make_python_model
262241

263242
return make_python_model(schema, cls)

0 commit comments

Comments
 (0)