From 16e0385e6e2cf7066f249e71d00fa9972a4cefd0 Mon Sep 17 00:00:00 2001 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: Thu, 22 Feb 2024 10:36:07 +0000 Subject: [PATCH 1/2] Adopt to IPython 8.22 --- README.md | 2 +- ipython_markdown_inspector/__init__.py | 25 ++---------- ipython_markdown_inspector/_hook_data.py | 9 ----- ipython_markdown_inspector/_ipython_patch.py | 40 ------------------- ipython_markdown_inspector/formatter.py | 3 +- .../tests/test_as_markdown.py | 2 +- pyproject.toml | 6 +-- 7 files changed, 9 insertions(+), 78 deletions(-) delete mode 100644 ipython_markdown_inspector/_hook_data.py delete mode 100644 ipython_markdown_inspector/_ipython_patch.py diff --git a/README.md b/README.md index 1b1c056..1a3c5cf 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Depends on [`docstring-to-markdown`](https://github.com/python-lsp/docstring-to- ## Installation -Requires `IPython` 8.21 or newer (which requires Python 3.10 or newer). +Requires `IPython` 8.22 or newer (which requires Python 3.10 or newer). ```bash pip install ipython-markdown-inspector diff --git a/ipython_markdown_inspector/__init__.py b/ipython_markdown_inspector/__init__.py index d462c7c..ac6a15b 100644 --- a/ipython_markdown_inspector/__init__.py +++ b/ipython_markdown_inspector/__init__.py @@ -1,36 +1,17 @@ from functools import partial -from typing import Any, List, Optional, Union +from typing import List from IPython.core.interactiveshell import InteractiveShell -from IPython.core.oinspect import OInfo +from IPython.core.oinspect import InspectorHookData -from ._hook_data import InspectorHookData from .formatter import as_markdown def hook( - obj_or_data: Union[InspectorHookData, Any], - info: Optional[OInfo] = None, + data: InspectorHookData, *_, ipython: InteractiveShell, ) -> str: - if isinstance(obj_or_data, InspectorHookData): - data = obj_or_data - else: - # fallback for IPython 8.21 - obj = obj_or_data - detail_level = 0 - omit_sections: List[str] = [] - info_dict = ipython.inspector.info( - obj, "", info=info, detail_level=detail_level - ) - data = InspectorHookData( - obj=obj, - info=info, - info_dict=info_dict, - detail_level=detail_level, - omit_sections=omit_sections, - ) return as_markdown(data) diff --git a/ipython_markdown_inspector/_hook_data.py b/ipython_markdown_inspector/_hook_data.py deleted file mode 100644 index 67f75f0..0000000 --- a/ipython_markdown_inspector/_hook_data.py +++ /dev/null @@ -1,9 +0,0 @@ -try: - from IPython.core.oinspect import InspectorHookData # type: ignore -except ImportError: - # TODO: remove once we require a version which includes - # https://github.com/ipython/ipython/pull/14342 - from ._ipython_patch import InspectorHookData - - -__all__ = ["InspectorHookData"] diff --git a/ipython_markdown_inspector/_ipython_patch.py b/ipython_markdown_inspector/_ipython_patch.py deleted file mode 100644 index 3391098..0000000 --- a/ipython_markdown_inspector/_ipython_patch.py +++ /dev/null @@ -1,40 +0,0 @@ -from typing import Any, TypedDict, Optional -from dataclasses import dataclass - -from IPython.core.oinspect import OInfo - - -class InfoDict(TypedDict): - type_name: Optional[str] - base_class: Optional[str] - string_form: Optional[str] - namespace: Optional[str] - length: Optional[str] - file: Optional[str] - definition: Optional[str] - docstring: Optional[str] - source: Optional[str] - init_definition: Optional[str] - class_docstring: Optional[str] - init_docstring: Optional[str] - call_def: Optional[str] - call_docstring: Optional[str] - subclasses: Optional[str] - # These won't be printed but will be used to determine how to - # format the object - ismagic: bool - isalias: bool - isclass: bool - found: bool - name: str - - -@dataclass -class InspectorHookData: - """Data passed to the mime hook""" - - obj: Any - info: Optional[OInfo] - info_dict: InfoDict - detail_level: int - omit_sections: list[str] diff --git a/ipython_markdown_inspector/formatter.py b/ipython_markdown_inspector/formatter.py index f79ec08..6cffc50 100644 --- a/ipython_markdown_inspector/formatter.py +++ b/ipython_markdown_inspector/formatter.py @@ -1,9 +1,8 @@ from typing import Dict, List import docstring_to_markdown -from IPython.core.oinspect import is_simple_callable +from IPython.core.oinspect import is_simple_callable, InspectorHookData -from ._hook_data import InspectorHookData from .models import Field, CodeField, DocField, RowField diff --git a/ipython_markdown_inspector/tests/test_as_markdown.py b/ipython_markdown_inspector/tests/test_as_markdown.py index f5198cd..29df52e 100644 --- a/ipython_markdown_inspector/tests/test_as_markdown.py +++ b/ipython_markdown_inspector/tests/test_as_markdown.py @@ -1,9 +1,9 @@ from IPython.core.interactiveshell import InteractiveShell +from IPython.core.oinspect import InspectorHookData from IPython import get_ipython import pytest from ipython_markdown_inspector.formatter import as_markdown -from ipython_markdown_inspector._hook_data import InspectorHookData def simple_func(arg): diff --git a/pyproject.toml b/pyproject.toml index c4b30d6..1bd6bff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,10 +4,10 @@ build-backend = "hatchling.build" [project] name = "ipython-markdown-inspector" -version = "0.0.0" +version = "1.0.0" dependencies = [ - "ipython>=8.21.0", - "docstring-to-markdown>=0.14.0,<1.0.0" + "ipython>=8.22.0", + "docstring-to-markdown>=0.15.0,<1.0.0" ] requires-python = ">=3.10" authors = [ From d396e31859ca8afbd51f52ef667d4b0265d58491 Mon Sep 17 00:00:00 2001 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: Thu, 22 Feb 2024 10:59:47 +0000 Subject: [PATCH 2/2] Better typing --- ipython_markdown_inspector/formatter.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ipython_markdown_inspector/formatter.py b/ipython_markdown_inspector/formatter.py index 6cffc50..412b3ef 100644 --- a/ipython_markdown_inspector/formatter.py +++ b/ipython_markdown_inspector/formatter.py @@ -1,4 +1,4 @@ -from typing import Dict, List +from typing import cast, Dict, List import docstring_to_markdown from IPython.core.oinspect import is_simple_callable, InspectorHookData @@ -50,7 +50,7 @@ """ -def markdown_formatter(text: str): +def markdown_formatter(text: str) -> str: try: converted = docstring_to_markdown.convert(text) return converted @@ -58,7 +58,7 @@ def markdown_formatter(text: str): return f"
{text}" -def code_formatter(code, language="python"): +def code_formatter(code: str, language="python") -> str: return f"```{language}\n{code}\n```" @@ -96,8 +96,10 @@ def as_markdown(data: InspectorHookData) -> str: chunks.append(TABLE_STARTER) chunks[-1] += f"\n| {field.label} | `{value}` |" if field.kind == "code": - chunks.append(f"#### {field.label}\n\n" + code_formatter(value)) + chunks.append(f"#### {field.label}\n\n" + code_formatter(cast(str, value))) if field.kind == "doc": - chunks.append(f"#### {field.label}\n\n" + markdown_formatter(value)) + chunks.append( + f"#### {field.label}\n\n" + markdown_formatter(cast(str, value)) + ) return "\n\n".join(chunks)