From 9622e867a77199899ca6d1adbd9a6f78f73e4655 Mon Sep 17 00:00:00 2001 From: Jiya Gupta Date: Fri, 4 Apr 2025 23:39:25 +0530 Subject: [PATCH 1/6] changed lang_indicator --- .../jupyter_ai_magics/magics.py | 2 +- .../jupyter_ai_magics/tests/test_magics.py | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py index ebfdf7b4a..71851de09 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py @@ -479,7 +479,7 @@ def display_output(self, output, display_format, md): # if the user wants code, add another cell with the output. if display_format == "code": # Strip a leading language indicator and trailing triple-backticks - lang_indicator = r"^```[a-zA-Z0-9]*\n" + lang_indicator = r"^\s*```[a-zA-Z0-9]\s\n" output = re.sub(lang_indicator, "", output) output = re.sub(r"\n```$", "", output) self.shell.set_next_input(output, replace=False) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py index bd440a97a..8e525e706 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py @@ -8,6 +8,11 @@ from langchain_core.messages import AIMessage, HumanMessage from pytest import fixture from traitlets.config.loader import Config +import re +import json +from unittest.mock import Mock +from IPython.display import HTML, Markdown, JSON +from jupyter_ai_magics.magics import AiMagics, DISPLAYS_BY_FORMAT @fixture @@ -121,3 +126,49 @@ def test_reset(ip): ai_magics.transcript = [AI1, H1, AI2, H2, AI3] result = ip.run_line_magic("ai", "reset") assert ai_magics.transcript == [] + class DummyShell: + def __init__(self): + self.set_next_input = Mock() + self.user_ns = {} + self.execution_count = 1 + + @pytest.fixture + def dummy_shell(): + return DummyShell() + + @pytest.fixture + def ai_magics(dummy_shell): + return AiMagics(shell=dummy_shell) + + def test_display_output_code_format(ai_magics, dummy_shell): + original_output = " ```python\n" + "def add(a, b):\n return a+b" + "\n```" + md = {"test_meta": "value"} + result = ai_magics.display_output(original_output, "code", md) + + expected_output = "def add(a, b):\n return a+b" + dummy_shell.set_next_input.assert_called_once_with(expected_output, replace=False) + + assert isinstance(result, HTML) + assert result.metadata == md + assert "AI generated code inserted below" in result.data + + def test_display_output_markdown_format(ai_magics): + output_text = "This is **markdown** output." + md = {"jupyter_ai": {"dummy": "test"}} + result = ai_magics.display_output(output_text, "markdown", md) + assert isinstance(result, Markdown) + assert result.data == output_text + bundle = result._repr_mimebundle_() + assert bundle.get("text/markdown") == output_text + + def test_display_output_json_format(ai_magics): + data = {"key": "value", "number": 42} + json_string = json.dumps(data) + md = {"jupyter_ai": {"dummy": "json"}} + result = ai_magics.display_output(json_string, "json", md) + assert isinstance(result, JSON) + bundle = result._repr_mimebundle_() + json_data = bundle.get("application/json") or bundle.get("text/json") + if isinstance(json_data, str): + json_data = json.loads(json_data) + assert json_data == data From bb2ab6dd2cc51ee7537e6dc666c8e419580ab51b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Apr 2025 18:14:22 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../jupyter_ai_magics/tests/test_magics.py | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py index 8e525e706..069e7d6b7 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py @@ -1,18 +1,16 @@ +import json import os +import re from unittest.mock import Mock, patch import pytest from IPython import InteractiveShell from IPython.core.display import Markdown -from jupyter_ai_magics.magics import AiMagics +from IPython.display import HTML, JSON, Markdown +from jupyter_ai_magics.magics import DISPLAYS_BY_FORMAT, AiMagics from langchain_core.messages import AIMessage, HumanMessage from pytest import fixture from traitlets.config.loader import Config -import re -import json -from unittest.mock import Mock -from IPython.display import HTML, Markdown, JSON -from jupyter_ai_magics.magics import AiMagics, DISPLAYS_BY_FORMAT @fixture @@ -126,8 +124,9 @@ def test_reset(ip): ai_magics.transcript = [AI1, H1, AI2, H2, AI3] result = ip.run_line_magic("ai", "reset") assert ai_magics.transcript == [] + class DummyShell: - def __init__(self): + def __init__(self): self.set_next_input = Mock() self.user_ns = {} self.execution_count = 1 @@ -144,10 +143,12 @@ def test_display_output_code_format(ai_magics, dummy_shell): original_output = " ```python\n" + "def add(a, b):\n return a+b" + "\n```" md = {"test_meta": "value"} result = ai_magics.display_output(original_output, "code", md) - + expected_output = "def add(a, b):\n return a+b" - dummy_shell.set_next_input.assert_called_once_with(expected_output, replace=False) - + dummy_shell.set_next_input.assert_called_once_with( + expected_output, replace=False + ) + assert isinstance(result, HTML) assert result.metadata == md assert "AI generated code inserted below" in result.data @@ -171,4 +172,4 @@ def test_display_output_json_format(ai_magics): json_data = bundle.get("application/json") or bundle.get("text/json") if isinstance(json_data, str): json_data = json.loads(json_data) - assert json_data == data + assert json_data == data From 304453eff7c6bffe0c91e27eef5d804143ec46f7 Mon Sep 17 00:00:00 2001 From: Jiya Gupta Date: Sat, 5 Apr 2025 16:07:40 +0530 Subject: [PATCH 3/6] use greedy operators --- .../jupyter_ai_magics/magics.py | 2 +- .../jupyter_ai_magics/tests/test_magics.py | 84 +++++++++---------- 2 files changed, 43 insertions(+), 43 deletions(-) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py index 71851de09..0bed2e7fb 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/magics.py @@ -479,7 +479,7 @@ def display_output(self, output, display_format, md): # if the user wants code, add another cell with the output. if display_format == "code": # Strip a leading language indicator and trailing triple-backticks - lang_indicator = r"^\s*```[a-zA-Z0-9]\s\n" + lang_indicator = r"^\s*```[a-zA-Z0-9]*\s*\n" output = re.sub(lang_indicator, "", output) output = re.sub(r"\n```$", "", output) self.shell.set_next_input(output, replace=False) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py index 8e525e706..056085154 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py @@ -126,49 +126,49 @@ def test_reset(ip): ai_magics.transcript = [AI1, H1, AI2, H2, AI3] result = ip.run_line_magic("ai", "reset") assert ai_magics.transcript == [] - class DummyShell: - def __init__(self): +class DummyShell: + def __init__(self): self.set_next_input = Mock() self.user_ns = {} self.execution_count = 1 - @pytest.fixture - def dummy_shell(): - return DummyShell() - - @pytest.fixture - def ai_magics(dummy_shell): - return AiMagics(shell=dummy_shell) - - def test_display_output_code_format(ai_magics, dummy_shell): - original_output = " ```python\n" + "def add(a, b):\n return a+b" + "\n```" - md = {"test_meta": "value"} - result = ai_magics.display_output(original_output, "code", md) - - expected_output = "def add(a, b):\n return a+b" - dummy_shell.set_next_input.assert_called_once_with(expected_output, replace=False) - - assert isinstance(result, HTML) - assert result.metadata == md - assert "AI generated code inserted below" in result.data - - def test_display_output_markdown_format(ai_magics): - output_text = "This is **markdown** output." - md = {"jupyter_ai": {"dummy": "test"}} - result = ai_magics.display_output(output_text, "markdown", md) - assert isinstance(result, Markdown) - assert result.data == output_text - bundle = result._repr_mimebundle_() - assert bundle.get("text/markdown") == output_text - - def test_display_output_json_format(ai_magics): - data = {"key": "value", "number": 42} - json_string = json.dumps(data) - md = {"jupyter_ai": {"dummy": "json"}} - result = ai_magics.display_output(json_string, "json", md) - assert isinstance(result, JSON) - bundle = result._repr_mimebundle_() - json_data = bundle.get("application/json") or bundle.get("text/json") - if isinstance(json_data, str): - json_data = json.loads(json_data) - assert json_data == data +@pytest.fixture +def dummy_shell(): + return DummyShell() + +@pytest.fixture +def ai_magics(dummy_shell): + return AiMagics(shell=dummy_shell) + +def test_display_output_code_format(ai_magics, dummy_shell): + original_output = " ```python\n" + "def add(a, b):\n return a+b" + "\n```" + md = {"test_meta": "value"} + result = ai_magics.display_output(original_output, "code", md) + + expected_output = "def add(a, b):\n return a+b" + dummy_shell.set_next_input.assert_called_once_with(expected_output, replace=False) + + assert isinstance(result, HTML) + assert result.metadata == md + assert "AI generated code inserted below" in result.data + +def test_display_output_markdown_format(ai_magics): + output_text = "This is **markdown** output." + md = {"jupyter_ai": {"dummy": "test"}} + result = ai_magics.display_output(output_text, "markdown", md) + assert isinstance(result, Markdown) + assert result.data == output_text + bundle = result._repr_mimebundle_() + assert bundle.get("text/markdown") == output_text + +def test_display_output_json_format(ai_magics): + data = {"key": "value", "number": 42} + json_string = json.dumps(data) + md = {"jupyter_ai": {"dummy": "json"}} + result = ai_magics.display_output(json_string, "json", md) + assert isinstance(result, JSON) + bundle = result._repr_mimebundle_() + json_data = bundle.get("application/json") or bundle.get("text/json") + if isinstance(json_data, str): + json_data = json.loads(json_data) + assert json_data == data From 81e17c4c32c075f99a8eb284438d92aa9a66b79c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Apr 2025 10:44:02 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py index 629a6c1d5..7d8e0253e 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py @@ -192,7 +192,7 @@ def test_display_output_json_format(ai_magics): json_data = bundle.get("application/json") or bundle.get("text/json") if isinstance(json_data, str): json_data = json.loads(json_data) - assert json_data == data + assert json_data == data ======= def test_display_output_json_format(ai_magics): data = {"key": "value", "number": 42} From f35215c4ee1c55ced1e47ee7e3d9bc4527672e48 Mon Sep 17 00:00:00 2001 From: Jiya Gupta Date: Sat, 5 Apr 2025 16:20:16 +0530 Subject: [PATCH 5/6] fix regex --- .../jupyter_ai_magics/tests/test_magics.py | 39 +++++-------------- 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py index 7d8e0253e..d9089e4da 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py @@ -1,16 +1,18 @@ -import json import os -import re from unittest.mock import Mock, patch import pytest from IPython import InteractiveShell from IPython.core.display import Markdown -from IPython.display import HTML, JSON, Markdown -from jupyter_ai_magics.magics import DISPLAYS_BY_FORMAT, AiMagics +from jupyter_ai_magics.magics import AiMagics from langchain_core.messages import AIMessage, HumanMessage from pytest import fixture from traitlets.config.loader import Config +import re +import json +from unittest.mock import Mock +from IPython.display import HTML, Markdown, JSON +from jupyter_ai_magics.magics import AiMagics, DISPLAYS_BY_FORMAT @fixture @@ -124,14 +126,8 @@ def test_reset(ip): ai_magics.transcript = [AI1, H1, AI2, H2, AI3] result = ip.run_line_magic("ai", "reset") assert ai_magics.transcript == [] -<<<<<<< HEAD class DummyShell: def __init__(self): -======= - - class DummyShell: - def __init__(self): ->>>>>>> bb2ab6dd2cc51ee7537e6dc666c8e419580ab51b self.set_next_input = Mock() self.user_ns = {} self.execution_count = 1 @@ -144,31 +140,14 @@ def dummy_shell(): def ai_magics(dummy_shell): return AiMagics(shell=dummy_shell) -<<<<<<< HEAD def test_display_output_code_format(ai_magics, dummy_shell): original_output = " ```python\n" + "def add(a, b):\n return a+b" + "\n```" md = {"test_meta": "value"} result = ai_magics.display_output(original_output, "code", md) -======= - def test_display_output_code_format(ai_magics, dummy_shell): - original_output = " ```python\n" + "def add(a, b):\n return a+b" + "\n```" - md = {"test_meta": "value"} - result = ai_magics.display_output(original_output, "code", md) - - expected_output = "def add(a, b):\n return a+b" - dummy_shell.set_next_input.assert_called_once_with( - expected_output, replace=False - ) - - assert isinstance(result, HTML) - assert result.metadata == md - assert "AI generated code inserted below" in result.data ->>>>>>> bb2ab6dd2cc51ee7537e6dc666c8e419580ab51b expected_output = "def add(a, b):\n return a+b" dummy_shell.set_next_input.assert_called_once_with(expected_output, replace=False) -<<<<<<< HEAD assert isinstance(result, HTML) assert result.metadata == md assert "AI generated code inserted below" in result.data @@ -192,8 +171,8 @@ def test_display_output_json_format(ai_magics): json_data = bundle.get("application/json") or bundle.get("text/json") if isinstance(json_data, str): json_data = json.loads(json_data) - assert json_data == data -======= + assert json_data == data + def test_display_output_json_format(ai_magics): data = {"key": "value", "number": 42} json_string = json.dumps(data) @@ -205,4 +184,4 @@ def test_display_output_json_format(ai_magics): if isinstance(json_data, str): json_data = json.loads(json_data) assert json_data == data ->>>>>>> bb2ab6dd2cc51ee7537e6dc666c8e419580ab51b + From dd2e8c1d6bacff7d461b3c51ebfee1fecf061bc2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Apr 2025 10:50:45 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../jupyter_ai_magics/tests/test_magics.py | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py index d9089e4da..cede18db2 100644 --- a/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py +++ b/packages/jupyter-ai-magics/jupyter_ai_magics/tests/test_magics.py @@ -1,18 +1,16 @@ +import json import os +import re from unittest.mock import Mock, patch import pytest from IPython import InteractiveShell from IPython.core.display import Markdown -from jupyter_ai_magics.magics import AiMagics +from IPython.display import HTML, JSON, Markdown +from jupyter_ai_magics.magics import DISPLAYS_BY_FORMAT, AiMagics from langchain_core.messages import AIMessage, HumanMessage from pytest import fixture from traitlets.config.loader import Config -import re -import json -from unittest.mock import Mock -from IPython.display import HTML, Markdown, JSON -from jupyter_ai_magics.magics import AiMagics, DISPLAYS_BY_FORMAT @fixture @@ -126,20 +124,25 @@ def test_reset(ip): ai_magics.transcript = [AI1, H1, AI2, H2, AI3] result = ip.run_line_magic("ai", "reset") assert ai_magics.transcript == [] + + class DummyShell: def __init__(self): - self.set_next_input = Mock() - self.user_ns = {} - self.execution_count = 1 + self.set_next_input = Mock() + self.user_ns = {} + self.execution_count = 1 + @pytest.fixture def dummy_shell(): return DummyShell() + @pytest.fixture def ai_magics(dummy_shell): return AiMagics(shell=dummy_shell) + def test_display_output_code_format(ai_magics, dummy_shell): original_output = " ```python\n" + "def add(a, b):\n return a+b" + "\n```" md = {"test_meta": "value"} @@ -152,6 +155,7 @@ def test_display_output_code_format(ai_magics, dummy_shell): assert result.metadata == md assert "AI generated code inserted below" in result.data + def test_display_output_markdown_format(ai_magics): output_text = "This is **markdown** output." md = {"jupyter_ai": {"dummy": "test"}} @@ -161,6 +165,7 @@ def test_display_output_markdown_format(ai_magics): bundle = result._repr_mimebundle_() assert bundle.get("text/markdown") == output_text + def test_display_output_json_format(ai_magics): data = {"key": "value", "number": 42} json_string = json.dumps(data) @@ -171,7 +176,7 @@ def test_display_output_json_format(ai_magics): json_data = bundle.get("application/json") or bundle.get("text/json") if isinstance(json_data, str): json_data = json.loads(json_data) - assert json_data == data + assert json_data == data def test_display_output_json_format(ai_magics): data = {"key": "value", "number": 42} @@ -184,4 +189,3 @@ def test_display_output_json_format(ai_magics): if isinstance(json_data, str): json_data = json.loads(json_data) assert json_data == data -