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

Lint Python code with ruff instead of flake8 and isort #903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import re
import sys

import sphinx_rtd_theme
from docutils import nodes, utils
from docutils.parsers.rst import roles
from docutils.parsers.rst.roles import set_classes
Expand Down
79 changes: 78 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,80 @@
[build-system]
requires = ["setuptools >= 68.0.0", "wheel", "setuptools-scm >= 3.0.3"]
build-backend = "setuptools.build_meta"

requires = [ "setuptools>=68", "setuptools-scm>=3.0.3", "wheel" ]

[tool.ruff]
exclude = [ "**/migrations/*" ]
line-length = 169
lint.select = [
"AIR", # Airflow
"ASYNC", # flake8-async
"C4", # flake8-comprehensions
"C90", # mccabe
"E", # pycodestyle errors
"F", # Pyflakes
"FA", # flake8-future-annotations
"FLY", # flynt
"I", # isort
"ICN", # flake8-import-conventions
"INT", # flake8-gettext
"LOG", # flake8-logging
"NPY", # NumPy-specific rules
"PD", # pandas-vet
"PLE", # Pylint errors
"PYI", # flake8-pyi
"SLOT", # flake8-slots
"T10", # flake8-debugger
"TCH", # flake8-type-checking
"W", # pycodestyle warnings
"YTT", # flake8-2020
# "A", # flake8-builtins
# "ANN", # flake8-annotations
# "ARG", # flake8-unused-arguments
# "B", # flake8-bugbear
# "BLE", # flake8-blind-except
# "COM", # flake8-commas
# "CPY", # flake8-copyright
# "D", # pydocstyle
# "DJ", # flake8-django
# "DOC", # pydoclint
# "DTZ", # flake8-datetimez
# "EM", # flake8-errmsg
# "ERA", # eradicate
# "EXE", # flake8-executable
# "FAST", # FastAPI
# "FBT", # flake8-boolean-trap
# "FIX", # flake8-fixme
# "FURB", # refurb
# "G", # flake8-logging-format
# "INP", # flake8-no-pep420
# "ISC", # flake8-implicit-str-concat
# "N", # pep8-naming
# "PERF", # Perflint
# "PGH", # pygrep-hooks
# "PIE", # flake8-pie
# "PLC", # Pylint conventions
# "PLR", # Pylint refactorings
# "PLW", # Pylint warnings
# "PT", # flake8-pytest-style
# "PTH", # flake8-use-pathlib
# "Q", # flake8-quotes
# "RET", # flake8-return
# "RSE", # flake8-raise
# "RUF", # Ruff-specific rules
# "S", # flake8-bandit
# "SIM", # flake8-simplify
# "SLF", # flake8-self
# "T20", # flake8-print
# "TD", # flake8-todos
# "TID", # flake8-tidy-imports
# "TRY", # tryceratops
# "UP", # pyupgrade
]
lint.isort.known-first-party = [ "drf_yasg", "testproj", "articles", "people", "snippets", "todo", "users", "urlconfs" ]
lint.mccabe.max-complexity = 13
lint.per-file-ignores."src/drf_yasg/inspectors/field.py" = [ "E721" ]
lint.per-file-ignores."src/drf_yasg/openapi.py" = [ "E721" ]
lint.per-file-ignores."testproj/testproj/settings/*" = [ "F405" ]

[tool.setuptools_scm]
6 changes: 2 additions & 4 deletions requirements/lint.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# used by the 'lint' tox env for linting via flake8
isort>=5.12
flake8>=6.0.0
flake8-isort>=6.0.0
# used by the 'lint' tox env for linting via ruff
ruff>=0.7.0
2 changes: 1 addition & 1 deletion src/drf_yasg/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import logging
from collections import OrderedDict

from django.utils.encoding import force_bytes
import yaml
from django.utils.encoding import force_bytes

try:
from swagger_spec_validator.common import SwaggerValidationError as SSVErr
Expand Down
2 changes: 1 addition & 1 deletion src/drf_yasg/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import uritemplate
from django.urls import URLPattern, URLResolver
from rest_framework import versioning
from rest_framework.schemas.openapi import SchemaGenerator
from rest_framework.schemas.generators import EndpointEnumerator as _EndpointEnumerator
from rest_framework.schemas.generators import endpoint_ordering, get_pk_name
from rest_framework.schemas.openapi import SchemaGenerator
from rest_framework.schemas.utils import get_pk_description, is_list_view
from rest_framework.settings import api_settings

Expand Down
22 changes: 15 additions & 7 deletions src/drf_yasg/inspectors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from ..app_settings import swagger_settings
from .base import (
BaseInspector, FieldInspector, FilterInspector, NotHandled, PaginatorInspector, SerializerInspector, ViewInspector
)
from .base import BaseInspector, FieldInspector, FilterInspector, NotHandled, PaginatorInspector, SerializerInspector, ViewInspector
from .field import (
CamelCaseJSONFilter, ChoiceFieldInspector, DictFieldInspector, FileFieldInspector, HiddenFieldInspector,
InlineSerializerInspector, JSONFieldInspector, RecursiveFieldInspector, ReferencingSerializerInspector,
RelatedFieldInspector, SerializerMethodFieldInspector, SimpleFieldInspector, StringDefaultFieldInspector
CamelCaseJSONFilter,
ChoiceFieldInspector,
DictFieldInspector,
FileFieldInspector,
HiddenFieldInspector,
InlineSerializerInspector,
JSONFieldInspector,
RecursiveFieldInspector,
ReferencingSerializerInspector,
RelatedFieldInspector,
SerializerMethodFieldInspector,
SimpleFieldInspector,
StringDefaultFieldInspector,
)
from .query import DrfAPICompatInspector, CoreAPICompatInspector, DjangoRestResponsePagination
from .query import CoreAPICompatInspector, DjangoRestResponsePagination, DrfAPICompatInspector
from .view import SwaggerAutoSchema

