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

Set up ruff #34

Merged
merged 2 commits into from
Sep 4, 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
8 changes: 0 additions & 8 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: isort
uses: jamescurtin/isort-action@master
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements/*.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Install Poetry
uses: snok/install-poetry@v1
Expand Down
40 changes: 5 additions & 35 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,12 @@ repos:
- id: check-merge-conflict
- id: detect-private-key

- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.2
hooks:
- id: isort
name: isort (python)

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
exclude: node_modules|migrations|scripts|.venv|__init__.py
additional_dependencies: [
# A flake8 plugin that checks django code style.
# https://github.com/rocioar/flake8-django
flake8-django,
# required by flake8-django
django,
django_extensions,
# A plugin for Flake8 finding likely bugs and design problems in your program.
# https://github.com/PyCQA/flake8-bugbear
flake8-bugbear,
# A flake8 plugin checking common style issues or inconsistencies with pytest-based tests.
# https://github.com/m-burst/flake8-pytest-style
flake8-pytest-style,
# A flake8 plugin that warn about backslashes usage.
# https://github.com/wemake-services/flake8-broken-line
flake8-broken-line,
# A simple module that adds an extension for the fantastic pydocstyle tool to flake8.
# https://github.com/PyCQA/flake8-docstrings
flake8-docstrings,
# McCabe complexity checker.
# https://github.com/PyCQA/mccabe
mccabe,
# A flake8 plug-in loading the configuration from pyproject.toml
flake8-pyproject,
]
- id: ruff
args: [ --fix ]
- id: ruff-format

- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
Expand Down
14 changes: 9 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import os
import pathlib
import sys

import django

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.append(os.path.abspath("."))
sys.path.append(os.path.abspath(".."))
sys.path.append(os.path.abspath("../tests"))
sys.path.append(str(pathlib.Path.cwd()))
sys.path.append(str(pathlib.Path("..").resolve()))
sys.path.append(str(pathlib.Path("../tests").resolve()))
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

django.setup()
Expand Down Expand Up @@ -106,7 +107,7 @@
]


# -- Options for Texinfo output ------------------------------------------------
# -- Options for Texinfo output -----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
Expand All @@ -129,5 +130,8 @@
# intersphinx documentation
intersphinx_mapping = {
"tablib": ("https://tablib.readthedocs.io/en/stable/", None),
"django-import-export": ("https://django-import-export.readthedocs.io/en/latest/", None),
"django-import-export": (
"https://django-import-export.readthedocs.io/en/latest/",
None,
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class ForceImportForm(base_forms.ImportForm):
"""Import form with `force_import` option."""

force_import = forms.BooleanField(
required=False,
initial=False,
Expand Down
24 changes: 14 additions & 10 deletions import_export_extensions/admin/mixins/export_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,24 @@ class CeleryExportAdminMixin(
If errors - traceback and error message.

"""

# export data encoding
to_encoding = "utf-8"

# template used to display ExportForm
celery_export_template_name = "admin/import_export/export.html"

export_status_template_name = "admin/import_export_extensions/celery_export_status.html"
export_status_template_name = (
"admin/import_export_extensions/celery_export_status.html"
)

export_results_template_name = "admin/import_export_extensions/celery_export_results.html"
export_results_template_name = (
"admin/import_export_extensions/celery_export_results.html"
)

import_export_change_list_template = "admin/import_export/change_list_export.html"
import_export_change_list_template = (
"admin/import_export/change_list_export.html"
)

# Statuses that should be displayed on 'results' page
export_results_statuses = models.ExportJob.export_finished_statuses
Expand Down Expand Up @@ -262,11 +269,8 @@ def create_export_job(
job = models.ExportJob.objects.create(
resource_path=resource_class.class_path,
resource_kwargs=resource_kwargs,
file_format_path=".".join(
[
file_format.__module__,
file_format.__name__,
],
file_format_path=(
f"{file_format.__module__}.{file_format.__name__}"
),
)
return job
Expand All @@ -278,7 +282,7 @@ def get_export_job(
) -> models.ExportJob:
"""Get ExportJob instance.

Raises:
Raises
Http404

"""
Expand Down Expand Up @@ -317,7 +321,7 @@ def _redirect_to_export_results_page(
def changelist_view(
self,
request: WSGIRequest,
context: typing.Optional[dict[str, typing.Any]] = None,
context: dict[str, typing.Any] | None = None,
):
"""Add the check for permission to changelist template context."""
context = context or {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ class CeleryImportExportMixin(
"""Import and export mixin."""

# template for change_list view
import_export_change_list_template = "admin/import_export/change_list_import_export.html"
import_export_change_list_template = (
"admin/import_export/change_list_import_export.html"
)
25 changes: 18 additions & 7 deletions import_export_extensions/admin/mixins/import_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CeleryImportAdminMixin(
with progress bar and import totals.

"""

# Import data encoding
from_encoding = "utf-8"

Expand All @@ -59,12 +60,18 @@ class CeleryImportAdminMixin(
celery_import_template = "admin/import_export/import.html"

# Template used to display status of import jobs
import_status_template = "admin/import_export_extensions/celery_import_status.html"
import_status_template = (
"admin/import_export_extensions/celery_import_status.html"
)

# template used to display results of import jobs
import_result_template_name = "admin/import_export_extensions/celery_import_results.html"
import_result_template_name = (
"admin/import_export_extensions/celery_import_results.html"
)

import_export_change_list_template = "admin/import_export/change_list_import.html"
import_export_change_list_template = (
"admin/import_export/change_list_import.html"
)

skip_admin_log = None
# Copy methods of mixin from original package to reuse it here
Expand Down Expand Up @@ -323,7 +330,11 @@ def create_import_job(
data_file=form.cleaned_data["import_file"],
resource_kwargs=resource.resource_init_kwargs,
created_by=request.user,
skip_parse_step=getattr(settings, "IMPORT_EXPORT_SKIP_ADMIN_CONFIRM", False),
skip_parse_step=getattr(
settings,
"IMPORT_EXPORT_SKIP_ADMIN_CONFIRM",
False,
),
force_import=form.cleaned_data["force_import"],
)

Expand All @@ -334,7 +345,7 @@ def get_import_job(
) -> models.ImportJob:
"""Get ImportJob instance.

Raises:
Raises
Http404

"""
Expand Down Expand Up @@ -366,7 +377,7 @@ def _redirect_to_results_page(
url = reverse(url_name, kwargs=dict(job_id=job.id))
query = request.GET.urlencode()
url = f"{url}?{query}" if query else url
if not job.import_status == models.ImportJob.ImportStatus.PARSED:
if job.import_status != models.ImportJob.ImportStatus.PARSED:
return HttpResponseRedirect(redirect_to=url)

# Redirections add one by one links to `redirect_to`
Expand All @@ -388,7 +399,7 @@ def _redirect_to_results_page(
def changelist_view(
self,
request: WSGIRequest,
context: typing.Optional[dict[str, typing.Any]] = None,
context: dict[str, typing.Any] | None = None,
):
"""Add the check for permission to changelist template context."""
context = context or {}
Expand Down
10 changes: 3 additions & 7 deletions import_export_extensions/admin/mixins/types.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import dataclasses
import typing

from django.db.models.options import Options

from import_export.formats import base_formats

from ... import resources

ResourceObj = typing.Union[
resources.CeleryResource,
resources.CeleryModelResource,
]
ResourceType = typing.Type[ResourceObj]
FormatType = typing.Type[base_formats.Format]
ResourceObj = resources.CeleryResource | resources.CeleryModelResource
ResourceType = type[ResourceObj]
FormatType = type[base_formats.Format]


@dataclasses.dataclass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing

from django.contrib import admin, messages
from django.core.handlers.wsgi import WSGIRequest
Expand Down Expand Up @@ -150,7 +149,7 @@ def get_readonly_fields(self, request, obj=None):
def get_fieldsets(
self,
request: WSGIRequest,
obj: typing.Optional[models.ExportJob] = None,
obj: models.ExportJob | None = None,
):
"""Get fieldsets depending on object status."""
status = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing

from django.contrib import admin, messages
from django.core.handlers.wsgi import WSGIRequest
Expand Down Expand Up @@ -118,7 +117,7 @@ def import_job_progress_view(
def get_readonly_fields(
self,
request: WSGIRequest,
obj: typing.Optional[models.ImportJob] = None,
obj: models.ImportJob | None = None,
) -> list[str]:
"""Return readonly fields.

Expand Down Expand Up @@ -151,7 +150,7 @@ def get_readonly_fields(

def _show_results(
self,
obj: typing.Optional[models.ImportJob] = None,
obj: models.ImportJob | None = None,
) -> str:
"""Show results totals.

Expand Down Expand Up @@ -189,7 +188,7 @@ def _input_errors(self, job: models.ImportJob):
def get_fieldsets(
self,
request: WSGIRequest,
obj: typing.Optional[models.ImportJob] = None,
obj: models.ImportJob | None = None,
):
"""Get fieldsets depending on object status."""
status = (
Expand Down
5 changes: 2 additions & 3 deletions import_export_extensions/admin/model_admins/mixins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import typing

from django.contrib.contenttypes.models import ContentType
from django.core.handlers.wsgi import WSGIRequest
Expand Down Expand Up @@ -45,8 +44,8 @@ def _model(self, obj: models.ImportJob) -> str:

def get_from_content_type(
self,
obj: typing.Union[models.ImportJob, models.ExportJob],
) -> typing.Union[ContentType, None]:
obj: models.ImportJob | models.ExportJob,
) -> ContentType | None:
"""Shortcut to get object from content_type."""
content_type = obj.resource_kwargs.get("content_type")
obj_id = obj.resource_kwargs.get("object_id")
Expand Down
16 changes: 8 additions & 8 deletions import_export_extensions/api/serializers/export_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ExportProgressSerializer(ProgressSerializer):
"""Serializer to show ExportJob progress."""

state = serializers.ChoiceField(
choices=models.ExportJob.ExportStatus.values
+ [
choices=[
*models.ExportJob.ExportStatus.values,
states.PENDING,
states.STARTED,
states.SUCCESS,
Expand Down Expand Up @@ -48,13 +48,13 @@ class CreateExportJob(serializers.Serializer):

"""

resource_class: typing.Type[resources.CeleryModelResource]
resource_class: type[resources.CeleryModelResource]

def __init__(
self,
*args,
filter_kwargs: typing.Optional[dict[str, typing.Any]] = None,
resource_kwargs: typing.Optional[dict[str, typing.Any]] = None,
filter_kwargs: dict[str, typing.Any] | None = None,
resource_kwargs: dict[str, typing.Any] | None = None,
**kwargs,
):
"""Set filter kwargs and current user."""
Expand Down Expand Up @@ -99,14 +99,14 @@ def update(self, instance, validated_data):


def get_create_export_job_serializer(
resource: typing.Type[resources.CeleryModelResource],
) -> typing.Type: # type: ignore
resource: type[resources.CeleryModelResource],
) -> type:
"""Create serializer for ExportJobs creation."""

class _CreateExportJob(CreateExportJob):
"""Serializer to start export job."""

resource_class: typing.Type[resources.CeleryModelResource] = resource
resource_class: type[resources.CeleryModelResource] = resource
file_format = serializers.ChoiceField(
required=True,
choices=[
Expand Down
Loading
Loading