Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: format docstrings with ruff #2248

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -75,7 +78,7 @@ select = [
"W", # pycodestyle warning
]

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"tests/*" = ["E721"]

[tool.mypy]
Expand Down
3 changes: 2 additions & 1 deletion src/marshmallow/class_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
26 changes: 16 additions & 10 deletions src/marshmallow/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions src/marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -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`.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/marshmallow/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.']
Expand Down