# these settings must be accessed only after defining/importing all the classes in this module to avoid ImportErrors
Expand Down
10 changes: 4 additions & 6 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
import inspect
import logging
import operator
import typing
import uuid
from contextlib import suppress
from collections import OrderedDict
from contextlib import suppress
from decimal import Decimal
from inspect import signature as inspect_signature

import typing
from django.core import validators
from django.db import models
from packaging import version
from rest_framework import serializers
from rest_framework.settings import api_settings as rest_framework_settings

from .base import call_view_method, FieldInspector, NotHandled, SerializerInspector
from .. import openapi
from ..errors import SwaggerGenerationError
from ..utils import (
decimal_as_float, field_value_to_representation, filter_none, get_serializer_class, get_serializer_ref_name
)
from ..utils import decimal_as_float, field_value_to_representation, filter_none, get_serializer_class, get_serializer_ref_name
from .base import FieldInspector, NotHandled, SerializerInspector, call_view_method

try:
from importlib import metadata
Expand Down
2 changes: 1 addition & 1 deletion src/drf_yasg/inspectors/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .. import openapi
from ..utils import force_real_str
from .base import FilterInspector, PaginatorInspector, NotHandled
from .base import FilterInspector, NotHandled, PaginatorInspector


def ignore_assert_decorator(func):
Expand Down
5 changes: 1 addition & 4 deletions src/drf_yasg/inspectors/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

from .. import openapi
from ..errors import SwaggerGenerationError
from ..utils import (
filter_none, force_real_str, force_serializer_instance, get_consumes, get_produces, guess_response_status,
merge_params, no_body, param_list_to_odict
)
from ..utils import filter_none, force_real_str, force_serializer_instance, get_consumes, get_produces, guess_response_status, merge_params, no_body, param_list_to_odict
from .base import ViewInspector, call_view_method

logger = logging.getLogger(__name__)
Expand Down
3 changes: 2 additions & 1 deletion src/drf_yasg/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import logging
import re
import urllib.parse as urlparse
from collections import OrderedDict, abc as collections_abc
from collections import OrderedDict
from collections import abc as collections_abc

from django.urls import get_script_prefix
from django.utils.functional import Promise
Expand Down
2 changes: 1 addition & 1 deletion src/drf_yasg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def force_real_str(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
if s is not None:
s = force_str(s, encoding, strings_only, errors)
if type(s) != str:
if not isinstance(s, str):
s = '' + s

# Remove common indentation to get the correct Markdown rendering
Expand Down
2 changes: 2 additions & 0 deletions testproj/testproj/settings/heroku.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import dj_database_url

from .base import * # noqa: F403
Expand Down
5 changes: 1 addition & 4 deletions testproj/todo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from drf_yasg.utils import swagger_auto_schema

from .models import Pack, Todo, TodoAnother, TodoTree, TodoYetAnother
from .serializer import (
HarvestSerializer, TodoAnotherSerializer, TodoRecursiveSerializer, TodoSerializer, TodoTreeSerializer,
TodoYetAnotherSerializer
)
from .serializer import HarvestSerializer, TodoAnotherSerializer, TodoRecursiveSerializer, TodoSerializer, TodoTreeSerializer, TodoYetAnotherSerializer


class TodoViewSet(viewsets.ReadOnlyModelViewSet):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_versioning.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from drf_yasg.errors import SwaggerGenerationError
from drf_yasg.codecs import yaml_sane_load
from drf_yasg.errors import SwaggerGenerationError


def _get_versioned_schema(prefix, client, validate_schema, path='/snippets/'):
Expand Down
2 changes: 1 addition & 1 deletion tests/urlconfs/additional_fields_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from testproj.urls import required_urlpatterns

from .url_versioning import SnippetList, SnippetSerializer, VersionedSchemaView, VERSION_PREFIX_URL
from .url_versioning import VERSION_PREFIX_URL, SnippetList, SnippetSerializer, VersionedSchemaView


class SnippetsSerializer(serializers.HyperlinkedModelSerializer, SnippetSerializer):
Expand Down
4 changes: 2 additions & 2 deletions tests/urlconfs/coreschema.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.urls import re_path
import coreapi
import coreschema
from django.urls import re_path
from rest_framework import pagination

from testproj.urls import required_urlpatterns

from .url_versioning import SnippetList, VersionedSchemaView, VERSION_PREFIX_URL
from .url_versioning import VERSION_PREFIX_URL, SnippetList, VersionedSchemaView


class FilterBackendWithoutParams:
Expand Down
2 changes: 1 addition & 1 deletion tests/urlconfs/url_versioning_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from testproj.urls import required_urlpatterns

from .url_versioning import SnippetList, VERSION_PREFIX_URL, VersionedSchemaView
from .url_versioning import VERSION_PREFIX_URL, SnippetList, VersionedSchemaView

urlpatterns = required_urlpatterns + [
re_path(VERSION_PREFIX_URL + r"extra/snippets/$", SnippetList.as_view()),
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ skip_install = true
deps =
-r requirements/lint.txt
commands =
flake8 src/drf_yasg testproj tests setup.py
ruff check

[testenv:py38-docs]
deps =
Expand Down