From c6f178af72ae9545d14e3a65803a81fc4fd99df0 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 2 Apr 2024 12:14:44 +1100 Subject: [PATCH] Linting fixes (#6906) * Python linting fixes - Prefix unused loop variable * Fix unneccesary f-string * Remove old 'pass' statement * Fix return type * Simplify if statement * Fix shadowing of builtin * Simplify is_bool function * Improve type hitning for increment_serial_number * Fix shadowing * Remove unused argument * Cleanup if statement * remove unused argument * Update type hinting - Pipe not available until python 3.10 --- InvenTree/InvenTree/config.py | 2 +- InvenTree/InvenTree/conversion.py | 3 +-- InvenTree/InvenTree/exchange.py | 2 +- InvenTree/InvenTree/filters.py | 9 ++++----- InvenTree/InvenTree/format.py | 10 +++++----- InvenTree/InvenTree/helpers.py | 12 ++++-------- InvenTree/InvenTree/permissions.py | 2 +- InvenTree/InvenTree/social_auth_urls.py | 2 +- InvenTree/InvenTree/sso.py | 2 +- 9 files changed, 19 insertions(+), 25 deletions(-) diff --git a/InvenTree/InvenTree/config.py b/InvenTree/InvenTree/config.py index 1fd379f5f2..6e5299ad3f 100644 --- a/InvenTree/InvenTree/config.py +++ b/InvenTree/InvenTree/config.py @@ -347,7 +347,7 @@ def get_secret_key(): # Create a random key file options = string.digits + string.ascii_letters + string.punctuation - key = ''.join([random.choice(options) for i in range(100)]) + key = ''.join([random.choice(options) for _idx in range(100)]) secret_key_file.write_text(key) logger.debug("Loading SECRET_KEY from '%s'", secret_key_file) diff --git a/InvenTree/InvenTree/conversion.py b/InvenTree/InvenTree/conversion.py index c02fd7a561..b2d15c16df 100644 --- a/InvenTree/InvenTree/conversion.py +++ b/InvenTree/InvenTree/conversion.py @@ -95,7 +95,7 @@ def from_engineering_notation(value): """ value = str(value).strip() - pattern = f'(\d+)([a-zA-Z]+)(\d+)(.*)' + pattern = '(\d+)([a-zA-Z]+)(\d+)(.*)' if match := re.match(pattern, value): left, prefix, right, suffix = match.groups() @@ -198,7 +198,6 @@ def convert_physical_value(value: str, unit: str = None, strip_units=True): break except Exception as exc: value = None - pass if value is None: if unit: diff --git a/InvenTree/InvenTree/exchange.py b/InvenTree/InvenTree/exchange.py index 55d880b900..88584b43cc 100644 --- a/InvenTree/InvenTree/exchange.py +++ b/InvenTree/InvenTree/exchange.py @@ -20,7 +20,7 @@ class InvenTreeExchange(SimpleExchangeBackend): name = 'InvenTreeExchange' - def get_rates(self, **kwargs) -> None: + def get_rates(self, **kwargs) -> dict: """Set the requested currency codes and get rates.""" from common.models import InvenTreeSetting from plugin import registry diff --git a/InvenTree/InvenTree/filters.py b/InvenTree/InvenTree/filters.py index c7c35532b8..52af9ba436 100644 --- a/InvenTree/InvenTree/filters.py +++ b/InvenTree/InvenTree/filters.py @@ -17,11 +17,10 @@ class InvenTreeDateFilter(rest_filters.DateFilter): def filter(self, qs, value): """Override the filter method to handle timezones correctly.""" - if settings.USE_TZ: - if value is not None: - tz = timezone.get_current_timezone() - value = datetime(value.year, value.month, value.day) - value = make_aware(value, tz, True) + if settings.USE_TZ and value is not None: + tz = timezone.get_current_timezone() + value = datetime(value.year, value.month, value.day) + value = make_aware(value, tz, True) return super().filter(qs, value) diff --git a/InvenTree/InvenTree/format.py b/InvenTree/InvenTree/format.py index b03d28f2a8..5cdeac1ed1 100644 --- a/InvenTree/InvenTree/format.py +++ b/InvenTree/InvenTree/format.py @@ -69,7 +69,7 @@ def construct_format_regex(fmt_string: str) -> str: for group in string.Formatter().parse(fmt_string): prefix = group[0] # Prefix (literal text appearing before this group) name = group[1] # Name of this format variable - format = group[2] # Format specifier e.g :04d + _fmt = group[2] # Format specifier e.g :04d rep = [ '+', @@ -106,16 +106,16 @@ def construct_format_regex(fmt_string: str) -> str: # Add a named capture group for the format entry if name: # Check if integer values are required - if format.endswith('d'): - chr = '\d' + if _fmt.endswith('d'): + c = '\d' else: - chr = '.' + c = '.' # Specify width # TODO: Introspect required width w = '+' - pattern += f'(?P<{name}>{chr}{w})' + pattern += f'(?P<{name}>{c}{w})' pattern += '$' diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index 82116c26f6..fab133036b 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -248,11 +248,7 @@ def str2int(text, default=None): def is_bool(text): """Determine if a string value 'looks' like a boolean.""" - if str2bool(text, True): - return True - elif str2bool(text, False): - return True - return False + return str2bool(text, True) or str2bool(text, False) def isNull(text): @@ -473,7 +469,7 @@ def DownloadFile( return response -def increment_serial_number(serial: str): +def increment_serial_number(serial): """Given a serial number, (attempt to) generate the *next* serial number. Note: This method is exposed to custom plugins. @@ -857,9 +853,9 @@ def hash_barcode(barcode_data): barcode_data = str(barcode_data).strip() barcode_data = remove_non_printable_characters(barcode_data) - hash = hashlib.md5(str(barcode_data).encode()) + barcode_hash = hashlib.md5(str(barcode_data).encode()) - return str(hash.hexdigest()) + return str(barcode_hash.hexdigest()) def hash_file(filename: Union[str, Path], storage: Union[Storage, None] = None): diff --git a/InvenTree/InvenTree/permissions.py b/InvenTree/InvenTree/permissions.py index da472b8637..2bbf91c301 100644 --- a/InvenTree/InvenTree/permissions.py +++ b/InvenTree/InvenTree/permissions.py @@ -7,7 +7,7 @@ from rest_framework import permissions import users.models -def get_model_for_view(view, raise_error=True): +def get_model_for_view(view): """Attempt to introspect the 'model' type for an API view.""" if hasattr(view, 'get_permission_model'): return view.get_permission_model() diff --git a/InvenTree/InvenTree/social_auth_urls.py b/InvenTree/InvenTree/social_auth_urls.py index c9a77eb5df..79f6bed08b 100644 --- a/InvenTree/InvenTree/social_auth_urls.py +++ b/InvenTree/InvenTree/social_auth_urls.py @@ -85,7 +85,7 @@ for name, provider in providers.registry.provider_map.items(): cls for cls in prov_mod.__dict__.values() if isinstance(cls, type) - and not cls == OAuth2Adapter + and cls != OAuth2Adapter and issubclass(cls, OAuth2Adapter) ] diff --git a/InvenTree/InvenTree/sso.py b/InvenTree/InvenTree/sso.py index 30958847fb..0ad165a0b7 100644 --- a/InvenTree/InvenTree/sso.py +++ b/InvenTree/InvenTree/sso.py @@ -27,7 +27,7 @@ def get_provider_app(provider): return apps.first() -def check_provider(provider, raise_error=False): +def check_provider(provider): """Check if the given provider is correctly configured. To be correctly configured, the following must be true: