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: Migrate from flake8 to ruff #1718

Merged
merged 8 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 0 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ repos:
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies:
- flake8-no-pep420==2.6.0
- flake8-pyi==23.5.0
types: []
files: ^.*.pyi?$
- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
hooks:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pre-commit install

### Testing and Linting

We use `mypy`, `pytest`, `flake8`, and `black` for quality control. All tools except pytest are executed using pre-commit when you make a commit.
We use `mypy`, `pytest`, `ruff`, and `black` for quality control. All tools except pytest are executed using pre-commit when you make a commit.
GabDug marked this conversation as resolved.
Show resolved Hide resolved
To ensure there are not formatting or typing issues in the entire repository you can run:

```bash
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/conf/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from django.utils.functional import LazyObject
from typing_extensions import Self

# explicit dependency on standard settings to make it loaded
from . import global_settings # noqa: F401
from . import global_settings

ENVIRONMENT_VARIABLE: Literal["DJANGO_SETTINGS_MODULE"]
DEFAULT_STORAGE_ALIAS: Literal["default"]
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/contrib/admin/decorators.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Sequence
from typing import Any, TypeVar, overload # noqa: Y037
from typing import Any, TypeVar, overload

from django.contrib.admin import ModelAdmin
from django.contrib.admin.sites import AdminSite
Expand All @@ -15,7 +15,7 @@ _ModelAdmin = TypeVar("_ModelAdmin", bound=ModelAdmin)
_Request = TypeVar("_Request", bound=HttpRequest)
_QuerySet = TypeVar("_QuerySet", bound=QuerySet)
# This is deliberately different from _DisplayT defined in contrib.admin.options
_DisplayCallable: TypeAlias = Callable[[_ModelAdmin, _Model], Any] | Callable[[_Model], Any] # noqa: Y037
_DisplayCallable: TypeAlias = Callable[[_ModelAdmin, _Model], Any] | Callable[[_Model], Any]
_DisplayCallableT = TypeVar("_DisplayCallableT", bound=_DisplayCallable)
_ActionReturn = TypeVar("_ActionReturn", bound=HttpResponseBase | None)

Expand Down
2 changes: 1 addition & 1 deletion django-stubs/contrib/admin/options.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from typing import Any, Generic, Literal, Optional, TypeVar # noqa: Y037 # https://github.com/python/mypy/issues/12211
from typing import Any, Generic, Literal, Optional, TypeVar # https://github.com/python/mypy/issues/12211
GabDug marked this conversation as resolved.
Show resolved Hide resolved

from django import forms
from django.contrib.admin.filters import FieldListFilter, ListFilter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getpass as getpass # noqa: F401
import getpass as getpass
from typing import Any

from django.core.management.base import BaseCommand
Expand Down
1 change: 0 additions & 1 deletion django-stubs/contrib/gis/db/models/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# noqa: F401
from django.contrib.gis.db.models.aggregates import *
from django.contrib.gis.db.models.fields import GeometryCollectionField as GeometryCollectionField
from django.contrib.gis.db.models.fields import GeometryField as GeometryField
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/contrib/gis/geos/io.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib.gis.geos.geometry import GEOSGeometry
from django.contrib.gis.geos.prototypes.io import WKBWriter as WKBWriter # noqa: F401
from django.contrib.gis.geos.prototypes.io import WKBWriter as WKBWriter
from django.contrib.gis.geos.prototypes.io import WKTWriter as WKTWriter
from django.contrib.gis.geos.prototypes.io import _WKBReader, _WKTReader

Expand Down
2 changes: 1 addition & 1 deletion django-stubs/core/validators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ EMPTY_VALUES: Any

_Regex: TypeAlias = str | Pattern[str]

_ValidatorCallable: TypeAlias = Callable[[Any], None] # noqa: Y047
_ValidatorCallable: TypeAlias = Callable[[Any], None] # noqa: PYI047

class RegexValidator:
regex: _Regex # Pattern[str] on instance, but may be str on class definition
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/db/models/fields/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _FieldDescriptor(Protocol[_F]):
@property
def field(self) -> _F: ...

_AllLimitChoicesTo: TypeAlias = _LimitChoicesTo | _ChoicesCallable # noqa: Y047
_AllLimitChoicesTo: TypeAlias = _LimitChoicesTo | _ChoicesCallable # noqa: PYI047
_ErrorMessagesMapping: TypeAlias = Mapping[str, _StrOrPromise]
_ErrorMessagesDict: TypeAlias = dict[str, _StrOrPromise]

Expand Down
6 changes: 2 additions & 4 deletions django-stubs/db/models/fields/related.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ from django.db.models.expressions import Combinable
from django.db.models.fields import Field, _AllLimitChoicesTo, _ErrorMessagesMapping, _FieldChoices, _LimitChoicesTo
from django.db.models.fields.mixins import FieldCacheMixin
from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor as ForwardManyToOneDescriptor
from django.db.models.fields.related_descriptors import ( # noqa: F401
ForwardOneToOneDescriptor as ForwardOneToOneDescriptor,
)
from django.db.models.fields.related_descriptors import ForwardOneToOneDescriptor as ForwardOneToOneDescriptor
from django.db.models.fields.related_descriptors import ManyToManyDescriptor as ManyToManyDescriptor
from django.db.models.fields.related_descriptors import ReverseManyToOneDescriptor as ReverseManyToOneDescriptor
from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor as ReverseOneToOneDescriptor
from django.db.models.fields.reverse_related import ForeignObjectRel as ForeignObjectRel # noqa: F401
from django.db.models.fields.reverse_related import ForeignObjectRel as ForeignObjectRel
from django.db.models.fields.reverse_related import ManyToManyRel as ManyToManyRel
from django.db.models.fields.reverse_related import ManyToOneRel as ManyToOneRel
from django.db.models.fields.reverse_related import OneToOneRel as OneToOneRel
Expand Down
10 changes: 2 additions & 8 deletions django-stubs/db/models/options.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from collections.abc import Iterable, Sequence
from typing import ( # noqa: Y037 # https://github.com/python/mypy/issues/12211
Any,
Generic,
Literal,
TypeVar,
overload,
)
from typing import Any, Generic, Literal, TypeVar, overload # https://github.com/python/mypy/issues/12211
GabDug marked this conversation as resolved.
Show resolved Hide resolved

from django.apps.config import AppConfig
from django.apps.registry import Apps
Expand All @@ -28,7 +22,7 @@ IMMUTABLE_WARNING: str
DEFAULT_NAMES: tuple[str, ...]

# https://github.com/python/mypy/issues/12211
_OptionTogetherT: TypeAlias = _ListOrTuple[_ListOrTuple[str] | str] | set[tuple[str, ...]] # noqa: Y047
_OptionTogetherT: TypeAlias = _ListOrTuple[_ListOrTuple[str] | str] | set[tuple[str, ...]] # noqa: PYI047

@overload
def normalize_together(option_together: _ListOrTuple[_ListOrTuple[str] | str]) -> tuple[tuple[str, ...], ...]: ...
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/db/models/query.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class RawQuerySet(Iterable[_T], Sized):
def resolve_model_init_order(self) -> tuple[list[str], list[int], list[tuple[str, int]]]: ...
def using(self, alias: str | None) -> RawQuerySet[_T]: ...

_QuerySetAny: TypeAlias = _QuerySet # noqa: Y047
_QuerySetAny: TypeAlias = _QuerySet # noqa: PYI047

QuerySet: TypeAlias = _QuerySet[_T, _T]

Expand Down
9 changes: 1 addition & 8 deletions django-stubs/forms/models.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
from collections.abc import Callable, Collection, Container, Iterator, Mapping, Sequence
from typing import ( # noqa: Y037 # https://github.com/python/mypy/issues/12211
Any,
ClassVar,
Generic,
Literal,
TypeVar,
overload,
)
from typing import Any, ClassVar, Generic, Literal, TypeVar, overload # https://github.com/python/mypy/issues/12211
GabDug marked this conversation as resolved.
Show resolved Hide resolved
from uuid import UUID

from django.db import models
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/forms/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ from django.utils.functional import _StrOrPromise
from django.utils.safestring import SafeString
from typing_extensions import TypeAlias

_DataT: TypeAlias = Mapping[str, Any] # noqa: Y047
_DataT: TypeAlias = Mapping[str, Any] # noqa: PYI047

_FilesT: TypeAlias = MultiValueDict[str, UploadedFile] # noqa: Y047
_FilesT: TypeAlias = MultiValueDict[str, UploadedFile] # noqa: PYI047

def pretty_name(name: str) -> str: ...
def flatatt(attrs: dict[str, Any]) -> SafeString: ...
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/template/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from logging import Logger
from re import Pattern
from typing import Any

from django.template.context import Context as Context # noqa: F401 # Django: imported for backwards compatibility
from django.template.context import Context as Context # Django: imported for backwards compatibility
from django.template.engine import Engine
from django.template.library import Library
from django.template.loaders.base import Loader
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/template/loader.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ from collections.abc import Mapping, Sequence
from typing import Any

from django.http.request import HttpRequest
from django.template.exceptions import TemplateDoesNotExist as TemplateDoesNotExist # noqa: F401
from django.template.exceptions import TemplateDoesNotExist as TemplateDoesNotExist
from django.utils.safestring import SafeString

from .backends.base import _EngineTemplate
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/template/response.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools
from collections.abc import Callable, Iterator, Sequence
from http.cookies import SimpleCookie
from typing import Any # noqa: Y037 # https://github.com/python/mypy/issues/12211
from typing import Any # https://github.com/python/mypy/issues/12211
GabDug marked this conversation as resolved.
Show resolved Hide resolved

from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/test/signals.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from django.core.signals import setting_changed as setting_changed # noqa: F401
from django.core.signals import setting_changed as setting_changed

template_rendered: Any
COMPLEX_OVERRIDE_SETTINGS: Any
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/test/testcases.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from typing import Any, overload
from django.core.exceptions import ImproperlyConfigured
from django.core.handlers.wsgi import WSGIHandler
from django.core.servers.basehttp import ThreadedWSGIServer, WSGIRequestHandler
from django.db import connections # noqa: F401
from django.db import connections
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.models.base import Model
from django.db.models.query import QuerySet, RawQuerySet
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/test/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestContextDecorator:
def __init__(self, attr_name: str | None = ..., kwarg_name: str | None = ...) -> None: ...
def enable(self) -> Any: ...
def disable(self) -> None: ...
def __enter__(self) -> Apps | None: ... # noqa: Y034
def __enter__(self) -> Apps | None: ...
def __exit__(
self,
exc_type: type[BaseException] | None,
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/urls/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ from .resolvers import get_resolver as get_resolver
from .utils import get_callable as get_callable
from .utils import get_mod_func as get_mod_func

_AnyURL: TypeAlias = URLPattern | URLResolver # noqa: Y047
_AnyURL: TypeAlias = URLPattern | URLResolver # noqa: PYI047
4 changes: 2 additions & 2 deletions django-stubs/utils/datastructures.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Collection, Iterable, Iterator, Mapping, MutableMapping, MutableSet
from typing import Any, Generic, NoReturn, Protocol, TypeVar, overload # noqa: Y022
from typing import Any, Generic, NoReturn, Protocol, TypeVar, overload

from _typeshed import Incomplete
from typing_extensions import Self, TypeAlias
Expand All @@ -11,7 +11,7 @@ _I = TypeVar("_I", covariant=True)

# Unfortunately, there's often check `if isinstance(var, (list, tuple))` in django
# codebase. So we need sometimes to declare exactly list or tuple.
_ListOrTuple: TypeAlias = list[_K] | tuple[_K, ...] | tuple[()] # noqa: Y047
_ListOrTuple: TypeAlias = list[_K] | tuple[_K, ...] | tuple[()] # noqa: PYI047

class _PropertyDescriptor(Generic[_K, _V]):
"""
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/utils/functional.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _StrPromise(Promise, Sequence[str]):
# Mypy requires this for the attribute hook to take effect
def __getattribute__(self, __name: str) -> Any: ...

_StrOrPromise: TypeAlias = str | _StrPromise # noqa: Y047
_StrOrPromise: TypeAlias = str | _StrPromise # noqa: PYI047
_C = TypeVar("_C", bound=Callable)

def lazy(func: _C, *resultclasses: Any) -> _C: ...
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/utils/translation/trans_real.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from gettext import NullTranslations
from re import Pattern

# switch to tuple once https://github.com/python/mypy/issues/11098 is fixed
from typing import Any, Literal, Protocol, TypeVar # noqa: Y022
from typing import Any, Literal, Protocol, TypeVar

from django.http.request import HttpRequest
from typing_extensions import TypeAlias
Expand Down
4 changes: 2 additions & 2 deletions mypy_django_plugin/django/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class ArrayField: # type: ignore


if TYPE_CHECKING:
from django.apps.registry import Apps # noqa: F401
from django.conf import LazySettings # noqa: F401
from django.apps.registry import Apps
from django.conf import LazySettings
from django.contrib.contenttypes.fields import GenericForeignKey


Expand Down
2 changes: 1 addition & 1 deletion mypy_django_plugin/transformers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
try:
related_manager_info = self.lookup_typeinfo_or_incomplete_defn_error(
fullnames.RELATED_MANAGER_CLASS
) # noqa: E501
)
default_manager = related_model_info.names.get("_default_manager")
if not default_manager:
raise helpers.IncompleteDefnException()
Expand Down
27 changes: 26 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ignore-words-list = "aadd,acount,nam"
[tool.ruff]
# Adds to default excludes: https://ruff.rs/docs/settings/#exclude
extend-exclude = [
".*/",
"django-source",
"stubgen",
"out",
Expand All @@ -17,11 +18,35 @@ line-length = 120
target-version = "py38"
# See Rules in Ruff documentation: https://beta.ruff.rs/docs/rules/
select = [
"B", # bugbear
"E", # pycodestyle
"F", # pyflakes
"INP", # flake8-tidy-imports
"W", # pycodestyle
"I", # isort
"F401", # Unused imports
"UP", # pyupgrade
"TID251", # Disallowed imports (flake8-tidy-imports.banned-api)
"PYI", # flake8-pyi
"RUF100", # Equivalent to flake8-noqa NQA103
"PGH004", # Equivalent to flake8-noqa NQA104
]
ignore = ["PYI021", "PYI024", "PYI041", "PYI043"]

[tool.ruff.per-file-ignores]
"*.pyi" = [
"B",
"E501",
"E741",
"E743",
"F401", # Unused import
GabDug marked this conversation as resolved.
Show resolved Hide resolved
"F403", # Use wildcard import
"F405",
"F822",
"F821",
]
"tests/*.py" = ["INP001"]
"ext/tests/*.py" = ["INP001"]
"setup.py" = ["INP001"]

[tool.ruff.flake8-tidy-imports.banned-api]
"_typeshed.Self".msg = "Use typing_extensions.Self (PEP 673) instead."
Expand Down
9 changes: 0 additions & 9 deletions setup.cfg

This file was deleted.