Skip to content

Commit

Permalink
Adopt to IPython 8.22 (#1)
Browse files Browse the repository at this point in the history
* Adopt to IPython 8.22

* Better typing
  • Loading branch information
krassowski authored Feb 22, 2024
1 parent 4d7a8fc commit 21b0a76
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 3 additions & 22 deletions ipython_markdown_inspector/__init__.py
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
9 changes: 0 additions & 9 deletions ipython_markdown_inspector/_hook_data.py

This file was deleted.

40 changes: 0 additions & 40 deletions ipython_markdown_inspector/_ipython_patch.py

This file was deleted.

15 changes: 8 additions & 7 deletions ipython_markdown_inspector/formatter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Dict, List
from typing import cast, 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


Expand Down Expand Up @@ -51,15 +50,15 @@
"""


def markdown_formatter(text: str):
def markdown_formatter(text: str) -> str:
try:
converted = docstring_to_markdown.convert(text)
return converted
except docstring_to_markdown.UnknownFormatError:
return f"<pre>{text}</pre>"


def code_formatter(code, language="python"):
def code_formatter(code: str, language="python") -> str:
return f"```{language}\n{code}\n```"


Expand Down Expand Up @@ -97,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)
2 changes: 1 addition & 1 deletion ipython_markdown_inspector/tests/test_as_markdown.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 21b0a76

Please sign in to comment.