From cbec8b4111306e5600b31b4dda18b7fc085fcabf Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 2 Sep 2024 10:36:43 +0300 Subject: [PATCH] tox: update ruff, mypy --- pyproject.toml | 2 +- pytest_django/asserts.py | 70 ++++++++++------------------ pytest_django/fixtures.py | 1 + pytest_django/lazy_django.py | 1 + pytest_django/plugin.py | 1 + tests/helpers.py | 3 +- tests/test_asserts.py | 1 + tests/test_django_configurations.py | 1 + tests/test_django_settings_module.py | 37 +++++++++------ tests/test_environment.py | 2 +- tests/test_fixtures.py | 1 + tox.ini | 6 +-- 12 files changed, 58 insertions(+), 68 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0d59ad44..bf236b70 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -144,7 +144,7 @@ ignore = [ "PT023", # Use `@pytest.mark.django_db()` over `@pytest.mark.django_db` ] -[tool.ruff.isort] +[tool.ruff.lint.isort] forced-separate = [ "tests", "pytest_django", diff --git a/pytest_django/asserts.py b/pytest_django/asserts.py index 9dc13783..14741066 100644 --- a/pytest_django/asserts.py +++ b/pytest_django/asserts.py @@ -1,6 +1,7 @@ """ Dynamically load all Django assertion cases and expose them for importing. """ + from __future__ import annotations from functools import wraps @@ -64,15 +65,13 @@ def assertRedirects( target_status_code: int = ..., msg_prefix: str = ..., fetch_redirect_response: bool = ..., - ) -> None: - ... + ) -> None: ... def assertURLEqual( url1: str, url2: str, msg_prefix: str = ..., - ) -> None: - ... + ) -> None: ... def assertContains( response: HttpResponseBase, @@ -81,8 +80,7 @@ def assertContains( status_code: int = ..., msg_prefix: str = ..., html: bool = False, - ) -> None: - ... + ) -> None: ... def assertNotContains( response: HttpResponseBase, @@ -90,16 +88,14 @@ def assertNotContains( status_code: int = ..., msg_prefix: str = ..., html: bool = False, - ) -> None: - ... + ) -> None: ... def assertFormError( form: forms.BaseForm, field: str | None, errors: str | Sequence[str], msg_prefix: str = ..., - ) -> None: - ... + ) -> None: ... def assertFormSetError( formset: forms.BaseFormSet, @@ -107,39 +103,34 @@ def assertFormSetError( field: str | None, errors: str | Sequence[str], msg_prefix: str = ..., - ) -> None: - ... + ) -> None: ... def assertTemplateUsed( response: HttpResponseBase | str | None = ..., template_name: str | None = ..., msg_prefix: str = ..., count: int | None = ..., - ): - ... + ): ... def assertTemplateNotUsed( response: HttpResponseBase | str | None = ..., template_name: str | None = ..., msg_prefix: str = ..., - ): - ... + ): ... def assertRaisesMessage( expected_exception: type[Exception], expected_message: str, *args, **kwargs, - ): - ... + ): ... def assertWarnsMessage( expected_warning: Warning, expected_message: str, *args, **kwargs, - ): - ... + ): ... def assertFieldOutput( fieldclass, @@ -148,58 +139,50 @@ def assertFieldOutput( field_args=..., field_kwargs=..., empty_value: str = ..., - ) -> None: - ... + ) -> None: ... def assertHTMLEqual( html1: str, html2: str, msg: str | None = ..., - ) -> None: - ... + ) -> None: ... def assertHTMLNotEqual( html1: str, html2: str, msg: str | None = ..., - ) -> None: - ... + ) -> None: ... def assertInHTML( needle: str, haystack: str, count: int | None = ..., msg_prefix: str = ..., - ) -> None: - ... + ) -> None: ... def assertJSONEqual( raw: str, expected_data: Any, msg: str | None = ..., - ) -> None: - ... + ) -> None: ... def assertJSONNotEqual( raw: str, expected_data: Any, msg: str | None = ..., - ) -> None: - ... + ) -> None: ... def assertXMLEqual( xml1: str, xml2: str, msg: str | None = ..., - ) -> None: - ... + ) -> None: ... def assertXMLNotEqual( xml1: str, xml2: str, msg: str | None = ..., - ) -> None: - ... + ) -> None: ... # Removed in Django 5.1: use assertQuerySetEqual. def assertQuerysetEqual( @@ -208,8 +191,7 @@ def assertQuerysetEqual( transform=..., ordered: bool = ..., msg: str | None = ..., - ) -> None: - ... + ) -> None: ... def assertQuerySetEqual( qs, @@ -217,8 +199,7 @@ def assertQuerySetEqual( transform=..., ordered: bool = ..., msg: str | None = ..., - ) -> None: - ... + ) -> None: ... def assertNumQueries( num: int, @@ -226,8 +207,7 @@ def assertNumQueries( *args, using: str = ..., **kwargs, - ): - ... + ): ... # Added in Django 5.0. def assertMessages( @@ -235,9 +215,7 @@ def assertMessages( expected_messages: Sequence[Message], *args, ordered: bool = ..., - ) -> None: - ... + ) -> None: ... # Fallback in case Django adds new asserts. - def __getattr__(name: str) -> Callable[..., Any]: - ... + def __getattr__(name: str) -> Callable[..., Any]: ... diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index fd922e9c..4e8d0a22 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -1,4 +1,5 @@ """All pytest-django fixtures""" + from __future__ import annotations import os diff --git a/pytest_django/lazy_django.py b/pytest_django/lazy_django.py index e599240b..b8a4b84f 100644 --- a/pytest_django/lazy_django.py +++ b/pytest_django/lazy_django.py @@ -1,6 +1,7 @@ """ Helpers to load Django lazily when Django settings can't be configured. """ + from __future__ import annotations import os diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 266e97a9..8b4aa2f7 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -3,6 +3,7 @@ This plugin handles creating and destroying the test environment and test database and provides some useful text fixtures. """ + from __future__ import annotations import contextlib diff --git a/tests/helpers.py b/tests/helpers.py index 36c75598..636185d9 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -10,8 +10,7 @@ def create_test_module( # type: ignore[empty-body] self, test_code: str, filename: str = ..., - ) -> Path: - ... + ) -> Path: ... def create_app_file(self, code: str, filename: str) -> Path: # type: ignore[empty-body] ... diff --git a/tests/test_asserts.py b/tests/test_asserts.py index 7a2db7dc..c9a01ec7 100644 --- a/tests/test_asserts.py +++ b/tests/test_asserts.py @@ -1,6 +1,7 @@ """ Tests the dynamic loading of all Django assertion cases. """ + from __future__ import annotations import inspect diff --git a/tests/test_django_configurations.py b/tests/test_django_configurations.py index e3bc6b26..88d89cf6 100644 --- a/tests/test_django_configurations.py +++ b/tests/test_django_configurations.py @@ -2,6 +2,7 @@ If these tests fail you probably forgot to install django-configurations. """ + import pytest diff --git a/tests/test_django_settings_module.py b/tests/test_django_settings_module.py index 51ca2780..449516a2 100644 --- a/tests/test_django_settings_module.py +++ b/tests/test_django_settings_module.py @@ -377,24 +377,31 @@ def test_django_debug_mode_keep( monkeypatch.delenv("DJANGO_SETTINGS_MODULE") pytester.makeini( """ - [pytest] - django_debug_mode = keep - """ + [pytest] + django_debug_mode = keep + """ ) pytester.makeconftest( - """ + f""" from django.conf import settings def pytest_configure(): - settings.configure(SECRET_KEY='set from pytest_configure', - DEBUG=%s, - DATABASES={'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': ':memory:'}}, - INSTALLED_APPS=['django.contrib.auth', - 'django.contrib.contenttypes',]) - """ - % settings_debug + settings.configure( + SECRET_KEY='set from pytest_configure', + DEBUG={settings_debug}, + DATABASES={ + { + 'default': {{ + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:'}} + } + }, + INSTALLED_APPS=[ + 'django.contrib.auth', + 'django.contrib.contenttypes', + ], + ) + """ ) pytester.makepyfile( @@ -402,7 +409,7 @@ def pytest_configure(): from django.conf import settings def test_debug_is_false(): assert settings.DEBUG is {settings_debug} - """ + """ ) r = pytester.runpytest_subprocess() @@ -414,7 +421,7 @@ def test_debug_is_false(): INSTALLED_APPS = [ 'tpkg.app.apps.TestApp', ] -""" + """ ) def test_django_setup_sequence(django_pytester) -> None: django_pytester.create_app_file( diff --git a/tests/test_environment.py b/tests/test_environment.py index 2a498de0..a3549732 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -154,7 +154,7 @@ def test_for_invalid_template(client): """ ) def test_invalid_template_variable_behaves_normally_when_ignored( - django_pytester: DjangoPytester + django_pytester: DjangoPytester, ) -> None: django_pytester.create_app_file( "
{{ invalid_var }}
", "templates/invalid_template_base.html" diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 20c907fc..ed7fcba2 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -3,6 +3,7 @@ Not quite all fixtures are tested here, the db and transactional_db fixtures are tested in test_database. """ + import socket from contextlib import contextmanager from typing import Generator diff --git a/tox.ini b/tox.ini index bc50b6b9..ffd82fd6 100644 --- a/tox.ini +++ b/tox.ini @@ -52,10 +52,10 @@ commands = [testenv:linting] extras = deps = - ruff==0.1.3 - mypy==1.6.1 + ruff==0.6.3 + mypy==1.11.2 commands = - ruff check --statistics {posargs:pytest_django pytest_django_test tests} + ruff check {posargs:pytest_django pytest_django_test tests} ruff format --quiet --diff {posargs:pytest_django pytest_django_test tests} mypy {posargs:pytest_django pytest_django_test tests}