diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7948ce3af..0b937e1aa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,6 +9,7 @@ repos: hooks: - id: check-github-workflows - id: check-readthedocs +# TODO: Remove blacken-docs when https://github.com/astral-sh/ruff/issues/8237 is implemented - repo: https://github.com/asottile/blacken-docs rev: 1.16.0 hooks: diff --git a/pyproject.toml b/pyproject.toml index d70d7525a..c4adfc160 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,9 @@ fix = true show-fixes = true show-source = true +[tool.ruff.format] +docstring-code-format = true + [tool.ruff.lint] ignore = ["E203", "E266", "E501", "E731"] select = [ @@ -75,7 +78,7 @@ select = [ "W", # pycodestyle warning ] -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] "tests/*" = ["E721"] [tool.mypy] diff --git a/src/marshmallow/class_registry.py b/src/marshmallow/class_registry.py index f427738d8..810d1155d 100644 --- a/src/marshmallow/class_registry.py +++ b/src/marshmallow/class_registry.py @@ -36,7 +36,8 @@ def register(classname: str, cls: SchemaType) -> None: class MyClass: pass - register('MyClass', MyClass) + + register("MyClass", MyClass) # Registry: # { # 'MyClass': [path.to.MyClass], diff --git a/src/marshmallow/decorators.py b/src/marshmallow/decorators.py index 82cdbb697..dafca9539 100644 --- a/src/marshmallow/decorators.py +++ b/src/marshmallow/decorators.py @@ -15,39 +15,45 @@ Example: :: from marshmallow import ( - Schema, pre_load, pre_dump, post_load, validates_schema, - validates, fields, ValidationError + Schema, + pre_load, + pre_dump, + post_load, + validates_schema, + validates, + fields, + ValidationError, ) - class UserSchema(Schema): + class UserSchema(Schema): email = fields.Str(required=True) age = fields.Integer(required=True) @post_load def lowerstrip_email(self, item, many, **kwargs): - item['email'] = item['email'].lower().strip() + item["email"] = item["email"].lower().strip() return item @pre_load(pass_many=True) def remove_envelope(self, data, many, **kwargs): - namespace = 'results' if many else 'result' + namespace = "results" if many else "result" return data[namespace] @post_dump(pass_many=True) def add_envelope(self, data, many, **kwargs): - namespace = 'results' if many else 'result' + namespace = "results" if many else "result" return {namespace: data} @validates_schema def validate_email(self, data, **kwargs): - if len(data['email']) < 3: - raise ValidationError('Email must be more than 3 characters', 'email') + if len(data["email"]) < 3: + raise ValidationError("Email must be more than 3 characters", "email") - @validates('age') + @validates("age") def validate_age(self, data, **kwargs): if data < 14: - raise ValidationError('Too young!') + raise ValidationError("Too young!") .. note:: These decorators only work with instance methods. Class and static diff --git a/src/marshmallow/fields.py b/src/marshmallow/fields.py index 9594378b2..d305fde1b 100644 --- a/src/marshmallow/fields.py +++ b/src/marshmallow/fields.py @@ -394,7 +394,7 @@ def _serialize( class TitleCase(Field): def _serialize(self, value, attr, obj, **kwargs): if not value: - return '' + return "" return str(value).title() :param value: The value to be serialized. @@ -496,6 +496,7 @@ class ChildSchema(Schema): parent = fields.Nested(lambda: ParentSchema(only=("id",)), dump_only=True) siblings = fields.List(fields.Nested(lambda: ChildSchema(only=("id", "name")))) + class ParentSchema(Schema): id = fields.Str() children = fields.List( @@ -512,10 +513,10 @@ class ParentSchema(Schema): :: # Yes - author = fields.Nested(UserSchema, only=('id', 'name')) + author = fields.Nested(UserSchema, only=("id", "name")) # No - author = fields.Nested(UserSchema(), only=('id', 'name')) + author = fields.Nested(UserSchema(), only=("id", "name")) :param nested: `Schema` instance, class, class name (string), dictionary, or callable that returns a `Schema` or dictionary. Dictionaries are converted with `Schema.from_dict`. @@ -677,16 +678,18 @@ class Pluck(Nested): from marshmallow import Schema, fields + class ArtistSchema(Schema): id = fields.Int() name = fields.Str() + class AlbumSchema(Schema): - artist = fields.Pluck(ArtistSchema, 'id') + artist = fields.Pluck(ArtistSchema, "id") - in_data = {'artist': 42} - loaded = AlbumSchema().load(in_data) # => {'artist': {'id': 42}} + in_data = {"artist": 42} + loaded = AlbumSchema().load(in_data) # => {'artist': {'id': 42}} dumped = AlbumSchema().dump(loaded) # => {'artist': 42} :param Schema nested: The Schema class or class name (string) diff --git a/src/marshmallow/validate.py b/src/marshmallow/validate.py index c10ce85ea..e4536d88c 100644 --- a/src/marshmallow/validate.py +++ b/src/marshmallow/validate.py @@ -47,10 +47,12 @@ class And(Validator): from marshmallow import validate, ValidationError + def is_even(value): if value % 2 != 0: raise ValidationError("Not an even value.") + validator = validate.And(validate.Range(min=0), is_even) validator(-1) # ValidationError: ['Must be greater than or equal to 0.', 'Not an even value.']