From 40941a9b550d8a289185e079a7e9eee543c66b8d Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Wed, 8 Jan 2025 11:21:19 -0600 Subject: [PATCH 1/8] initial test draft Signed-off-by: ravi_kumar_pilla --- kedro/__init__.py | 6 ++++++ tests/ipython/test_ipython.py | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/kedro/__init__.py b/kedro/__init__.py index 0a0d050098..4376702c08 100644 --- a/kedro/__init__.py +++ b/kedro/__init__.py @@ -29,3 +29,9 @@ class KedroPythonVersionWarning(UserWarning): or set the PYTHONWARNINGS environment variable accordingly.""", KedroPythonVersionWarning, ) + + +def load_ipython_extension(ipython) -> None: # ruff: noqa + import kedro.ipython + + kedro.ipython.load_ipython_extension(ipython) diff --git a/tests/ipython/test_ipython.py b/tests/ipython/test_ipython.py index 4228e9e6ae..8cc6089fd1 100644 --- a/tests/ipython/test_ipython.py +++ b/tests/ipython/test_ipython.py @@ -222,6 +222,17 @@ def test_line_magic_with_invalid_arguments(self, mocker, ipython): ): ipython.magic("reload_kedro --invalid_arg=dummy") + def test_ipython_kedro_extension_alias(self, mocker, ipython): + mock_ipython_extension = mocker.patch( + "kedro.ipython.load_ipython_extension", autospec=True + ) + # Ensure that `kedro` is not loaded initially + assert "kedro" not in ipython.extension_manager.loaded + ipython.magic("load_ext kedro") + mock_ipython_extension.assert_called_once_with(ipython) + # Ensure that `kedro` extension has been loaded + assert "kedro" in ipython.extension_manager.loaded + class TestProjectPathResolution: def test_only_path_specified(self): From fd651dd0a0e10b4ca368c5b53863105fad91a73c Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Wed, 8 Jan 2025 11:21:30 -0600 Subject: [PATCH 2/8] initial test draft Signed-off-by: ravi_kumar_pilla --- kedro/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kedro/__init__.py b/kedro/__init__.py index 4376702c08..a2bec8418f 100644 --- a/kedro/__init__.py +++ b/kedro/__init__.py @@ -31,7 +31,7 @@ class KedroPythonVersionWarning(UserWarning): ) -def load_ipython_extension(ipython) -> None: # ruff: noqa +def load_ipython_extension(ipython) -> None: import kedro.ipython kedro.ipython.load_ipython_extension(ipython) From 1a88348263b5548b70e240306fead5be511614c8 Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Wed, 8 Jan 2025 15:02:56 -0600 Subject: [PATCH 3/8] fix lint Signed-off-by: ravi_kumar_pilla --- kedro/__init__.py | 3 ++- pyproject.toml | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kedro/__init__.py b/kedro/__init__.py index a2bec8418f..e74c81be86 100644 --- a/kedro/__init__.py +++ b/kedro/__init__.py @@ -5,6 +5,7 @@ import sys import warnings +from typing import Any __version__ = "0.19.10" @@ -31,7 +32,7 @@ class KedroPythonVersionWarning(UserWarning): ) -def load_ipython_extension(ipython) -> None: +def load_ipython_extension(ipython: Any) -> None: import kedro.ipython kedro.ipython.load_ipython_extension(ipython) diff --git a/pyproject.toml b/pyproject.toml index 0881b6627b..c72d580763 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -178,7 +178,8 @@ layers = [ ] ignore_imports = [ "kedro.runner.task -> kedro.framework.project", - "kedro.framework.hooks.specs -> kedro.framework.context" + "kedro.framework.hooks.specs -> kedro.framework.context", + "kedro -> kedro.ipython" ] [[tool.importlinter.contracts]] @@ -188,6 +189,9 @@ modules = [ "kedro.pipeline", "kedro.io" ] +ignore_imports = [ + "kedro -> kedro.ipython" +] [[tool.importlinter.contracts]] name = "Config cannot import Runner et al" From 4cad8b694320fa51421cf9f46a4d40a00823e626 Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Thu, 9 Jan 2025 10:48:48 -0600 Subject: [PATCH 4/8] update release note Signed-off-by: ravi_kumar_pilla --- RELEASE.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE.md b/RELEASE.md index f522b029b8..112b8ba19f 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -4,6 +4,7 @@ * Implemented `KedroDataCatalog.to_config()` method that converts the catalog instance into a configuration format suitable for serialization. * Improve OmegaConfigLoader performance. * Replaced `trufflehog` with `detect-secrets` for detecting secrets within a code base. +* Added support for `%load_ext kedro`. ## Bug fixes and other changes * Added validation to ensure dataset versions consistency across catalog. From 4ff4745e4b5be9d0cc5effdd214481e7a64329a3 Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Tue, 14 Jan 2025 10:01:49 -0600 Subject: [PATCH 5/8] update type for ipython Signed-off-by: ravi_kumar_pilla --- kedro/__init__.py | 10 ++++++++-- kedro/ipython/__init__.py | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/kedro/__init__.py b/kedro/__init__.py index e74c81be86..9b8934b5dd 100644 --- a/kedro/__init__.py +++ b/kedro/__init__.py @@ -3,9 +3,15 @@ configuration and pipeline assembly. """ +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from IPython.core.interactiveshell import InteractiveShellABC + import sys import warnings -from typing import Any __version__ = "0.19.10" @@ -32,7 +38,7 @@ class KedroPythonVersionWarning(UserWarning): ) -def load_ipython_extension(ipython: Any) -> None: +def load_ipython_extension(ipython: InteractiveShellABC) -> None: import kedro.ipython kedro.ipython.load_ipython_extension(ipython) diff --git a/kedro/ipython/__init__.py b/kedro/ipython/__init__.py index 0e479e8a68..12f77bddfc 100644 --- a/kedro/ipython/__init__.py +++ b/kedro/ipython/__init__.py @@ -19,6 +19,8 @@ if TYPE_CHECKING: from collections import OrderedDict + from IPython.core.interactiveshell import InteractiveShellABC + from IPython.core.getipython import get_ipython from IPython.core.magic import needs_local_scope, register_line_magic from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring @@ -50,7 +52,7 @@ RICH_INSTALLED = True if importlib.util.find_spec("rich") is not None else False -def load_ipython_extension(ipython: Any) -> None: +def load_ipython_extension(ipython: InteractiveShellABC) -> None: """ Main entry point when %load_ext kedro.ipython is executed, either manually or automatically through `kedro ipython` or `kedro jupyter lab/notebook`. From 8e58bd0652e15d3a340540c6448a09e4c0d602ba Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Tue, 14 Jan 2025 10:11:13 -0600 Subject: [PATCH 6/8] update type for ipython Signed-off-by: ravi_kumar_pilla --- kedro/__init__.py | 4 ++-- kedro/ipython/__init__.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kedro/__init__.py b/kedro/__init__.py index 9b8934b5dd..4e42f039d8 100644 --- a/kedro/__init__.py +++ b/kedro/__init__.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from IPython.core.interactiveshell import InteractiveShellABC + from IPython.core.interactiveshell import InteractiveShell import sys import warnings @@ -38,7 +38,7 @@ class KedroPythonVersionWarning(UserWarning): ) -def load_ipython_extension(ipython: InteractiveShellABC) -> None: +def load_ipython_extension(ipython: InteractiveShell) -> None: import kedro.ipython kedro.ipython.load_ipython_extension(ipython) diff --git a/kedro/ipython/__init__.py b/kedro/ipython/__init__.py index 12f77bddfc..d2b83911fb 100644 --- a/kedro/ipython/__init__.py +++ b/kedro/ipython/__init__.py @@ -19,7 +19,7 @@ if TYPE_CHECKING: from collections import OrderedDict - from IPython.core.interactiveshell import InteractiveShellABC + from IPython.core.interactiveshell import InteractiveShell from IPython.core.getipython import get_ipython from IPython.core.magic import needs_local_scope, register_line_magic @@ -52,7 +52,7 @@ RICH_INSTALLED = True if importlib.util.find_spec("rich") is not None else False -def load_ipython_extension(ipython: InteractiveShellABC) -> None: +def load_ipython_extension(ipython: InteractiveShell) -> None: """ Main entry point when %load_ext kedro.ipython is executed, either manually or automatically through `kedro ipython` or `kedro jupyter lab/notebook`. From 03dc8d849fd07b5ec2ef1238cb2e1d0e3a0eaf1c Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Tue, 14 Jan 2025 10:34:22 -0600 Subject: [PATCH 7/8] fix lint Signed-off-by: ravi_kumar_pilla --- kedro/ipython/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kedro/ipython/__init__.py b/kedro/ipython/__init__.py index d2b83911fb..ab273b0213 100644 --- a/kedro/ipython/__init__.py +++ b/kedro/ipython/__init__.py @@ -59,9 +59,9 @@ def load_ipython_extension(ipython: InteractiveShell) -> None: IPython will look for this function specifically. See https://ipython.readthedocs.io/en/stable/config/extensions/index.html """ - ipython.register_magic_function(magic_reload_kedro, magic_name="reload_kedro") + ipython.register_magic_function(func=magic_reload_kedro, magic_name="reload_kedro") logger.info("Registered line magic '%reload_kedro'") - ipython.register_magic_function(magic_load_node, magic_name="load_node") + ipython.register_magic_function(func=magic_load_node, magic_name="load_node") logger.info("Registered line magic '%load_node'") if _find_kedro_project(Path.cwd()) is None: From 962f1048cf647376e95bbd5359c411c2aab3b96e Mon Sep 17 00:00:00 2001 From: ravi_kumar_pilla Date: Tue, 14 Jan 2025 11:40:30 -0600 Subject: [PATCH 8/8] mypy ignore Signed-off-by: ravi_kumar_pilla --- kedro/ipython/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kedro/ipython/__init__.py b/kedro/ipython/__init__.py index ab273b0213..20578a3e7c 100644 --- a/kedro/ipython/__init__.py +++ b/kedro/ipython/__init__.py @@ -59,9 +59,9 @@ def load_ipython_extension(ipython: InteractiveShell) -> None: IPython will look for this function specifically. See https://ipython.readthedocs.io/en/stable/config/extensions/index.html """ - ipython.register_magic_function(func=magic_reload_kedro, magic_name="reload_kedro") + ipython.register_magic_function(func=magic_reload_kedro, magic_name="reload_kedro") # type: ignore[call-arg] logger.info("Registered line magic '%reload_kedro'") - ipython.register_magic_function(func=magic_load_node, magic_name="load_node") + ipython.register_magic_function(func=magic_load_node, magic_name="load_node") # type: ignore[call-arg] logger.info("Registered line magic '%load_node'") if _find_kedro_project(Path.cwd()) is None: