From 1d763a66597df0ed2510ef859867651889efe55f Mon Sep 17 00:00:00 2001 From: chillaranand Date: Fri, 13 May 2022 15:51:58 +0530 Subject: [PATCH] refactor: Fix flake8 issues --- frappe/contacts/doctype/address/address.py | 1 - frappe/custom/doctype/customize_form/customize_form.py | 5 ++++- frappe/database/postgres/setup_db.py | 2 +- frappe/email/doctype/newsletter/newsletter.py | 4 ++-- frappe/model/db_query.py | 8 ++++---- frappe/model/delete_doc.py | 2 +- frappe/model/meta.py | 5 ++++- frappe/patches/v14_0/remove_db_aggregation.py | 2 +- frappe/utils/background_jobs.py | 4 ++-- frappe/utils/backups.py | 8 ++++---- frappe/utils/error.py | 2 +- frappe/utils/global_search.py | 8 ++++---- frappe/utils/print_format.py | 2 -- 13 files changed, 28 insertions(+), 25 deletions(-) diff --git a/frappe/contacts/doctype/address/address.py b/frappe/contacts/doctype/address/address.py index d9ba31d474..c7564e8866 100644 --- a/frappe/contacts/doctype/address/address.py +++ b/frappe/contacts/doctype/address/address.py @@ -268,7 +268,6 @@ def address_query(doctype, txt, searchfield, start, page_len, filters): `tabAddress`.idx desc, `tabAddress`.name limit %(start)s, %(page_len)s """.format( mcond=get_match_cond(doctype), - key=searchfield, search_condition=search_condition, condition=condition or "", ), diff --git a/frappe/custom/doctype/customize_form/customize_form.py b/frappe/custom/doctype/customize_form/customize_form.py index e92fd50ea8..20c3a7c025 100644 --- a/frappe/custom/doctype/customize_form/customize_form.py +++ b/frappe/custom/doctype/customize_form/customize_form.py @@ -524,7 +524,10 @@ class CustomizeForm(Document): """allow type change, if both old_type and new_type are in same field group. field groups are defined in ALLOWED_FIELDTYPE_CHANGE variables. """ - in_field_group = lambda group: (old_type in group) and (new_type in group) + + def in_field_group(group): + return (old_type in group) and (new_type in group) + return any(map(in_field_group, ALLOWED_FIELDTYPE_CHANGE)) diff --git a/frappe/database/postgres/setup_db.py b/frappe/database/postgres/setup_db.py index 90d5f72c16..5584c098ce 100644 --- a/frappe/database/postgres/setup_db.py +++ b/frappe/database/postgres/setup_db.py @@ -114,7 +114,7 @@ def drop_user_and_database(db_name, root_login, root_password): ) root_conn.commit() root_conn.sql( - f"SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = %s", + "SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = %s", (db_name,), ) root_conn.sql(f"DROP DATABASE IF EXISTS {db_name}") diff --git a/frappe/email/doctype/newsletter/newsletter.py b/frappe/email/doctype/newsletter/newsletter.py index b04ad4db40..18ca440738 100644 --- a/frappe/email/doctype/newsletter/newsletter.py +++ b/frappe/email/doctype/newsletter/newsletter.py @@ -237,7 +237,7 @@ def confirmed_unsubscribe(email, group): @frappe.whitelist(allow_guest=True) -def subscribe(email, email_group=_("Website")): +def subscribe(email, email_group=_("Website")): # noqa """API endpoint to subscribe an email to a particular email group. Triggers a confirmation email.""" # build subscription confirmation URL @@ -282,7 +282,7 @@ def subscribe(email, email_group=_("Website")): @frappe.whitelist(allow_guest=True) -def confirm_subscription(email, email_group=_("Website")): +def confirm_subscription(email, email_group=_("Website")): # noqa """API endpoint to confirm email subscription. This endpoint is called when user clicks on the link sent to their mail. """ diff --git a/frappe/model/db_query.py b/frappe/model/db_query.py index 005b7e3741..334a3208c6 100644 --- a/frappe/model/db_query.py +++ b/frappe/model/db_query.py @@ -982,11 +982,11 @@ def is_parent_only_filter(doctype, filters): only_parent_doctype = True if isinstance(filters, list): - for flt in filters: - if doctype not in flt: + for filter in filters: + if doctype not in filter: only_parent_doctype = False - if "Between" in flt: - flt[3] = get_between_date_filter(flt[3]) + if "Between" in filter: + filter[3] = get_between_date_filter(flt[3]) return only_parent_doctype diff --git a/frappe/model/delete_doc.py b/frappe/model/delete_doc.py index 733e8ca367..2eccc1e717 100644 --- a/frappe/model/delete_doc.py +++ b/frappe/model/delete_doc.py @@ -106,7 +106,7 @@ def delete_doc( ): try: delete_controllers(name, doc.module) - except (FileNotFoundError, OSError, KeyError): + except (OSError, KeyError): # in case a doctype doesnt have any controller code nor any app and module pass diff --git a/frappe/model/meta.py b/frappe/model/meta.py index aeb12136ef..4f7dc01ea4 100644 --- a/frappe/model/meta.py +++ b/frappe/model/meta.py @@ -774,7 +774,10 @@ def trim_table(doctype, dry_run=True): ignore_fields = default_fields + optional_fields + child_table_fields columns = frappe.db.get_table_columns(doctype) fields = frappe.get_meta(doctype, cached=False).get_fieldnames_with_value() - is_internal = lambda f: f not in ignore_fields and not f.startswith("_") + + def is_internal(field): + return field not in ignore_fields and not field.startswith("_") + columns_to_remove = [f for f in list(set(columns) - set(fields)) if is_internal(f)] DROPPED_COLUMNS = columns_to_remove[:] diff --git a/frappe/patches/v14_0/remove_db_aggregation.py b/frappe/patches/v14_0/remove_db_aggregation.py index 6dc34a784b..4b0a58c2d6 100644 --- a/frappe/patches/v14_0/remove_db_aggregation.py +++ b/frappe/patches/v14_0/remove_db_aggregation.py @@ -30,6 +30,6 @@ def execute(): name, script = server_script["name"], server_script["script"] for agg in ["avg", "max", "min", "sum"]: - script = re.sub(f"frappe.db.{agg}\(", f"frappe.qb.{agg}(", script) + script = re.sub(f"frappe.db.{agg}\\(", f"frappe.qb.{agg}(", script) frappe.db.update("Server Script", name, "script", script) diff --git a/frappe/utils/background_jobs.py b/frappe/utils/background_jobs.py index bc89e5279e..ce8e44665a 100755 --- a/frappe/utils/background_jobs.py +++ b/frappe/utils/background_jobs.py @@ -227,8 +227,8 @@ def get_jobs(site=None, queue=None, key="method"): # optional keyword arguments are stored in 'kwargs' of 'kwargs' jobs_per_site[job.kwargs["site"]].append(job.kwargs["kwargs"][key]) - for queue in get_queue_list(queue): - q = get_queue(queue) + for _queue in get_queue_list(queue): + q = get_queue(_queue) jobs = q.jobs + get_running_jobs_in_queue(q) for job in jobs: if job.kwargs.get("site"): diff --git a/frappe/utils/backups.py b/frappe/utils/backups.py index 927ae9c2db..85826b4ce7 100644 --- a/frappe/utils/backups.py +++ b/frappe/utils/backups.py @@ -654,14 +654,14 @@ class Backup: print("Invalid path", self.file_path) return else: - os.rename(self.file_path, self.file_path + ".gpg") - file_path = self.file_path + ".gpg" + file_path_with_ext = self.file_path + ".gpg" + os.rename(self.file_path, file_path_with_ext) cmd_string = "gpg --yes --passphrase {passphrase} --pinentry-mode loopback -o {decrypted_file} -d {file_location}" command = cmd_string.format( passphrase=passphrase, - file_location=file_path, - decrypted_file=file_path.rstrip(".gpg"), + file_location=file_path_with_ext, + decrypted_file=self.file_path, ) frappe.utils.execute_in_shell(command) diff --git a/frappe/utils/error.py b/frappe/utils/error.py index 26a05e9443..bc369eccad 100644 --- a/frappe/utils/error.py +++ b/frappe/utils/error.py @@ -84,7 +84,7 @@ def get_snapshot(exception, context=10): # basic frame information f = {"file": file, "func": func, "call": call, "lines": {}, "lnum": lnum} - def reader(lnum=[lnum]): + def reader(lnum=[lnum]): # noqa try: return linecache.getline(file, lnum[0]) finally: diff --git a/frappe/utils/global_search.py b/frappe/utils/global_search.py index b121a9b46f..6e482baa78 100644 --- a/frappe/utils/global_search.py +++ b/frappe/utils/global_search.py @@ -447,13 +447,13 @@ def search(text, start=0, limit=20, doctype=""): allowed_doctypes = get_doctypes_for_global_search() - for text in set(text.split("&")): - text = text.strip() - if not text: + for word in set(text.split("&")): + word = word.strip() + if not word: continue global_search = frappe.qb.Table("__global_search") - rank = Match(global_search.content).Against(text).as_("rank") + rank = Match(global_search.content).Against(word).as_("rank") query = ( frappe.qb.from_(global_search) .select(global_search.doctype, global_search.name, global_search.content, rank) diff --git a/frappe/utils/print_format.py b/frappe/utils/print_format.py index 87fb646f47..b6d0cb8f8e 100644 --- a/frappe/utils/print_format.py +++ b/frappe/utils/print_format.py @@ -165,5 +165,3 @@ def print_by_server( frappe.throw(_("PDF generation failed")) except cups.IPPError: frappe.throw(_("Printing failed")) - finally: - return