chore: Upgrade Python syntax with pyupgrade
Run with the following pre-commit configuration: ``` repos: - repo: https://github.com/asottile/pyupgrade rev: v3.1.0 hooks: - id: pyupgrade args: [ "--py3-plus", "--py36-plus", "--py37-plus" ] ``` The rationale for every change can be found at https://github.com/asottile/pyupgrade
This commit is contained in:
1 parent
92eab3c929
commit
bd4eb89b8c
29 files changed
+32
-65
No files matched your search
@@ -6,20 +6,20 @@ class Cache(dict):
|
||||
def __init__(self, django_folder, database, cache_path):
|
||||
self.filename = os.path.join(
|
||||
cache_path,
|
||||
"{0}_{1}.pickle".format(django_folder.replace(os.sep, "_"), database),
|
||||
"{}_{}.pickle".format(django_folder.replace(os.sep, "_"), database),
|
||||
)
|
||||
|
||||
if not os.path.exists(os.path.dirname(self.filename)):
|
||||
os.makedirs(os.path.dirname(self.filename))
|
||||
|
||||
super(Cache, self).__init__()
|
||||
super().__init__()
|
||||
|
||||
def load(self):
|
||||
try:
|
||||
with open(self.filename, "rb") as f:
|
||||
tmp_dict = pickle.load(f)
|
||||
self.update(tmp_dict)
|
||||
except IOError:
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def save(self):
|
||||
|
||||
@@ -19,7 +19,7 @@ from ..utils import (
|
||||
CONFIG_NAME = "django_migration_linter"
|
||||
PYPROJECT_TOML = "pyproject.toml"
|
||||
DEFAULT_CONFIG_FILES = (
|
||||
".{}.cfg".format(CONFIG_NAME),
|
||||
f".{CONFIG_NAME}.cfg",
|
||||
"setup.cfg",
|
||||
"tox.ini",
|
||||
)
|
||||
|
||||
@@ -34,7 +34,7 @@ class Command(MakeMigrationsCommand):
|
||||
help = "Creates new migration(s) for apps and lints them."
|
||||
|
||||
def add_arguments(self, parser):
|
||||
super(Command, self).add_arguments(parser)
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument(
|
||||
"--lint",
|
||||
action="store_true",
|
||||
@@ -49,10 +49,10 @@ class Command(MakeMigrationsCommand):
|
||||
self.warnings_as_errors = options["warnings_as_errors"]
|
||||
self.sql_analyser = options["sql_analyser"]
|
||||
configure_logging(options["verbosity"])
|
||||
return super(Command, self).handle(*app_labels, **options)
|
||||
return super().handle(*app_labels, **options)
|
||||
|
||||
def write_migration_files(self, changes):
|
||||
super(Command, self).write_migration_files(changes)
|
||||
super().write_migration_files(changes)
|
||||
|
||||
if (
|
||||
not getattr(settings, "MIGRATION_LINTER_OVERRIDE_MAKEMIGRATIONS", False)
|
||||
|
||||
@@ -36,7 +36,7 @@ class MessageType(Enum):
|
||||
return list(map(lambda c: c.value, MessageType))
|
||||
|
||||
|
||||
class MigrationLinter(object):
|
||||
class MigrationLinter:
|
||||
def __init__(
|
||||
self,
|
||||
path=None,
|
||||
@@ -263,17 +263,17 @@ class MigrationLinter(object):
|
||||
if lint_result.value in self.quiet:
|
||||
return
|
||||
if not self.no_output:
|
||||
print("({0}, {1})... {2}".format(app_label, migration_name, msg))
|
||||
print(f"({app_label}, {migration_name})... {msg}")
|
||||
|
||||
def print_errors(self, errors):
|
||||
if MessageType.ERROR.value in self.quiet:
|
||||
return
|
||||
for err in errors:
|
||||
error_str = "\t{0}".format(err["msg"])
|
||||
error_str = "\t{}".format(err["msg"])
|
||||
if err.get("table"):
|
||||
error_str += " (table: {0}".format(err["table"])
|
||||
error_str += " (table: {}".format(err["table"])
|
||||
if err.get("column"):
|
||||
error_str += ", column: {0}".format(err["column"])
|
||||
error_str += ", column: {}".format(err["column"])
|
||||
error_str += ")"
|
||||
if not self.no_output:
|
||||
print(error_str)
|
||||
@@ -291,10 +291,10 @@ class MigrationLinter(object):
|
||||
if self.no_output:
|
||||
return
|
||||
print("*** Summary ***")
|
||||
print("Valid migrations: {}/{}".format(self.nb_valid, self.nb_total))
|
||||
print("Erroneous migrations: {}/{}".format(self.nb_erroneous, self.nb_total))
|
||||
print("Migrations with warnings: {}/{}".format(self.nb_warnings, self.nb_total))
|
||||
print("Ignored migrations: {}/{}".format(self.nb_ignored, self.nb_total))
|
||||
print(f"Valid migrations: {self.nb_valid}/{self.nb_total}")
|
||||
print(f"Erroneous migrations: {self.nb_erroneous}/{self.nb_total}")
|
||||
print(f"Migrations with warnings: {self.nb_warnings}/{self.nb_total}")
|
||||
print(f"Ignored migrations: {self.nb_ignored}/{self.nb_total}")
|
||||
|
||||
@property
|
||||
def has_errors(self):
|
||||
@@ -302,7 +302,7 @@ class MigrationLinter(object):
|
||||
|
||||
def get_sql(self, app_label, migration_name):
|
||||
logger.info(
|
||||
"Calling sqlmigrate command {} {}".format(app_label, migration_name)
|
||||
f"Calling sqlmigrate command {app_label} {migration_name}"
|
||||
)
|
||||
dev_null = open(os.devnull, "w")
|
||||
try:
|
||||
@@ -327,7 +327,7 @@ class MigrationLinter(object):
|
||||
from django.db.migrations.loader import MIGRATIONS_MODULE_NAME
|
||||
|
||||
return (
|
||||
re.search(r"/{0}/.*\.py".format(MIGRATIONS_MODULE_NAME), filename)
|
||||
re.search(fr"/{MIGRATIONS_MODULE_NAME}/.*\.py", filename)
|
||||
and "__init__" not in filename
|
||||
)
|
||||
|
||||
@@ -343,12 +343,12 @@ class MigrationLinter(object):
|
||||
|
||||
migrations = []
|
||||
try:
|
||||
with open(migrations_file_path, "r") as file:
|
||||
with open(migrations_file_path) as file:
|
||||
for line in file:
|
||||
if cls.is_migration_file(line):
|
||||
app_label, name = split_migration_path(line)
|
||||
migrations.append((app_label, name))
|
||||
except IOError:
|
||||
except OSError:
|
||||
logger.exception("Migrations list path not found %s", migrations_file_path)
|
||||
raise Exception("Error while reading migrations list file")
|
||||
|
||||
@@ -363,9 +363,9 @@ class MigrationLinter(object):
|
||||
migrations = []
|
||||
# Get changes since specified commit
|
||||
git_diff_command = (
|
||||
"cd {0} && git diff --relative --name-only --diff-filter=AR {1}"
|
||||
"cd {} && git diff --relative --name-only --diff-filter=AR {}"
|
||||
).format(self.django_path, git_commit_id)
|
||||
logger.info("Executing {0}".format(git_diff_command))
|
||||
logger.info(f"Executing {git_diff_command}")
|
||||
diff_process = Popen(git_diff_command, shell=True, stdout=PIPE, stderr=PIPE)
|
||||
for line in map(clean_bytes_to_str, diff_process.stdout.readlines()):
|
||||
# Only gather lines that include added migrations
|
||||
@@ -537,7 +537,7 @@ class MigrationLinter(object):
|
||||
model = model.split(".", 1)[0]
|
||||
has_get_model_call = (
|
||||
re.search(
|
||||
r"{}.*= +\w+\.get_model\(".format(model),
|
||||
fr"{model}.*= +\w+\.get_model\(",
|
||||
source_code,
|
||||
)
|
||||
is not None
|
||||
|
||||
@@ -36,13 +36,13 @@ def has_add_unique(sql_statements, **kwargs):
|
||||
|
||||
concerned_table = regex_result.group(1)
|
||||
table_is_added_in_transaction = any(
|
||||
sql.startswith("CREATE TABLE {}".format(concerned_table))
|
||||
sql.startswith(f"CREATE TABLE {concerned_table}")
|
||||
for sql in sql_statements
|
||||
)
|
||||
return not table_is_added_in_transaction
|
||||
|
||||
|
||||
class BaseAnalyser(object):
|
||||
class BaseAnalyser:
|
||||
base_migration_tests = [
|
||||
{
|
||||
"code": "RENAME_TABLE",
|
||||
|
||||
@@ -16,7 +16,7 @@ def has_create_index(sql_statements, **kwargs):
|
||||
|
||||
concerned_table = regex_result.group(2)
|
||||
table_is_added_in_transaction = any(
|
||||
sql.startswith("CREATE TABLE {}".format(concerned_table))
|
||||
sql.startswith(f"CREATE TABLE {concerned_table}")
|
||||
for sql in sql_statements
|
||||
)
|
||||
return not table_is_added_in_transaction
|
||||
|
||||
@@ -38,7 +38,7 @@ def get_migration_abspath(app_label, migration_name):
|
||||
from django.db.migrations.loader import MigrationLoader
|
||||
|
||||
module_name, _ = MigrationLoader.migrations_module(app_label)
|
||||
migration_path = "{}.{}".format(module_name, migration_name)
|
||||
migration_path = f"{module_name}.{migration_name}"
|
||||
migration_module = import_module(migration_path)
|
||||
|
||||
migration_file = migration_module.__file__
|
||||
|
||||
Reference in new issue
Block a user