Skip to content

Update tests further for Mypy strict mode #942

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

Merged
merged 3 commits into from
Aug 28, 2022
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
4 changes: 4 additions & 0 deletions requirements/py310-django32.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,7 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
4 changes: 4 additions & 0 deletions requirements/py310-django40.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
4 changes: 4 additions & 0 deletions requirements/py310-django41.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
4 changes: 4 additions & 0 deletions requirements/py311-django41.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,7 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
1 change: 1 addition & 0 deletions requirements/py37-django32.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via
# -r requirements.in
# asgiref
# importlib-metadata
zipp==3.8.1 \
Expand Down
4 changes: 4 additions & 0 deletions requirements/py38-django32.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
zipp==3.8.1 \
--hash=sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2 \
--hash=sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009
Expand Down
4 changes: 4 additions & 0 deletions requirements/py38-django40.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
zipp==3.8.1 \
--hash=sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2 \
--hash=sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009
Expand Down
4 changes: 4 additions & 0 deletions requirements/py38-django41.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
zipp==3.8.1 \
--hash=sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2 \
--hash=sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009
Expand Down
4 changes: 4 additions & 0 deletions requirements/py39-django32.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
zipp==3.8.1 \
--hash=sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2 \
--hash=sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009
Expand Down
4 changes: 4 additions & 0 deletions requirements/py39-django40.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
zipp==3.8.1 \
--hash=sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2 \
--hash=sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009
Expand Down
4 changes: 4 additions & 0 deletions requirements/py39-django41.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
typing-extensions==4.3.0 \
--hash=sha256:25642c956049920a5aa49edcdd6ab1e06d7e5d467fc00e0506c44ac86fbfca02 \
--hash=sha256:e6d2677a32f47fc7eb2795db1dd15c1f34eff616bcaf2cfb5e997f854fa1c4a6
# via -r requirements.in
zipp==3.8.1 \
--hash=sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2 \
--hash=sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pytest
pytest-django
pytest-flake8-path
pytest-randomly
typing-extensions
5 changes: 3 additions & 2 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
SizedBinaryField,
SizedTextField,
)
from tests.testapp.utils import conn_is_mysql


class EnumModel(Model):
Expand Down Expand Up @@ -122,7 +123,7 @@ class DynamicModel(Model):
@classmethod
def check(cls, **kwargs: Any) -> list[checks.CheckMessage]:
# Disable the checks on MySQL so that checks tests don't fail
if not connection.mysql_is_mariadb:
if conn_is_mysql(connection) and not connection.mysql_is_mariadb:
return []
return super().check(**kwargs)

Expand All @@ -136,7 +137,7 @@ class SpeclessDynamicModel(Model):
@classmethod
def check(cls, **kwargs):
# Disable the checks on MySQL so that checks tests don't fail
if not connection.mysql_is_mariadb:
if conn_is_mysql(connection) and not connection.mysql_is_mariadb:
return []
return super().check(**kwargs)

Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/test_listcharfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_char_lookup_contains(self):
def test_char_lookup_icontains(self):
self.check_char_lookup("icontains")

def check_char_lookup(self, lookup):
def check_char_lookup(self, lookup: str) -> None:
lname = "field__" + lookup
mymodel = CharListModel.objects.create(field=["mouldy", "rotten"])

Expand Down
5 changes: 3 additions & 2 deletions tests/testapp/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from django_mysql.operations import AlterStorageEngine, InstallPlugin, InstallSOName
from django_mysql.test.utils import override_mysql_variables
from tests.testapp.utils import conn_is_mysql


def plugin_exists(plugin_name: str) -> bool:
Expand Down Expand Up @@ -40,9 +41,9 @@ class PluginOperationTests(TransactionTestCase):
databases = {"default", "other"}

@classmethod
def setUpClass(cls):
def setUpClass(cls) -> None:
super().setUpClass()
if not connection.mysql_is_mariadb:
if not conn_is_mysql(connection) or not connection.mysql_is_mariadb:
raise SkipTest("The metadata_lock_info plugin is required")

def test_install_plugin_describe(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/test_rewrite_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_non_select_update_deletes_ignored(self):
== "SHOW TABLES "
)

def check_identity(self, query):
def check_identity(self, query: str) -> None:
assert rewrite_query(query) == query

def test_straight_join(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/testapp/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class OverrideVarsMethodTest(TestCase):
def test_method_decorator(self):
self.check_timestamp(123)

def check_timestamp(self, expected, using="default"):
def check_timestamp(self, expected: int, using: str = "default") -> None:
with connections[using].cursor() as cursor:
cursor.execute("SELECT @@TIMESTAMP")
mode = cursor.fetchone()[0]
mode: int = cursor.fetchone()[0]

assert mode == expected

Expand Down
60 changes: 43 additions & 17 deletions tests/testapp/utils.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,95 @@
from __future__ import annotations

import sys
from contextlib import contextmanager
from typing import Any
from types import TracebackType
from typing import Any, Generator

import pytest
from django.db import DEFAULT_DB_ALIAS, connection, connections
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper
from django.db.backends.utils import CursorWrapper
from django.test.utils import CaptureQueriesContext

if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from typing_extensions import TypeGuard


def conn_is_mysql(connection: BaseDatabaseWrapper) -> TypeGuard[MySQLDatabaseWrapper]:
return connection.vendor == "mysql"


@contextmanager
def skip_if_mysql_8_plus():
if not connection.mysql_is_mariadb and connection.mysql_version >= (8,):
def skip_if_mysql_8_plus() -> Generator[None, None, None]:
if not conn_is_mysql(connection) or (
not connection.mysql_is_mariadb and connection.mysql_version >= (8,)
):
pytest.skip("Requires MySQL<8 or MariaDB")
yield


def column_type(table_name, column_name):
def column_type(table_name: str, column_name: str) -> str:
with connection.cursor() as cursor:
cursor.execute(
"""SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s AND
COLUMN_NAME = %s""",
(table_name, column_name),
)
return cursor.fetchone()[0]
type_: str = cursor.fetchone()[0]
return type_


class CaptureLastQuery:
def __init__(self, conn=None):
def __init__(self, conn: BaseDatabaseWrapper | None = None) -> None:
if conn is None: # pragma: no branch
conn = connection
self.conn = conn
self.conn: BaseDatabaseWrapper = conn

def __enter__(self):
def __enter__(self) -> CaptureLastQuery:
self.capturer = CaptureQueriesContext(self.conn)
self.capturer.__enter__()
return self

def __exit__(self, a, b, c):
self.capturer.__exit__(a, b, c)
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
self.capturer.__exit__(exc_type, exc_val, exc_tb)

@property
def query(self):
def query(self) -> str:
return self.capturer.captured_queries[-1]["sql"]


class print_all_queries:
def __init__(self, conn=None):
def __init__(self, conn: BaseDatabaseWrapper | None = None) -> None:
if conn is None: # pragma: no branch
conn = connection
self.conn = conn
self.conn: BaseDatabaseWrapper = conn

def __enter__(self):
def __enter__(self) -> print_all_queries:
self.capturer = CaptureQueriesContext(self.conn)
self.capturer.__enter__()
return self

def __exit__(self, a, b, c):
self.capturer.__exit__(a, b, c)
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
self.capturer.__exit__(exc_type, exc_val, exc_tb)
for q in self.capturer.captured_queries:
print(q["sql"])


def used_indexes(query, using=DEFAULT_DB_ALIAS):
def used_indexes(query: str, using: str = DEFAULT_DB_ALIAS) -> set[str]:
"""
Given SQL 'query', run EXPLAIN and return the names of the indexes used
"""
Expand Down