From 0ea2222dcc5ebe382872a71c019cc2d0412f585e Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Mon, 5 Jun 2023 15:14:08 +0300 Subject: [PATCH] CI: Replace isort with Ruff import sorting (#1507) This is the first step in adopting Ruff: https://beta.ruff.rs/ Ruff is faster and will allow us to perform more fixes and checks in the future (e.g. replacing flake8, pyupgrade, auto-removing unused imports, etc). There are some import formatting changes around places that use renamed imports (import X import Y as Z) but I think it's not any worse. --- .gitignore | 1 + .pre-commit-config.yaml | 11 ++++------ django-stubs/db/backends/base/operations.pyi | 3 +-- django-stubs/db/models/fields/__init__.pyi | 3 +-- django-stubs/utils/dateparse.pyi | 3 +-- django-stubs/utils/formats.pyi | 3 +-- mypy_django_plugin/django/context.py | 3 +-- mypy_django_plugin/lib/helpers.py | 3 +-- mypy_django_plugin/transformers/fields.py | 3 +-- mypy_django_plugin/transformers/forms.py | 3 +-- mypy_django_plugin/transformers/functional.py | 3 +-- mypy_django_plugin/transformers/managers.py | 3 +-- mypy_django_plugin/transformers/meta.py | 3 +-- mypy_django_plugin/transformers/models.py | 3 +-- .../transformers/orm_lookups.py | 3 +-- mypy_django_plugin/transformers/querysets.py | 3 +-- mypy_django_plugin/transformers/request.py | 3 +-- mypy_django_plugin/transformers/settings.py | 3 +-- pyproject.toml | 21 +++++++++++++------ 19 files changed, 36 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index cf90aa3c1..cc1ac47cb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .idea/ .mypy_cache/ .pytest_cache/ +.ruff_cache/ .venv/ __pycache__/ django-source/ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3674d62c5..8da69e56e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,14 +17,11 @@ repos: hooks: - id: pyupgrade args: ["--py38-plus"] - - repo: https://github.com/pycqa/isort - rev: 5.12.0 + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.0.269 hooks: - - id: isort - name: isort (python) - - id: isort - name: isort (pyi) - types: [pyi] + - id: ruff + args: ["--fix", "--fixable=I001", "--exit-non-zero-on-fix"] - repo: https://github.com/psf/black rev: 23.3.0 hooks: diff --git a/django-stubs/db/backends/base/operations.pyi b/django-stubs/db/backends/base/operations.pyi index 040fc8e37..c6377f1e8 100644 --- a/django-stubs/db/backends/base/operations.pyi +++ b/django-stubs/db/backends/base/operations.pyi @@ -1,7 +1,6 @@ from collections.abc import Iterable, Sequence -from datetime import date +from datetime import date, time, timedelta from datetime import datetime as real_datetime -from datetime import time, timedelta from decimal import Decimal from typing import Any diff --git a/django-stubs/db/models/fields/__init__.pyi b/django-stubs/db/models/fields/__init__.pyi index 71c1e787f..3e739e94f 100644 --- a/django-stubs/db/models/fields/__init__.pyi +++ b/django-stubs/db/models/fields/__init__.pyi @@ -1,9 +1,8 @@ import decimal import uuid from collections.abc import Callable, Iterable, Sequence -from datetime import date +from datetime import date, time, timedelta from datetime import datetime as real_datetime -from datetime import time, timedelta from typing import Any, Generic, Protocol, TypeVar, overload from django.core import validators # due to weird mypy.stubtest error diff --git a/django-stubs/utils/dateparse.pyi b/django-stubs/utils/dateparse.pyi index 33c5f6c5a..7bac1bb61 100644 --- a/django-stubs/utils/dateparse.pyi +++ b/django-stubs/utils/dateparse.pyi @@ -1,6 +1,5 @@ -from datetime import date +from datetime import date, time, timedelta from datetime import datetime as builtin_datetime -from datetime import time, timedelta from re import Pattern date_re: Pattern[str] diff --git a/django-stubs/utils/formats.pyi b/django-stubs/utils/formats.pyi index 5943bd307..a195c83cc 100644 --- a/django-stubs/utils/formats.pyi +++ b/django-stubs/utils/formats.pyi @@ -1,8 +1,7 @@ import types from collections.abc import Iterator -from datetime import date +from datetime import date, time from datetime import datetime as builtin_datetime -from datetime import time from decimal import Decimal from typing import Any, TypeVar, overload diff --git a/mypy_django_plugin/django/context.py b/mypy_django_plugin/django/context.py index 1cd5eb700..7a3110506 100644 --- a/mypy_django_plugin/django/context.py +++ b/mypy_django_plugin/django/context.py @@ -17,9 +17,8 @@ from mypy.checker import TypeChecker from mypy.nodes import TypeInfo from mypy.plugin import MethodContext -from mypy.types import AnyType, Instance +from mypy.types import AnyType, Instance, TypeOfAny, UnionType from mypy.types import Type as MypyType -from mypy.types import TypeOfAny, UnionType from mypy_django_plugin.lib import fullnames, helpers from mypy_django_plugin.lib.fullnames import WITH_ANNOTATIONS_FULLNAME diff --git a/mypy_django_plugin/lib/helpers.py b/mypy_django_plugin/lib/helpers.py index f2e37c603..c20c16426 100644 --- a/mypy_django_plugin/lib/helpers.py +++ b/mypy_django_plugin/lib/helpers.py @@ -33,9 +33,8 @@ SemanticAnalyzerPluginInterface, ) from mypy.semanal import SemanticAnalyzer -from mypy.types import AnyType, Instance, NoneTyp, TupleType +from mypy.types import AnyType, Instance, NoneTyp, TupleType, TypedDictType, TypeOfAny, UnionType from mypy.types import Type as MypyType -from mypy.types import TypedDictType, TypeOfAny, UnionType from mypy_django_plugin.lib import fullnames from mypy_django_plugin.lib.fullnames import WITH_ANNOTATIONS_FULLNAME diff --git a/mypy_django_plugin/transformers/fields.py b/mypy_django_plugin/transformers/fields.py index 0e56b6e8d..410de10cf 100644 --- a/mypy_django_plugin/transformers/fields.py +++ b/mypy_django_plugin/transformers/fields.py @@ -6,9 +6,8 @@ from django.db.models.fields.reverse_related import ForeignObjectRel from mypy.nodes import AssignmentStmt, NameExpr, TypeInfo from mypy.plugin import FunctionContext -from mypy.types import AnyType, Instance +from mypy.types import AnyType, Instance, TypeOfAny, UnionType from mypy.types import Type as MypyType -from mypy.types import TypeOfAny, UnionType from mypy_django_plugin.django.context import DjangoContext from mypy_django_plugin.lib import fullnames, helpers diff --git a/mypy_django_plugin/transformers/forms.py b/mypy_django_plugin/transformers/forms.py index 34ac97770..f745a68de 100644 --- a/mypy_django_plugin/transformers/forms.py +++ b/mypy_django_plugin/transformers/forms.py @@ -1,9 +1,8 @@ from typing import Optional from mypy.plugin import ClassDefContext, MethodContext -from mypy.types import CallableType, Instance, NoneTyp +from mypy.types import CallableType, Instance, NoneTyp, TypeType from mypy.types import Type as MypyType -from mypy.types import TypeType from mypy_django_plugin.lib import helpers diff --git a/mypy_django_plugin/transformers/functional.py b/mypy_django_plugin/transformers/functional.py index 005f7da25..9a8a51c05 100644 --- a/mypy_django_plugin/transformers/functional.py +++ b/mypy_django_plugin/transformers/functional.py @@ -2,9 +2,8 @@ from mypy.errorcodes import ATTR_DEFINED from mypy.nodes import CallExpr, MemberExpr from mypy.plugin import AttributeContext -from mypy.types import AnyType, Instance +from mypy.types import AnyType, Instance, TypeOfAny from mypy.types import Type as MypyType -from mypy.types import TypeOfAny from mypy_django_plugin.lib import helpers diff --git a/mypy_django_plugin/transformers/managers.py b/mypy_django_plugin/transformers/managers.py index eeca04751..7fa324437 100644 --- a/mypy_django_plugin/transformers/managers.py +++ b/mypy_django_plugin/transformers/managers.py @@ -18,9 +18,8 @@ from mypy.plugin import AttributeContext, ClassDefContext, DynamicClassDefContext from mypy.semanal import SemanticAnalyzer from mypy.semanal_shared import has_placeholder -from mypy.types import AnyType, CallableType, Instance, ProperType +from mypy.types import AnyType, CallableType, Instance, ProperType, TypeOfAny from mypy.types import Type as MypyType -from mypy.types import TypeOfAny from mypy.typevars import fill_typevars from mypy_django_plugin.lib import fullnames, helpers diff --git a/mypy_django_plugin/transformers/meta.py b/mypy_django_plugin/transformers/meta.py index ea6a4e3bd..3e164d7c9 100644 --- a/mypy_django_plugin/transformers/meta.py +++ b/mypy_django_plugin/transformers/meta.py @@ -1,8 +1,7 @@ from django.core.exceptions import FieldDoesNotExist from mypy.plugin import MethodContext -from mypy.types import AnyType, Instance +from mypy.types import AnyType, Instance, TypeOfAny from mypy.types import Type as MypyType -from mypy.types import TypeOfAny from mypy_django_plugin.django.context import DjangoContext from mypy_django_plugin.lib import helpers diff --git a/mypy_django_plugin/transformers/models.py b/mypy_django_plugin/transformers/models.py index 8df69b1fe..c3d7fa267 100644 --- a/mypy_django_plugin/transformers/models.py +++ b/mypy_django_plugin/transformers/models.py @@ -9,9 +9,8 @@ from mypy.plugin import AnalyzeTypeContext, AttributeContext, CheckerPluginInterface, ClassDefContext from mypy.plugins import common from mypy.semanal import SemanticAnalyzer -from mypy.types import AnyType, Instance +from mypy.types import AnyType, Instance, TypedDictType, TypeOfAny, get_proper_type from mypy.types import Type as MypyType -from mypy.types import TypedDictType, TypeOfAny, get_proper_type from mypy.typevars import fill_typevars from mypy_django_plugin.django.context import DjangoContext diff --git a/mypy_django_plugin/transformers/orm_lookups.py b/mypy_django_plugin/transformers/orm_lookups.py index 6af0312f0..c2f530d0c 100644 --- a/mypy_django_plugin/transformers/orm_lookups.py +++ b/mypy_django_plugin/transformers/orm_lookups.py @@ -1,7 +1,6 @@ from mypy.plugin import MethodContext -from mypy.types import AnyType, Instance +from mypy.types import AnyType, Instance, TypeOfAny from mypy.types import Type as MypyType -from mypy.types import TypeOfAny from mypy_django_plugin.django.context import DjangoContext from mypy_django_plugin.lib import fullnames, helpers diff --git a/mypy_django_plugin/transformers/querysets.py b/mypy_django_plugin/transformers/querysets.py index 6536ddc9e..8499aedc7 100644 --- a/mypy_django_plugin/transformers/querysets.py +++ b/mypy_django_plugin/transformers/querysets.py @@ -7,9 +7,8 @@ from django.db.models.fields.reverse_related import ForeignObjectRel from mypy.nodes import ARG_NAMED, ARG_NAMED_OPT, Expression, NameExpr from mypy.plugin import FunctionContext, MethodContext -from mypy.types import AnyType, Instance, TupleType +from mypy.types import AnyType, Instance, TupleType, TypedDictType, TypeOfAny, get_proper_type from mypy.types import Type as MypyType -from mypy.types import TypedDictType, TypeOfAny, get_proper_type from mypy_django_plugin.django.context import DjangoContext, LookupsAreUnsupported from mypy_django_plugin.lib import fullnames, helpers diff --git a/mypy_django_plugin/transformers/request.py b/mypy_django_plugin/transformers/request.py index cfc857868..a77001f12 100644 --- a/mypy_django_plugin/transformers/request.py +++ b/mypy_django_plugin/transformers/request.py @@ -1,7 +1,6 @@ from mypy.plugin import AttributeContext, MethodContext -from mypy.types import Instance +from mypy.types import Instance, UninhabitedType, UnionType from mypy.types import Type as MypyType -from mypy.types import UninhabitedType, UnionType from mypy_django_plugin.django.context import DjangoContext from mypy_django_plugin.lib import helpers diff --git a/mypy_django_plugin/transformers/settings.py b/mypy_django_plugin/transformers/settings.py index b598450a9..67caa6a50 100644 --- a/mypy_django_plugin/transformers/settings.py +++ b/mypy_django_plugin/transformers/settings.py @@ -1,8 +1,7 @@ from mypy.nodes import MemberExpr from mypy.plugin import AttributeContext, FunctionContext -from mypy.types import AnyType, Instance +from mypy.types import AnyType, Instance, TypeOfAny, TypeType from mypy.types import Type as MypyType -from mypy.types import TypeOfAny, TypeType from mypy_django_plugin.django.context import DjangoContext from mypy_django_plugin.lib import helpers diff --git a/pyproject.toml b/pyproject.toml index 17b12804f..0162edf54 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,12 +2,21 @@ line-length = 120 include = '\.pyi?$' -[tool.isort] -profile = 'black' -line_length = 120 -multi_line_output = 3 -include_trailing_comma = true -float_to_top = true +[tool.ruff] +# Adds to default excludes: https://ruff.rs/docs/settings/#exclude +extend-exclude = [ + "django-source", + "stubgen", + "out", +] +line-length = 120 +target-version = "py38" +select = [ + "I", # isort +] + +[tool.ruff.isort] +split-on-trailing-comma = false [build-system] requires = ["setuptools<64", "wheel"]