From 350b5b035ac32b9f12bc6df7045b65f8ce27561a Mon Sep 17 00:00:00 2001 From: Teal Dulcet Date: Wed, 8 Jan 2025 05:22:41 -0800 Subject: [PATCH] Fixed EM102 (f-string-in-exception): Exception must not use an f-string literal, assign to variable first --- management/dns_update.py | 15 ++++++++++----- management/mail_log.py | 3 ++- management/mailconfig.py | 3 ++- tests/fail2ban.py | 3 ++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/management/dns_update.py b/management/dns_update.py index 840b98ad..b874624e 100755 --- a/management/dns_update.py +++ b/management/dns_update.py @@ -590,7 +590,8 @@ def get_dns_zonefile(zone, env): if zone == domain: break else: - raise ValueError(f"{zone} is not a domain name that corresponds to a zone.") + msg = f"{zone} is not a domain name that corresponds to a zone." + raise ValueError(msg) nsd_zonefile = "/etc/nsd/zones/" + fn with open(nsd_zonefile, encoding="utf-8") as f: @@ -896,7 +897,8 @@ def set_custom_dns_record(qname, rtype, value, action, env): else: # No match. if qname != "_secondary_nameserver": - raise ValueError(f"{qname} is not a domain name or a subdomain of a domain name managed by this box.") + msg = f"{qname} is not a domain name or a subdomain of a domain name managed by this box." + raise ValueError(msg) # validate rtype rtype = rtype.upper() @@ -926,7 +928,8 @@ def set_custom_dns_record(qname, rtype, value, action, env): # anything goes pass else: - raise ValueError(f"Unknown record type '{rtype}'.") + msg = f"Unknown record type '{rtype}'." + raise ValueError(msg) # load existing config config = list(get_custom_dns_config(env)) @@ -1037,7 +1040,8 @@ def set_secondary_dns(hostnames, env): try: resolver.resolve(item, "AAAA") except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.Timeout): - raise ValueError(f"Could not resolve the IP address of {item}.") + msg = f"Could not resolve the IP address of {item}." + raise ValueError(msg) else: # Validate IP address. try: @@ -1046,7 +1050,8 @@ def set_secondary_dns(hostnames, env): else: ipaddress.ip_address(item[4:]) # raises a ValueError if there's a problem except ValueError: - raise ValueError(f"'{item[4:]}' is not an IPv4 or IPv6 address or subnet.") + msg = f"'{item[4:]}' is not an IPv4 or IPv6 address or subnet." + raise ValueError(msg) # Set. set_custom_dns_record("_secondary_nameserver", "A", " ".join(hostnames), "set", env) diff --git a/management/mail_log.py b/management/mail_log.py index 60e5e649..8907342e 100755 --- a/management/mail_log.py +++ b/management/mail_log.py @@ -608,7 +608,8 @@ def valid_date(string): try: date = dateutil.parser.parse(string) except ValueError: - raise argparse.ArgumentTypeError(f"Unrecognized date and/or time '{string}'") + msg = f"Unrecognized date and/or time '{string}'" + raise argparse.ArgumentTypeError(msg) return date diff --git a/management/mailconfig.py b/management/mailconfig.py index 61fd3e2a..65bb4ad2 100755 --- a/management/mailconfig.py +++ b/management/mailconfig.py @@ -341,7 +341,8 @@ def get_mail_password(email, env): c.execute('SELECT password FROM users WHERE email=?', (email,)) rows = c.fetchall() if len(rows) != 1: - raise ValueError(f"That's not a user ({email}).") + msg = f"That's not a user ({email})." + raise ValueError(msg) return rows[0][0] def remove_mail_user(email, env): diff --git a/tests/fail2ban.py b/tests/fail2ban.py index 30e3c267..46ce9271 100644 --- a/tests/fail2ban.py +++ b/tests/fail2ban.py @@ -142,7 +142,8 @@ def http_test(url, expected_status, postdata=None, qsargs=None, auth=None): # return response status code if r.status_code != expected_status: r.raise_for_status() # anything but 200 - raise OSError(f"Got unexpected status code {r.status_code}.") + msg = f"Got unexpected status code {r.status_code}." + raise OSError(msg) # define how to run a test