diff --git a/RELEASE.md b/RELEASE.md index 0436ec5ac4..8f36ca5b21 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -23,6 +23,7 @@ * Updated [Databricks documentation](https://kedro.readthedocs.io/en/0.18.1/deployment/databricks.html) to include how to get it working with IPython extension and Kedro-Viz. * Update sections on visualisation, namespacing, and experiment tracking in the spaceflight tutorial to correspond to the complete spaceflights starter. * Fixed `Jinja2` syntax loading with `TemplatedConfigLoader` using `globals.yml`. +* Removed global `_active_session`, `_activate_session` and `_deactivate_session`. Plugins that need to access objects such as the config loader should now do so through `context` in the new `after_context_created` hook. * `config_loader` is available as a public read-only attribute of `KedroContext`. * Made `hook_manager` argument optional for `runner.run`. diff --git a/kedro/extras/extensions/ipython.py b/kedro/extras/extensions/ipython.py index b8b4f039cd..03dffb04cf 100644 --- a/kedro/extras/extensions/ipython.py +++ b/kedro/extras/extensions/ipython.py @@ -45,7 +45,6 @@ def reload_kedro( from kedro.framework.cli import load_entry_points from kedro.framework.project import configure_project, pipelines from kedro.framework.session import KedroSession - from kedro.framework.session.session import _activate_session from kedro.framework.startup import bootstrap_project # If a path is provided, set it as default for subsequent calls @@ -64,7 +63,6 @@ def reload_kedro( session = KedroSession.create( metadata.package_name, default_project_path, env=env, extra_params=extra_params ) - _activate_session(session, force=True) logger.debug("Loading the context from %s", default_project_path) context = session.load_context() catalog = context.catalog diff --git a/kedro/framework/session/session.py b/kedro/framework/session/session.py index 0e8be3a1b8..1618438a09 100644 --- a/kedro/framework/session/session.py +++ b/kedro/framework/session/session.py @@ -28,24 +28,6 @@ from kedro.io.core import generate_timestamp from kedro.runner import AbstractRunner, SequentialRunner -_active_session = None - - -def _activate_session(session: "KedroSession", force: bool = False) -> None: - global _active_session - - if _active_session and not force and session is not _active_session: - raise RuntimeError( - "Cannot activate the session as another active session already exists." - ) - - _active_session = session - - -def _deactivate_session() -> None: - global _active_session - _active_session = None - def _describe_git(project_path: Path) -> Dict[str, Dict[str, Any]]: project_path = str(project_path) @@ -282,12 +264,7 @@ def close(self): if self.save_on_close: self._store.save() - if _active_session is self: - _deactivate_session() - def __enter__(self): - if _active_session is not self: - _activate_session(self) return self def __exit__(self, exc_type, exc_value, tb_): diff --git a/tests/extras/extensions/test_ipython.py b/tests/extras/extensions/test_ipython.py index 6771c04b01..f343538393 100644 --- a/tests/extras/extensions/test_ipython.py +++ b/tests/extras/extensions/test_ipython.py @@ -2,7 +2,6 @@ import pytest from kedro.extras.extensions.ipython import load_ipython_extension, reload_kedro -from kedro.framework.session.session import _deactivate_session from kedro.framework.startup import ProjectMetadata from kedro.pipeline import Pipeline @@ -23,7 +22,6 @@ def mocked_logging(mocker): @pytest.fixture(autouse=True) def cleanup_session(): yield - _deactivate_session() @pytest.fixture() diff --git a/tests/framework/session/test_session.py b/tests/framework/session/test_session.py index f72161d8af..f712489041 100644 --- a/tests/framework/session/test_session.py +++ b/tests/framework/session/test_session.py @@ -17,7 +17,6 @@ _HasSharedParentClassValidator, _IsSubclassValidator, _ProjectSettings, - configure_project, ) from kedro.framework.session import KedroSession from kedro.framework.session.store import BaseSessionStore, ShelveStore @@ -285,6 +284,12 @@ def test_create( assert session.load_context() is mock_context_class.return_value assert isinstance(session._get_config_loader(), ConfigLoader) + @pytest.mark.usefixtures("mock_settings") + def test_create_multiple_sessions(self, fake_project, mock_package_name): + with KedroSession.create(mock_package_name, fake_project): + with KedroSession.create(mock_package_name, fake_project): + pass + @pytest.mark.usefixtures("mock_settings_context_class") def test_create_no_env_extra_params( self, @@ -519,19 +524,6 @@ def test_log_error(self, fake_project, mock_package_name): "raise FakeException" in tb_line for tb_line in exception["traceback"] ) - @pytest.mark.usefixtures("mock_settings") - def test_nested_sessions(self, fake_project, mock_package_name): - configure_project(mock_package_name) - session1 = KedroSession.create(mock_package_name, fake_project) - session2 = KedroSession.create(mock_package_name, fake_project) - - with session1: - pattern = ( - "Cannot activate the session as another active session already exists" - ) - with pytest.raises(RuntimeError, match=pattern), session2: - pass # pragma: no cover - @pytest.mark.usefixtures("mock_settings_context_class") @pytest.mark.parametrize("fake_pipeline_name", [None, _FAKE_PIPELINE_NAME]) def test_run(