Skip to content

Commit

Permalink
typing extension on 3.10 before
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Mar 23, 2023
1 parent 844f4c5 commit 765ef0c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
51 changes: 31 additions & 20 deletions IPython/core/oinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

UnformattedBundle: TypeAlias = Dict[str, List[Tuple[str, str]]]
Bundle: TypeAlias = Dict[str, List[str]]

UnformattedBundle: TypeAlias = Dict[str, List[Tuple[str, str]]] # List of (title, body)
Bundle: TypeAlias = Dict[str, str]


@dataclass
Expand Down Expand Up @@ -577,27 +578,37 @@ def format_mime(self, bundle: UnformattedBundle) -> Bundle:
assert isinstance(bundle["text/plain"], list)
for item in bundle["text/plain"]:
assert isinstance(item, tuple)
if isinstance(bundle["text/plain"], (list, tuple)):
# bundle['text/plain'] is a list of (head, formatted body) pairs
lines = []
_len = max(len(h) for h, _ in bundle["text/plain"])

for head, body in bundle["text/plain"]:
body = body.strip("\n")
delim = "\n" if "\n" in body else " "
lines.append(
f"{self.__head(head+':')}{(_len - len(head))*' '}{delim}{body}"
)

bundle["text/plain"] = "\n".join(lines)
new_b: Bundle = {}
lines = []
_len = max(len(h) for h, _ in bundle["text/plain"])

# Format the text/html mimetype
if isinstance(bundle["text/html"], (list, tuple)):
# bundle['text/html'] is a list of (head, formatted body) pairs
bundle["text/html"] = "\n".join(
(f"<h1>{head}</h1>\n{body}" for (head, body) in bundle["text/html"])
for head, body in bundle["text/plain"]:
body = body.strip("\n")
delim = "\n" if "\n" in body else " "
lines.append(
f"{self.__head(head+':')}{(_len - len(head))*' '}{delim}{body}"
)
return bundle

new_b["text/plain"] = "\n".join(lines)

if "text/html" in bundle:
assert isinstance(bundle["text/html"], list)
for item in bundle["text/html"]:
assert isinstance(item, tuple)
# Format the text/html mimetype
if isinstance(bundle["text/html"], (list, tuple)):
# bundle['text/html'] is a list of (head, formatted body) pairs
new_b["text/html"] = "\n".join(
(f"<h1>{head}</h1>\n{body}" for (head, body) in bundle["text/html"])
)

for k in bundle.keys():
if k in ("text/html", "text/plain"):
continue
else:
new_b = bundle[k] # type:ignore
return new_b

def _append_info_field(
self,
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ install_requires =
pygments>=2.4.0
stack_data
traitlets>=5
typing_extensions ; python_version<'3.10'"

[options.extras_require]
black =
Expand All @@ -54,7 +55,6 @@ doc =
matplotlib
stack_data
pytest<7
typing_extensions
%(test)s
kernel =
ipykernel
Expand Down

0 comments on commit 765ef0c

Please sign in to comment.