Skip to content

Commit

Permalink
Switch to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Jan 16, 2024
1 parent 5f3fdfe commit dfdf244
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 62 deletions.
4 changes: 0 additions & 4 deletions .flake8

This file was deleted.

18 changes: 4 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: pyupgrade
args: [--py38-plus]
- id: ruff
- id: ruff-format
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.3
hooks:
- id: check-github-workflows
- id: check-readthedocs
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear==23.12.2]
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
Expand Down
6 changes: 1 addition & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Flask-Marshmallow
*****************

|pypi-package| |build-status| |docs| |marshmallow3| |black|
|pypi-package| |build-status| |docs| |marshmallow3|

Flask + marshmallow for beautiful APIs
======================================
Expand Down Expand Up @@ -130,7 +130,3 @@ MIT licensed. See the bundled `LICENSE <https://github.com/marshmallow-code/flas
.. |marshmallow3| image:: https://badgen.net/badge/marshmallow/3
:target: https://marshmallow.readthedocs.io/en/latest/upgrading.html
:alt: marshmallow 3 compatible

.. |black| image:: https://badgen.net/badge/code%20style/black/000
:target: https://github.com/ambv/black
:alt: code style: black
14 changes: 7 additions & 7 deletions docs/_themes/flask_theme_support.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# flasky extensions. flasky pygments style based on tango style
from pygments.style import Style
from pygments.token import (
Keyword,
Name,
Comment,
String,
Error,
Generic,
Keyword,
Literal,
Name,
Number,
Operator,
Generic,
Whitespace,
Punctuation,
Other,
Literal,
Punctuation,
String,
Whitespace,
)


Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime as dt
import sys
import os
import sys

sys.path.insert(0, os.path.abspath(os.path.join("..", "src")))
import flask_marshmallow # noqa: E402
Expand Down
18 changes: 15 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,21 @@ build-backend = "flit_core.buildapi"
include = ["docs/", "tests/", "CHANGELOG.rst", "CONTRIBUTING.rst", "tox.ini"]
exclude = ["docs/_build/"]

[tool.black]
line-length = 88
target-version = ['py38', 'py39', 'py310', 'py311', 'py312']
[tool.ruff]
src = ["src"]
fix = true
show-fixes = true
show-source = true

[tool.ruff.lint]
select = [
"B", # flake8-bugbear
"E", # pycodestyle error
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
"W", # pycodestyle warning
]

[tool.pytest.ini_options]
filterwarnings = [
Expand Down
12 changes: 7 additions & 5 deletions src/flask_marshmallow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"""
import warnings

from marshmallow import fields as base_fields, exceptions, pprint
from marshmallow import exceptions, pprint
from marshmallow import fields as base_fields

from . import fields
from .schema import Schema
Expand Down Expand Up @@ -80,14 +81,15 @@ class Meta:
})
In order to integrate with Flask-SQLAlchemy, this extension must be initialized *after*
`flask_sqlalchemy.SQLAlchemy`. ::
In order to integrate with Flask-SQLAlchemy, this extension must be initialized
*after* `flask_sqlalchemy.SQLAlchemy`. ::
db = SQLAlchemy(app)
ma = Marshmallow(app)
This gives you access to `ma.SQLAlchemySchema` and `ma.SQLAlchemyAutoSchema`, which generate
marshmallow `~marshmallow.Schema` classes based on the passed in model or table. ::
This gives you access to `ma.SQLAlchemySchema` and `ma.SQLAlchemyAutoSchema`, which
generate marshmallow `~marshmallow.Schema` classes
based on the passed in model or table. ::
class AuthorSchema(ma.SQLAlchemyAutoSchema):
class Meta:
Expand Down
19 changes: 10 additions & 9 deletions src/flask_marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
"""
import re

from flask import url_for
from flask import current_app
from marshmallow import fields
from marshmallow import missing

from flask import current_app, url_for
from marshmallow import fields, missing

__all__ = [
"URLFor",
Expand Down Expand Up @@ -78,7 +75,10 @@ class URLFor(fields.Field):
Usage: ::
url = URLFor('author_get', values=dict(id='<id>'))
https_url = URLFor('author_get', values=dict(id='<id>', _scheme='https', _external=True))
https_url = URLFor(
'author_get',
values=dict(id='<id>', _scheme='https', _external=True),
)
:param str endpoint: Flask endpoint name.
:param dict values: Same keyword arguments as Flask's url_for, except string
Expand Down Expand Up @@ -109,8 +109,7 @@ def _serialize(self, value, key, obj):
param_values[name] = attribute_value
else:
raise AttributeError(
"{attr_name!r} is not a valid "
"attribute of {obj!r}".format(attr_name=attr_name, obj=obj)
f"{attr_name!r} is not a valid " f"attribute of {obj!r}"
)
else:
param_values[name] = attr_tpl
Expand All @@ -135,7 +134,9 @@ def __init__(self, endpoint, values=None, **kwargs):


def _rapply(d, func, *args, **kwargs):
"""Apply a function to all values in a dictionary or list of dictionaries, recursively."""
"""Apply a function to all values in a dictionary or
list of dictionaries, recursively.
"""
if isinstance(d, (tuple, list)):
return [_rapply(each, func, *args, **kwargs) for each in d]
if isinstance(d, dict):
Expand Down
1 change: 0 additions & 1 deletion src/flask_marshmallow/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import flask
import marshmallow as ma


sentinel = object()


Expand Down
3 changes: 2 additions & 1 deletion src/flask_marshmallow/sqla.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
`SQLAlchemyAutoSchema <marshmallow_sqlalchemy.SQLAlchemyAutoSchema>` classes
that use the scoped session from Flask-SQLAlchemy.
"""
from flask import url_for, current_app
from urllib import parse

import marshmallow_sqlalchemy as msqla
from flask import current_app, url_for
from marshmallow.exceptions import ValidationError

from .schema import Schema
Expand Down
3 changes: 2 additions & 1 deletion src/flask_marshmallow/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class FileSize(Validator):
"""Validator which succeeds if the file passed to it is within the specified
size range. If ``min`` is not specified, or is specified as `None`,
no lower bound exists. If ``max`` is not specified, or is specified as `None`,
no upper bound exists. The inclusivity of the bounds (if they exist) is configurable.
no upper bound exists. The inclusivity of the bounds (if they exist)
is configurable.
If ``min_inclusive`` is not specified, or is specified as `True`, then
the ``min`` bound is included in the range. If ``max_inclusive`` is not specified,
or is specified as `True`, then the ``max`` bound is included in the range.
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Pytest fixtures for the test suite."""
import pytest
from flask import Flask

from flask_marshmallow import Marshmallow

_app = Flask(__name__)
Expand Down
4 changes: 2 additions & 2 deletions tests/markers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import importlib.metadata
from packaging.version import Version

import pytest
from packaging.version import Version

flask_version = Version(importlib.metadata.version("flask"))
flask_1_req = pytest.mark.skipif(
Expand Down
7 changes: 4 additions & 3 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import pytest
from flask import url_for
from flask_marshmallow.fields import _tpl
from marshmallow.exceptions import ValidationError
from werkzeug.routing import BuildError
from werkzeug.datastructures import FileStorage
from werkzeug.routing import BuildError

from flask_marshmallow.fields import _tpl


@pytest.mark.parametrize(
Expand Down Expand Up @@ -137,7 +138,7 @@ def test_absolute_url_deserialization(ma):


def test_aliases(ma):
from flask_marshmallow.fields import AbsoluteUrlFor, AbsoluteURLFor, UrlFor, URLFor
from flask_marshmallow.fields import AbsoluteURLFor, AbsoluteUrlFor, URLFor, UrlFor

assert UrlFor is URLFor
assert AbsoluteUrlFor is AbsoluteURLFor
Expand Down
7 changes: 4 additions & 3 deletions tests/test_sqla.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from flask import Flask, url_for
from flask_sqlalchemy import SQLAlchemy
from marshmallow import ValidationError
from werkzeug.wrappers import Response

from flask_marshmallow import Marshmallow
from flask_marshmallow.sqla import HyperlinkRelated
from marshmallow import ValidationError
from tests.conftest import Bunch

try:
Expand Down Expand Up @@ -171,8 +171,9 @@ class Meta:
assert isinstance(resp, Response)

# FIXME: temporarily filter out this warning
# this is triggered by marshmallow-sqlalchemy on sqlalchemy v1.4.x on the current version
# it should be fixed in an upcoming marshmallow-sqlalchemy release
# this is triggered by marshmallow-sqlalchemy on sqlalchemy v1.4.x
# on the current version it should be fixed
# in an upcoming marshmallow-sqlalchemy release
@requires_sqlalchemyschema
def test_hyperlink_related_field(self, extma, models, db, extapp):
class BookSchema(extma.SQLAlchemySchema):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_filesize_repr():
min=None, max=None, error=None, min_inclusive=True, max_inclusive=True
)
)
== "<FileSize(min=None, max=None, min_inclusive=True, max_inclusive=True, error=None)>"
== "<FileSize(min=None, max=None, min_inclusive=True, max_inclusive=True, error=None)>" # noqa: E501
)

assert (
Expand All @@ -71,8 +71,8 @@ def test_filesize_repr():
max_inclusive=False,
)
)
== "<FileSize(min='1 KiB', max='3 KiB', min_inclusive=False, max_inclusive=False, error='foo')>"
) # noqa: E501
== "<FileSize(min='1 KiB', max='3 KiB', min_inclusive=False, max_inclusive=False, error='foo')>" # noqa: E501
)


def test_filesize_wrongtype():
Expand Down

0 comments on commit dfdf244

Please sign in to comment.