diff options
author | Arthur Zamarin <arthurzam@gentoo.org> | 2022-08-02 21:06:39 +0300 |
---|---|---|
committer | Arthur Zamarin <arthurzam@gentoo.org> | 2022-08-24 19:25:08 +0300 |
commit | 7aed9341485ee53c0784df9e2982338e1ec271f2 (patch) | |
tree | 52c4fc55ef226f70a50cbab1350c36b419b1a628 | |
parent | klass: add typing for jit_attr (diff) | |
download | snakeoil-7aed9341485ee53c0784df9e2982338e1ec271f2.tar.gz snakeoil-7aed9341485ee53c0784df9e2982338e1ec271f2.tar.bz2 snakeoil-7aed9341485ee53c0784df9e2982338e1ec271f2.zip |
test/*.py: various modernization of code
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
-rw-r--r-- | src/snakeoil/test/argparse_helpers.py | 6 | ||||
-rw-r--r-- | src/snakeoil/test/demandload.py | 7 | ||||
-rw-r--r-- | src/snakeoil/test/eq_hash_inheritance.py | 14 | ||||
-rw-r--r-- | src/snakeoil/test/mixins.py | 31 | ||||
-rw-r--r-- | src/snakeoil/test/modules.py | 2 | ||||
-rw-r--r-- | src/snakeoil/test/slot_shadowing.py | 15 |
6 files changed, 35 insertions, 40 deletions
diff --git a/src/snakeoil/test/argparse_helpers.py b/src/snakeoil/test/argparse_helpers.py index b163c76..152e335 100644 --- a/src/snakeoil/test/argparse_helpers.py +++ b/src/snakeoil/test/argparse_helpers.py @@ -56,7 +56,7 @@ class Color(FormatterObject): self.color = color def __repr__(self): - return '<Color: mode - %s; color - %s>' % (self.mode, self.color) + return f'<Color: mode - {self.mode}; color - {self.color}>' class Reset(FormatterObject): @@ -115,8 +115,8 @@ class FakeStreamFormatter(PlainTextFormatter): def get_text_stream(self): return b''.join( - [x for x in self.stream - if not isinstance(x, FormatterObject)]).decode('ascii') + (x for x in self.stream + if not isinstance(x, FormatterObject))).decode('ascii') class ArgParseMixin: diff --git a/src/snakeoil/test/demandload.py b/src/snakeoil/test/demandload.py index b253642..9ce8f5d 100644 --- a/src/snakeoil/test/demandload.py +++ b/src/snakeoil/test/demandload.py @@ -10,9 +10,8 @@ class DemandLoadTargets(mixins.PythonNamespaceWalker): self._failures = [] def teardown_method(self, method): - msg = "\n".join(sorted("%s: error %s" % (target, e) - for target, e in self._failures)) - assert not self._failures, "bad demandload targets:\n%s" % (msg,) + msg = "\n".join(sorted(f"{target}: error {e}" for target, e in self._failures)) + assert not self._failures, "bad demandload targets:\n" + msg def test_demandload_targets(self): for x in self.walk_namespace( @@ -28,4 +27,4 @@ class DemandLoadTargets(mixins.PythonNamespaceWalker): getattr(obj, "__class__", None) except ImportError as ie: # hit one. - self._failures.append(("%s: target %s" % (mod.__name__, attr), ie)) + self._failures.append((f"{mod.__name__}: target {attr}", ie)) diff --git a/src/snakeoil/test/eq_hash_inheritance.py b/src/snakeoil/test/eq_hash_inheritance.py index 5199c4a..96cedb5 100644 --- a/src/snakeoil/test/eq_hash_inheritance.py +++ b/src/snakeoil/test/eq_hash_inheritance.py @@ -17,7 +17,7 @@ class Test(mixins.TargetedNamespaceWalker, mixins.KlassWalker): if getattr(cls, "__hash__intentionally_disabled__", False): return True - namepath = "%s.%s" % (cls.__module__, cls.__name__) + namepath = "{cls.__module__}.{cls.__name__}" return not namepath.startswith(self.target_namespace) def run_check(self, cls): @@ -37,10 +37,10 @@ class Test(mixins.TargetedNamespaceWalker, mixins.KlassWalker): # pylint: disable=undefined-loop-variable # 'parent' is guaranteed to be defined due to the 'else' clause above - assert getattr(cls, '__hash__') != None, ( - "class '%s.%s' had its __hash__ reset, while it would've inherited " - "__hash__ from parent '%s.%s'; this occurs in py3k when __eq__ is " - "defined alone. If this is desired behaviour, set " + assert getattr(cls, '__hash__') is not None, ( + f"class '{cls.__module__}.{cls.__name__}' had its __hash__ reset, " + "while it would've inherited __hash__ from parent " + f"'{parent.__module__}.{parent.__name__}'; this occurs in py3k when " + "__eq__ is defined alone. If this is desired behaviour, set " "__hash__intentionally_disabled__ to True to explicitly ignore this" - " class" % (cls.__module__, cls.__name__, parent.__module__, - parent.__name__)) + " class") diff --git a/src/snakeoil/test/mixins.py b/src/snakeoil/test/mixins.py index 764b194..422b1bd 100644 --- a/src/snakeoil/test/mixins.py +++ b/src/snakeoil/test/mixins.py @@ -52,16 +52,16 @@ class PythonNamespaceWalker: ignore_all_import_failures = False - valid_inits = frozenset("__init__.%s" % x for x in ("py", "pyc", "pyo", "so")) + valid_inits = frozenset(f"__init__.{x}" for x in ("py", "pyc", "pyo", "so")) # This is for py3.2/PEP3149; dso's now have the interp + major/minor embedded # in the name. # TODO: update this for pypy's naming abi_target = 'cpython-%i%i' % tuple(sys.version_info[:2]) - module_blacklist = frozenset([ + module_blacklist = frozenset({ 'snakeoil.cli.arghparse', 'snakeoil.pickling', - ]) + }) def _default_module_blacklister(self, target): return target in self.module_blacklist or target.startswith('snakeoil.dist') @@ -80,7 +80,7 @@ class PythonNamespaceWalker: mangle = lambda x: x else: orig_namespace = namespace - mangle = lambda x: "%s.%s" % (orig_namespace, x) + mangle = lambda x: f"{orig_namespace}.{x}" if blacklist_func is None: blacklist_func = self._default_module_blacklister for mod_name in feed: @@ -109,7 +109,7 @@ class PythonNamespaceWalker: else: yield None - stats = [] + stats: list[tuple[str, int]] = [] for x in l: try: stats.append((x, os.stat(os.path.join(location, x)).st_mode)) @@ -119,11 +119,11 @@ class PythonNamespaceWalker: # file disappeared under our feet... lock file from # trial can cause this. ignore. import logging - logging.debug("file %r disappeared under our feet, ignoring" % - (os.path.join(location, x))) + logging.debug("file %r disappeared under our feet, ignoring", + os.path.join(location, x)) seen = set(['__init__']) - for (x, st) in stats: + for x, st in stats: if not (x.startswith(".") or x.endswith("~")) and stat.S_ISREG(st): if (x.endswith(".py") or x.endswith(".pyc") or x.endswith(".pyo") or x.endswith(".so")): @@ -138,22 +138,22 @@ class PythonNamespaceWalker: seen.add(y) yield y - for (x, st) in stats: + for x, st in stats: if stat.S_ISDIR(st): for y in self.recurse(os.path.join(location, x)): if y is None: yield x else: - yield "%s.%s" % (x, y) + yield f"{x}.{y}" @staticmethod - def poor_mans_load(namespace, existance_check=False): + def poor_mans_load(namespace, existence_check=False): try: obj = __import__(namespace) - if existance_check: + if existence_check: return True except: - if existance_check: + if existence_check: return False raise for chunk in namespace.split(".")[1:]: @@ -162,10 +162,9 @@ class PythonNamespaceWalker: except IGNORED_EXCEPTIONS: raise except AttributeError: - raise AssertionError("failed importing target %s" % namespace) + raise AssertionError(f"failed importing target {namespace}") except Exception as e: - raise AssertionError("failed importing target %s; error %s" - % (namespace, e)) + raise AssertionError(f"failed importing target {namespace}; error {e}") return obj diff --git a/src/snakeoil/test/modules.py b/src/snakeoil/test/modules.py index 5a564ec..6b3a6a1 100644 --- a/src/snakeoil/test/modules.py +++ b/src/snakeoil/test/modules.py @@ -11,4 +11,4 @@ class ExportedModules(mixins.PythonNamespaceWalker): for target in getattr(module, '__all__', ()): if not hasattr(module, target): failures.append((module, target)) - assert not failures, "nonexistent __all__ targets spotted: %s" % (failures,) + assert not failures, f"nonexistent __all__ targets spotted: {failures}" diff --git a/src/snakeoil/test/slot_shadowing.py b/src/snakeoil/test/slot_shadowing.py index 429fb77..bad525c 100644 --- a/src/snakeoil/test/slot_shadowing.py +++ b/src/snakeoil/test/slot_shadowing.py @@ -22,7 +22,7 @@ class SlotShadowing(mixins.TargetedNamespaceWalker, mixins.SubclassWalker): @staticmethod def mk_name(kls): - return '%s.%s' % (kls.__module__, kls.__name__) + return f'{kls.__module__}.{kls.__name__}' def _should_ignore(self, kls): return self.mk_name(kls).split(".")[0] != self.target_namespace @@ -56,8 +56,7 @@ class SlotShadowing(mixins.TargetedNamespaceWalker, mixins.SubclassWalker): if isinstance(slots, str): if self.err_if_slots_is_str: pytest.fail( - "cls %r; slots is %r (should be a tuple or list)" % - (kls, slots)) + "cls {kls!r}; slots is {slots!r} (should be a tuple or list)") slots = (slots,) if slots is None: @@ -66,7 +65,7 @@ class SlotShadowing(mixins.TargetedNamespaceWalker, mixins.SubclassWalker): if not isinstance(slots, tuple): if self.err_if_slots_is_mutable: pytest.fail( - "cls %r; slots is %r- - should be a tuple" % (kls, slots)) + "cls {kls!r}; slots is {slots!r}- - should be a tuple") slots = tuple(slots) if slots is None or (slots and slots in raw_slottings): @@ -74,12 +73,10 @@ class SlotShadowing(mixins.TargetedNamespaceWalker, mixins.SubclassWalker): # this means that the child either didn't define __slots__, or # daftly copied the parents... thus defeating the purpose. pytest.fail( - "cls %r; slots is %r, seemingly inherited from %r; the " - "derivative class should be __slots__ = ()" % - (kls, slots, raw_slottings[slots])) + "cls {kls!r}; slots is {slots!r}, seemingly inherited from " + "{raw_slottings[slots]!r}; the derivative class should be __slots__ = ()") for slot in slots: if slot in slotting: pytest.fail( - "cls %r; slot %r was already defined at %r" % - (kls, slot, slotting[slot])) + "cls {kls!r}; slot {slot!r} was already defined at {slotting[slot]!r}") |