From 845709c03faaa006c1d12c669a5c204e6aed23d8 Mon Sep 17 00:00:00 2001 From: Tai Sakuma Date: Sun, 26 May 2024 15:08:02 -0400 Subject: [PATCH 1/2] Replace sys.modules with the import statement --- .../detect/test_is_jupyter_notebook.py | 24 ++++++++------- .../presentation/detect/test_is_spyder_ide.py | 11 +++---- tests/presentation/test_base.py | 13 ++++---- .../presentation/test_create_presentation.py | 30 +++++++++---------- tests/progress_report/test_reporter.py | 11 +++---- tests/scenarios/conftest.py | 24 ++++++++------- 6 files changed, 61 insertions(+), 52 deletions(-) diff --git a/tests/presentation/detect/test_is_jupyter_notebook.py b/tests/presentation/detect/test_is_jupyter_notebook.py index beb7166..21f0168 100644 --- a/tests/presentation/detect/test_is_jupyter_notebook.py +++ b/tests/presentation/detect/test_is_jupyter_notebook.py @@ -1,6 +1,5 @@ -from collections.abc import Iterator -import sys import unittest.mock as mock +from collections.abc import Iterator import pytest @@ -9,22 +8,26 @@ @pytest.fixture() def mock_widgets_module(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: + from atpbar.presentation.detect import jupy + ret = mock.Mock() - module = sys.modules['atpbar.presentation.detect.jupy'] - monkeypatch.setattr(module, 'widgets', ret) + monkeypatch.setattr(jupy, 'widgets', ret) yield ret @pytest.fixture() def mock_display_module(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: + from atpbar.presentation.detect import jupy + ret = mock.Mock() - module = sys.modules['atpbar.presentation.detect.jupy'] - monkeypatch.setattr(module, 'display', ret) + monkeypatch.setattr(jupy, 'display', ret) yield ret @pytest.fixture() def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: + from atpbar.presentation.detect import jupy + mock_ipython = mock.Mock() mock_ipython.config = { 'IPKernelApp': { @@ -33,17 +36,16 @@ def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: } ret = mock.Mock() ret.return_value = mock_ipython - - module = sys.modules['atpbar.presentation.detect.jupy'] - monkeypatch.setattr(module, 'get_ipython', ret) + monkeypatch.setattr(jupy, 'get_ipython', ret) yield ret @pytest.fixture() def mock_is_spyder_ide(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: + from atpbar.presentation.detect import jupy + ret = mock.Mock() - module = sys.modules['atpbar.presentation.detect.jupy'] - monkeypatch.setattr(module, 'is_spyder_ide', ret) + monkeypatch.setattr(jupy, 'is_spyder_ide', ret) yield ret diff --git a/tests/presentation/detect/test_is_spyder_ide.py b/tests/presentation/detect/test_is_spyder_ide.py index e1cd142..457756a 100644 --- a/tests/presentation/detect/test_is_spyder_ide.py +++ b/tests/presentation/detect/test_is_spyder_ide.py @@ -1,5 +1,4 @@ import os -import sys import unittest.mock as mock from collections.abc import Iterator @@ -10,14 +9,17 @@ @pytest.fixture() def mock_spyder_module(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: + from atpbar.presentation.detect import spy + ret = mock.Mock() - module = sys.modules['atpbar.presentation.detect.spy'] - monkeypatch.setattr(module, 'spyder', ret) + monkeypatch.setattr(spy, 'spyder', ret) yield ret @pytest.fixture() def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: + from atpbar.presentation.detect import spy + mock_ipython = mock.Mock() mock_ipython.config = { 'IPKernelApp': { @@ -27,8 +29,7 @@ def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]: ret = mock.Mock() ret.return_value = mock_ipython - module = sys.modules['atpbar.presentation.detect.spy'] - monkeypatch.setattr(module, 'get_ipython', ret) + monkeypatch.setattr(spy, 'get_ipython', ret) yield ret diff --git a/tests/presentation/test_base.py b/tests/presentation/test_base.py index fe250bd..fc03e07 100644 --- a/tests/presentation/test_base.py +++ b/tests/presentation/test_base.py @@ -1,4 +1,4 @@ -import sys +import time import unittest.mock as mock import uuid @@ -15,11 +15,12 @@ def _present(self, report: Report) -> None: @pytest.fixture() def mock_time(monkeypatch: pytest.MonkeyPatch) -> mock.Mock: - ret = mock.Mock() - module = sys.modules['atpbar.presentation.base'] - monkeypatch.setattr(module, 'time', ret) - ret.time.return_value = 1533374055.904203 - return ret + from atpbar.presentation import base + + m = mock.Mock(wraps=time) + m.time.return_value = 1533374055.904203 + monkeypatch.setattr(base, 'time', m) + return m @pytest.fixture() diff --git a/tests/presentation/test_create_presentation.py b/tests/presentation/test_create_presentation.py index 0b656e5..aa8a87f 100644 --- a/tests/presentation/test_create_presentation.py +++ b/tests/presentation/test_create_presentation.py @@ -17,16 +17,14 @@ @pytest.fixture(params=[True, False]) def isatty(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> bool: + from atpbar.presentation import create + ret = request.param org_stdout = sys.stdout - f = mock.Mock( - **{ - 'stdout.isatty.return_value': ret, - 'stdout.write.side_effect': lambda x: org_stdout.write(x), - } - ) - module = sys.modules['atpbar.presentation.create'] - monkeypatch.setattr(module, 'sys', f) + m = mock.Mock(wraps=sys) + m.stdout.isatty.return_value = ret + m.stdout.write.side_effect = lambda x: org_stdout.write(x) + monkeypatch.setattr(create, 'sys', m) return ret @@ -40,11 +38,12 @@ def isatty(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> b def is_jupyter_notebook( request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch ) -> bool: + from atpbar.presentation import create + ret = request.param - f = mock.Mock() - f.return_value = ret - module = sys.modules['atpbar.presentation.create'] - monkeypatch.setattr(module, 'is_jupyter_notebook', f) + m = mock.Mock(wraps=create.is_jupyter_notebook) + m.return_value = ret + monkeypatch.setattr(create, 'is_jupyter_notebook', m) return ret @@ -52,14 +51,15 @@ def is_jupyter_notebook( def del_ProgressBarJupyter( request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch ) -> bool: + from atpbar.presentation import create + ret = request.param - module = sys.modules['atpbar.presentation.create'] if ret: - monkeypatch.delattr(module, 'ProgressBarJupyter', raising=False) + monkeypatch.delattr(create, 'ProgressBarJupyter', raising=False) else: m = mock.Mock() m().__class__.__name__ = 'ProgressBarJupyter' - monkeypatch.setattr(module, 'ProgressBarJupyter', m, raising=False) + monkeypatch.setattr(create, 'ProgressBarJupyter', m, raising=False) return ret diff --git a/tests/progress_report/test_reporter.py b/tests/progress_report/test_reporter.py index 2312f56..d8eefc0 100644 --- a/tests/progress_report/test_reporter.py +++ b/tests/progress_report/test_reporter.py @@ -1,4 +1,4 @@ -import sys +import time import unittest.mock as mock import uuid from typing import Any @@ -15,10 +15,11 @@ def mock_queue() -> mock.Mock: @pytest.fixture() def mock_time(monkeypatch: pytest.MonkeyPatch) -> mock.Mock: - ret = mock.Mock() - module = sys.modules['atpbar.progress_report.reporter'] - monkeypatch.setattr(module, 'time', ret) - return ret + from atpbar.progress_report import reporter + + m = mock.Mock(wraps=time) + monkeypatch.setattr(reporter, 'time', m) + return m @pytest.fixture() diff --git a/tests/scenarios/conftest.py b/tests/scenarios/conftest.py index e2a8f30..3538295 100644 --- a/tests/scenarios/conftest.py +++ b/tests/scenarios/conftest.py @@ -1,4 +1,3 @@ -import sys from collections.abc import Iterator from uuid import UUID @@ -66,23 +65,28 @@ def __call__(self) -> MockProgressBar: @pytest.fixture(autouse=True) def mock_create_presentation(monkeypatch: pytest.MonkeyPatch) -> MockCreatePresentation: + from atpbar import machine + ret = MockCreatePresentation() - module = sys.modules['atpbar.machine'] - monkeypatch.setattr(module, 'create_presentation', ret) + monkeypatch.setattr(machine, 'create_presentation', ret) return ret @pytest.fixture(autouse=True) def machine(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: - module = sys.modules['atpbar.funcs'] - y = module.StateMachine() - monkeypatch.setattr(module, '_machine', y) - yield - shutdown() + from atpbar import funcs + + y = funcs.StateMachine() + monkeypatch.setattr(funcs, '_machine', y) + try: + yield + finally: + shutdown() @pytest.fixture(autouse=True) def reporter_interval(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: - module = sys.modules['atpbar.progress_report.reporter'] - monkeypatch.setattr(module, 'DEFAULT_INTERVAL', 0) + from atpbar.progress_report import reporter + + monkeypatch.setattr(reporter, 'DEFAULT_INTERVAL', 0) yield From e4c9224e113f7426af2b6483aff2c1a729995c48 Mon Sep 17 00:00:00 2001 From: Tai Sakuma Date: Sun, 26 May 2024 15:09:25 -0400 Subject: [PATCH 2/2] Silence Flake8 errors --- tests/presentation/test_create_presentation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/presentation/test_create_presentation.py b/tests/presentation/test_create_presentation.py index aa8a87f..f9b713a 100644 --- a/tests/presentation/test_create_presentation.py +++ b/tests/presentation/test_create_presentation.py @@ -7,8 +7,8 @@ has_jupyter_notebook = False try: - import ipywidgets as widgets - from IPython.display import display + import ipywidgets as widgets # noqa: F401 + from IPython.display import display # noqa: F401 has_jupyter_notebook = True except ImportError: