From 3101c026b96e15f155aa95e3e3321dbee586643c Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Sun, 4 Feb 2024 19:39:21 +0100 Subject: [PATCH] [flake8-bugbear] Remove hidden global state to import only once --- pyproject.toml | 1 - src/_pytest/unittest.py | 19 ++++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 634b08cbd..e3d64805d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -141,7 +141,6 @@ select = [ ignore = [ # bugbear ignore "B004", # Using `hasattr(x, "__call__")` to test if x is callable is unreliable. - "B006", # Do not use mutable data structures for argument defaults "B007", # Loop control variable `i` not used within loop body "B009", # Do not call `getattr` with a constant attribute value "B010", # [*] Do not call `setattr` with a constant attribute value. diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index eccd7eca6..6598bdbc5 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -361,14 +361,21 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None: # Twisted trial support. +classImplements_has_run = False @hookimpl(wrapper=True) def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]: if isinstance(item, TestCaseFunction) and "twisted.trial.unittest" in sys.modules: ut: Any = sys.modules["twisted.python.failure"] + global classImplements_has_run Failure__init__ = ut.Failure.__init__ - check_testcase_implements_trial_reporter() + if not classImplements_has_run: + from twisted.trial.itrial import IReporter + from zope.interface import classImplements + + classImplements(TestCaseFunction, IReporter) + classImplements_has_run = True def excstore( self, exc_value=None, exc_type=None, exc_tb=None, captureVars=None @@ -396,16 +403,6 @@ def pytest_runtest_protocol(item: Item) -> Generator[None, object, object]: return res -def check_testcase_implements_trial_reporter(done: List[int] = []) -> None: - if done: - return - from twisted.trial.itrial import IReporter - from zope.interface import classImplements - - classImplements(TestCaseFunction, IReporter) - done.append(1) - - def _is_skipped(obj) -> bool: """Return True if the given object has been marked with @unittest.skip.""" return bool(getattr(obj, "__unittest_skip__", False))