diff --git a/docs/source/changes.md b/docs/source/changes.md index 5b9038fd..20541a4e 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -28,6 +28,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and - {pull}`540` changes the CLI entry-point and allow `pytask.build(tasks=task_func)` as the signatures suggested. - {pull}`542` refactors the plugin manager. +- {pull}`543` fixes imports in tests and related issues. ## 0.4.4 - 2023-12-04 diff --git a/src/pytask/__init__.py b/src/pytask/__init__.py index 612dc537..8c530cc2 100644 --- a/src/pytask/__init__.py +++ b/src/pytask/__init__.py @@ -6,7 +6,6 @@ from _pytask.build import build from _pytask.capture_utils import CaptureMethod from _pytask.capture_utils import ShowCapture -from _pytask.cli import cli from _pytask.click import ColoredCommand from _pytask.click import ColoredGroup from _pytask.click import EnumChoice @@ -76,6 +75,10 @@ from _pytask.warnings_utils import warning_record_to_str from _pytask.warnings_utils import WarningReport +# _pytask.cli needs to be imported last because it triggers extending the cli and +# therefore loading plugins which will attempt to import modules that might only be +# partially initialized. Maybe not here, but definitely for plugins. +from _pytask.cli import cli # noreorder __all__ = [ "BaseTable", diff --git a/tests/conftest.py b/tests/conftest.py index ee77081b..6b0ac497 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import re import sys from contextlib import contextmanager @@ -7,10 +8,11 @@ from typing import Any import pytest -from _pytask.pluginmanager import storage from click.testing import CliRunner +from nbmake.pytest_items import NotebookItem from packaging import version from pytask import console +from pytask import storage @pytest.fixture(autouse=True) @@ -106,3 +108,11 @@ def invoke(self, *args, **kwargs): @pytest.fixture() def runner(): return CustomCliRunner() + + +def pytest_collection_modifyitems(session, config, items) -> None: # noqa: ARG001 + """Add markers to Jupyter notebook tests.""" + if sys.platform == "debian" and "CI" in os.environ: # pragma: no cover + for item in items: + if isinstance(item, NotebookItem): + item.add_marker(pytest.mark.xfail(reason="Fails regularly on MacOS")) diff --git a/tests/test_collect.py b/tests/test_collect.py index b6cbf122..6d01eec6 100644 --- a/tests/test_collect.py +++ b/tests/test_collect.py @@ -8,12 +8,12 @@ import pytest from _pytask.collect import _find_shortest_uniquely_identifiable_name_for_tasks from _pytask.collect import pytask_collect_node -from _pytask.exceptions import NodeNotCollectedError -from _pytask.models import NodeInfo from pytask import build from pytask import cli from pytask import CollectionOutcome from pytask import ExitCode +from pytask import NodeInfo +from pytask import NodeNotCollectedError from pytask import Session from pytask import Task diff --git a/tests/test_collect_command.py b/tests/test_collect_command.py index 0af0c229..94788da8 100644 --- a/tests/test_collect_command.py +++ b/tests/test_collect_command.py @@ -9,10 +9,10 @@ import pytest from _pytask.collect_command import _find_common_ancestor_of_all_nodes from _pytask.collect_command import _print_collected_tasks -from _pytask.nodes import PathNode from attrs import define from pytask import cli from pytask import ExitCode +from pytask import PathNode from pytask import Task diff --git a/tests/test_collect_utils.py b/tests/test_collect_utils.py index 4969097f..55ee009d 100644 --- a/tests/test_collect_utils.py +++ b/tests/test_collect_utils.py @@ -12,9 +12,9 @@ from _pytask.collect_utils import _find_args_with_product_annotation from _pytask.collect_utils import _merge_dictionaries from _pytask.collect_utils import _Placeholder -from _pytask.typing import Product # noqa: TCH002 from pytask import depends_on from pytask import produces +from pytask import Product from typing_extensions import Annotated diff --git a/tests/test_database.py b/tests/test_database.py index 5c4fd5ca..1639360c 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -3,12 +3,12 @@ import textwrap import pytest -from _pytask.database_utils import create_database -from _pytask.database_utils import DatabaseSession -from _pytask.database_utils import State from pytask import build from pytask import cli +from pytask import create_database +from pytask import DatabaseSession from pytask import ExitCode +from pytask import State from pytask.path import hash_path from sqlalchemy.engine import make_url diff --git a/tests/test_execute.py b/tests/test_execute.py index d9774dfb..ea8f0819 100644 --- a/tests/test_execute.py +++ b/tests/test_execute.py @@ -683,7 +683,7 @@ def task_example() -> Annotated[Dict[str, str], nodes]: @pytest.mark.end_to_end() def test_execute_tasks_and_pass_values_only_by_python_nodes(runner, tmp_path): source = """ - from _pytask.nodes import PathNode + from pytask import PathNode from pytask import PythonNode from typing_extensions import Annotated from pathlib import Path diff --git a/tests/test_hook_module.py b/tests/test_hook_module.py index 24784c1d..40a712d9 100644 --- a/tests/test_hook_module.py +++ b/tests/test_hook_module.py @@ -9,6 +9,7 @@ from pytask import ExitCode +@pytest.mark.end_to_end() @pytest.mark.parametrize( "module_name", [ @@ -55,6 +56,7 @@ def pytask_extend_command_line_interface(cli): assert "--new-option" in result.stdout.decode() +@pytest.mark.end_to_end() @pytest.mark.parametrize( "module_name", [ @@ -100,6 +102,7 @@ def pytask_extend_command_line_interface(cli): assert "--new-option" in result.stdout.decode() +@pytest.mark.end_to_end() def test_error_when_hook_module_path_does_not_exist(tmp_path): result = subprocess.run( # noqa: PLW1510 ("pytask", "build", "--hook-module", "hooks.py", "--help"), @@ -110,6 +113,7 @@ def test_error_when_hook_module_path_does_not_exist(tmp_path): assert b"Error: Invalid value for '--hook-module'" in result.stderr +@pytest.mark.end_to_end() def test_error_when_hook_module_module_does_not_exist(tmp_path): result = subprocess.run( # noqa: PLW1510 ("pytask", "build", "--hook-module", "hooks", "--help"), @@ -120,6 +124,7 @@ def test_error_when_hook_module_module_does_not_exist(tmp_path): assert b"Error: Invalid value for '--hook-module':" in result.stderr +@pytest.mark.end_to_end() def test_error_when_hook_module_is_no_iterable(tmp_path): tmp_path.joinpath("pyproject.toml").write_text( "[tool.pytask.ini_options]\nhook_module = 'hooks'" diff --git a/tests/test_live.py b/tests/test_live.py index 0058691a..468ce36b 100644 --- a/tests/test_live.py +++ b/tests/test_live.py @@ -6,8 +6,8 @@ import pytest from _pytask.live import LiveExecution from _pytask.live import LiveManager -from _pytask.reports import ExecutionReport from pytask import cli +from pytask import ExecutionReport from pytask import ExitCode from pytask import Task from pytask import TaskOutcome diff --git a/tests/test_logging.py b/tests/test_logging.py index e14e90f4..972716ff 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -8,9 +8,9 @@ from _pytask.logging import _format_plugin_names_and_versions from _pytask.logging import _humanize_time from _pytask.logging import pytask_log_session_footer -from _pytask.outcomes import ExitCode -from _pytask.outcomes import TaskOutcome from pytask import cli +from pytask import ExitCode +from pytask import TaskOutcome class DummyDist(NamedTuple): diff --git a/tests/test_mark_utils.py b/tests/test_mark_utils.py index 0050acea..fdcbc2a2 100644 --- a/tests/test_mark_utils.py +++ b/tests/test_mark_utils.py @@ -4,7 +4,7 @@ import pytask import pytest -from _pytask.models import CollectionMetadata +from pytask import CollectionMetadata from pytask import get_all_marks from pytask import get_marks from pytask import has_mark diff --git a/tests/test_outcomes.py b/tests/test_outcomes.py index cda4325e..5253ca21 100644 --- a/tests/test_outcomes.py +++ b/tests/test_outcomes.py @@ -1,10 +1,10 @@ from __future__ import annotations import pytest -from _pytask.reports import CollectionReport -from _pytask.reports import ExecutionReport from pytask import CollectionOutcome +from pytask import CollectionReport from pytask import count_outcomes +from pytask import ExecutionReport from pytask import TaskOutcome diff --git a/tests/test_path.py b/tests/test_path.py index 265f92b1..e7df4389 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -15,8 +15,8 @@ from _pytask.path import find_case_sensitive_path from _pytask.path import find_closest_ancestor from _pytask.path import find_common_ancestor -from _pytask.path import import_path from _pytask.path import relative_to +from pytask.path import import_path @pytest.mark.unit() diff --git a/tests/test_persist.py b/tests/test_persist.py index 047b69ff..db57e2c8 100644 --- a/tests/test_persist.py +++ b/tests/test_persist.py @@ -3,8 +3,6 @@ import textwrap import pytest -from _pytask.database_utils import State -from _pytask.path import hash_path from _pytask.persist import pytask_execute_task_process_report from pytask import build from pytask import create_database @@ -12,7 +10,9 @@ from pytask import ExitCode from pytask import Persisted from pytask import SkippedUnchanged +from pytask import State from pytask import TaskOutcome +from pytask.path import hash_path from tests.conftest import restore_sys_path_and_module_after_test_execution diff --git a/tests/test_profile.py b/tests/test_profile.py index 8dbafdd7..6c3bfcda 100644 --- a/tests/test_profile.py +++ b/tests/test_profile.py @@ -3,13 +3,13 @@ import textwrap import pytest -from _pytask.cli import cli from _pytask.profile import _to_human_readable_size -from _pytask.profile import Runtime from pytask import build +from pytask import cli from pytask import create_database from pytask import DatabaseSession from pytask import ExitCode +from pytask import Runtime @pytest.mark.end_to_end() diff --git a/tests/test_shared.py b/tests/test_shared.py index 0547f742..22cd5cb2 100644 --- a/tests/test_shared.py +++ b/tests/test_shared.py @@ -4,10 +4,10 @@ from contextlib import ExitStack as does_not_raise # noqa: N813 import pytest -from _pytask.outcomes import ExitCode from _pytask.shared import convert_to_enum from _pytask.shared import find_duplicates from pytask import build +from pytask import ExitCode from pytask import ShowCapture diff --git a/tests/test_task.py b/tests/test_task.py index b1086198..e4b00014 100644 --- a/tests/test_task.py +++ b/tests/test_task.py @@ -708,7 +708,7 @@ def func(): source = """ from pytask import task from pathlib import Path - from _pytask.path import import_path + from pytask.path import import_path import inspect _ROOT_PATH = Path(__file__).parent diff --git a/tests/test_tree_util.py b/tests/test_tree_util.py index 99d010a5..8c93cb15 100644 --- a/tests/test_tree_util.py +++ b/tests/test_tree_util.py @@ -4,11 +4,11 @@ import textwrap import pytest -from _pytask.outcomes import ExitCode -from _pytask.tree_util import tree_map -from _pytask.tree_util import tree_structure from pytask import build from pytask import cli +from pytask import ExitCode +from pytask.tree_util import tree_map +from pytask.tree_util import tree_structure @pytest.mark.end_to_end() @@ -52,7 +52,7 @@ def test_profile_with_pytree(tmp_path, runner): source = """ import time import pytask - from _pytask.tree_util import tree_leaves + from pytask.tree_util import tree_leaves @pytask.mark.produces([{"out_1": "out_1.txt"}, {"out_2": "out_2.txt"}]) def task_example(produces): diff --git a/tests/test_typing.py b/tests/test_typing.py index eb60d461..1a084c7f 100644 --- a/tests/test_typing.py +++ b/tests/test_typing.py @@ -3,7 +3,7 @@ import functools import pytest -from _pytask.typing import is_task_function +from pytask import is_task_function @pytest.mark.unit()