From fc2d56b1a14d58577439ff960921e1f7a93f32a0 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Tue, 14 Nov 2023 15:34:40 +0000 Subject: [PATCH] ruff 'unsafe' autofixes --- cf_units/__init__.py | 10 ++++------ cf_units/_udunits2_parser/__init__.py | 2 +- cf_units/_udunits2_parser/graph.py | 2 +- cf_units/_udunits2_parser/parser/udunits2Parser.py | 4 ++-- cf_units/tests/test_coding_standards.py | 2 +- cf_units/util.py | 10 +++++----- setup.py | 10 +++++----- 7 files changed, 19 insertions(+), 21 deletions(-) diff --git a/cf_units/__init__.py b/cf_units/__init__.py index 4b96bc1e..06debc0c 100644 --- a/cf_units/__init__.py +++ b/cf_units/__init__.py @@ -191,8 +191,7 @@ def suppress_errors(): except _ud.UdunitsError as e: error_msg = ': "%s"' % e.error_msg() if e.errnum else "" raise OSError( - "[%s] Failed to open UDUNITS-2 XML unit database%s" - % (e.status_msg(), error_msg) + f"[{e.status_msg()}] Failed to open UDUNITS-2 XML unit database{error_msg}" ) @@ -930,7 +929,7 @@ def title(self, value): dt = self.num2date(value) result = dt.strftime("%Y-%m-%d %H:%M:%S") else: - result = "%s %s" % (str(value), self) + result = f"{str(value)} {self}" return result @property @@ -1793,8 +1792,7 @@ def convert(self, value, other, ctype=FLOAT64, inplace=False): # Strict type check of numpy array. if result.dtype.type not in (np.float32, np.float64): raise TypeError( - "Expect a numpy array of '%s' or '%s'" - % np.float32, + "Expect a numpy array of '{}' or '{}'".format(*np.float32), np.float64, ) ctype = result.dtype.type @@ -1827,7 +1825,7 @@ def convert(self, value, other, ctype=FLOAT64, inplace=False): return result else: raise ValueError( - "Unable to convert from '%r' to '%r'." % (self, other) + f"Unable to convert from '{self!r}' to '{other!r}'." ) @property diff --git a/cf_units/_udunits2_parser/__init__.py b/cf_units/_udunits2_parser/__init__.py index 25f32c76..aec51503 100644 --- a/cf_units/_udunits2_parser/__init__.py +++ b/cf_units/_udunits2_parser/__init__.py @@ -192,7 +192,7 @@ def _debug_tokens(unit_string): continue token_type_idx = token.type rule = TOKEN_ID_NAMES[token_type_idx] - print("%s: %s" % (token.text, rule)) + print(f"{token.text}: {rule}") def normalize(unit_string): diff --git a/cf_units/_udunits2_parser/graph.py b/cf_units/_udunits2_parser/graph.py index 4022563d..561271aa 100644 --- a/cf_units/_udunits2_parser/graph.py +++ b/cf_units/_udunits2_parser/graph.py @@ -30,7 +30,7 @@ def _repr_ctx(self): kwargs = ", ".join( f"{key}={value!r}" for key, value in self._attrs.items() ) - return dict(cls_name=self.__class__.__name__, kwargs=kwargs) + return {"cls_name": self.__class__.__name__, "kwargs": kwargs} def __repr__(self): return "{cls_name}({kwargs})".format(**self._repr_ctx()) diff --git a/cf_units/_udunits2_parser/parser/udunits2Parser.py b/cf_units/_udunits2_parser/parser/udunits2Parser.py index 67371300..bd197034 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Parser.py +++ b/cf_units/_udunits2_parser/parser/udunits2Parser.py @@ -2223,8 +2223,8 @@ def timezone_offset(self): return localctx def sempred(self, localctx: RuleContext, ruleIndex: int, predIndex: int): - if self._predicates == None: - self._predicates = dict() + if self._predicates is None: + self._predicates = {} self._predicates[2] = self.product_sempred pred = self._predicates.get(ruleIndex, None) if pred is None: diff --git a/cf_units/tests/test_coding_standards.py b/cf_units/tests/test_coding_standards.py index b8f458db..ff4d0364 100644 --- a/cf_units/tests/test_coding_standards.py +++ b/cf_units/tests/test_coding_standards.py @@ -109,7 +109,7 @@ def test_license_headers(self): ) failed = False - for fname, last_change in sorted(last_change_by_fname.items()): + for fname, _last_change in sorted(last_change_by_fname.items()): full_fname = os.path.join(REPO_DIR, fname) if ( full_fname.endswith(".py") diff --git a/cf_units/util.py b/cf_units/util.py index 2f5841bf..adf5e40b 100644 --- a/cf_units/util.py +++ b/cf_units/util.py @@ -64,8 +64,8 @@ def __new__(cls, name, bases, namespace): if "__init__" not in namespace: # Create a default __init__ method for the class method_source = ( - "def __init__(self, %s):\n " - "self._init_from_tuple((%s,))" % (args, args) + f"def __init__(self, {args}):\n " + f"self._init_from_tuple(({args},))" ) exec(method_source, namespace) @@ -74,12 +74,12 @@ def __new__(cls, name, bases, namespace): if "_init" not in namespace: # Create a default _init method for the class method_source = ( - "def _init(self, %s):\n " - "self._init_from_tuple((%s,))" % (args, args) + f"def _init(self, {args}):\n " + f"self._init_from_tuple(({args},))" ) exec(method_source, namespace) - return super(_MetaOrderedHashable, cls).__new__( + return super().__new__( cls, name, bases, namespace ) diff --git a/setup.py b/setup.py index 34fb29e4..54aa30f2 100644 --- a/setup.py +++ b/setup.py @@ -147,10 +147,10 @@ def _set_builtin(name, value): cmdclass = {"clean_cython": CleanCython, "build_ext": numpy_build_ext} -kwargs = dict( - cmdclass=cmdclass, - ext_modules=[udunits_ext], - package_data=get_package_data(), -) +kwargs = { + "cmdclass": cmdclass, + "ext_modules": [udunits_ext], + "package_data": get_package_data(), +} setup(**kwargs)