Add internal classes and add type hints + mypy.

This commit is contained in:
David Wobrock authored and David Wobrock committed 2022-12-26 16:26:24 +01:00
1 parent 847ac1241a
commit 2ee7dac3b3
88 files changed
+603 -340

No files matched your search

@@ -1,12 +1,15 @@
from __future__ import annotations
import configparser
import itertools
import os
import sys
from importlib import import_module
from typing import Any, Callable
import toml
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.management.base import BaseCommand, CommandParser
from ...constants import __version__
from ...migration_linter import MessageType, MigrationLinter
@@ -28,7 +31,7 @@ DEFAULT_CONFIG_FILES = (
class Command(BaseCommand):
help = "Lint your migrations"
def add_arguments(self, parser):
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument(
"app_label",
nargs="?",
@@ -184,7 +187,7 @@ class Command(BaseCommand):
sys.exit(1)
@staticmethod
def read_django_settings(options):
def read_django_settings(options: dict[str, Any]) -> dict[str, Any]:
django_settings_options = dict()
django_migration_linter_settings = getattr(
@@ -197,12 +200,13 @@ class Command(BaseCommand):
return django_settings_options
@staticmethod
def read_config_file(options):
def read_config_file(options: dict[str, Any]) -> dict[str, Any]:
config_options = dict()
config_parser = configparser.ConfigParser()
config_parser.read(DEFAULT_CONFIG_FILES, encoding="utf-8")
for key, value in options.items():
config_get_fn: Callable
if isinstance(value, bool):
config_get_fn = config_parser.getboolean
else:
@@ -214,7 +218,7 @@ class Command(BaseCommand):
return config_options
@staticmethod
def read_toml_file(options):
def read_toml_file(options: dict[str, Any]) -> dict[str, Any]:
toml_options = dict()
if os.path.exists(PYPROJECT_TOML):
@@ -226,5 +230,5 @@ class Command(BaseCommand):
return toml_options
def get_version(self):
def get_version(self) -> str:
return __version__
@@ -1,9 +1,13 @@
from __future__ import annotations
import os
from django.conf import settings
from django.core.management import CommandParser
from django.core.management.commands.makemigrations import (
Command as MakeMigrationsCommand,
)
from django.db.migrations import Migration
from django.db.migrations.questioner import InteractiveMigrationQuestioner
from django.db.migrations.writer import MigrationWriter
@@ -16,7 +20,7 @@ from ..utils import (
)
def ask_should_keep_migration():
def ask_should_keep_migration() -> bool:
questioner = InteractiveMigrationQuestioner()
msg = """
The migration linter detected that this migration is not backward compatible.
@@ -33,7 +37,7 @@ def default_should_keep_migration():
class Command(MakeMigrationsCommand):
help = "Creates new migration(s) for apps and lints them."
def add_arguments(self, parser):
def add_arguments(self, parser: CommandParser) -> None:
super().add_arguments(parser)
parser.add_argument(
"--lint",
@@ -51,7 +55,7 @@ class Command(MakeMigrationsCommand):
configure_logging(options["verbosity"])
return super().handle(*app_labels, **options)
def write_migration_files(self, changes):
def write_migration_files(self, changes: dict[str, list[Migration]]) -> None:
super().write_migration_files(changes)
if (
@@ -79,7 +83,7 @@ class Command(MakeMigrationsCommand):
all_warnings_as_errors,
) = extract_warnings_as_errors_option(self.warnings_as_errors)
# Lint migrations
# Lint migrations.
linter = MigrationLinter(
path=os.environ["DJANGO_SETTINGS_MODULE"],
database=self.database,
@@ -103,7 +107,7 @@ class Command(MakeMigrationsCommand):
self.delete_migration(migration)
linter.reset_counters()
def delete_migration(self, migration):
def delete_migration(self, migration: Migration) -> None:
writer = MigrationWriter(migration)
os.remove(writer.path)
+9 -3
View File
@@ -1,9 +1,13 @@
from __future__ import annotations
import logging
from django.core.management import CommandParser
from ..sql_analyser.analyser import ANALYSER_STRING_MAPPING
def register_linting_configuration_options(parser):
def register_linting_configuration_options(parser: CommandParser) -> None:
parser.add_argument(
"--database",
type=str,
@@ -37,7 +41,7 @@ def register_linting_configuration_options(parser):
)
def configure_logging(verbosity):
def configure_logging(verbosity: int) -> None:
logger = logging.getLogger("django_migration_linter")
if verbosity > 1:
@@ -48,7 +52,9 @@ def configure_logging(verbosity):
logging.basicConfig(format="%(message)s")
def extract_warnings_as_errors_option(warnings_as_errors):
def extract_warnings_as_errors_option(
warnings_as_errors: list[str] | None,
) -> tuple[list[str] | None, bool]:
if isinstance(warnings_as_errors, list):
warnings_as_errors_tests = warnings_as_errors
# If the option is specified but without any test codes,