Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle classes in inspect when methods=True #1874

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion rich/_inspect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

import inspect
from inspect import cleandoc, getdoc, getfile, isclass, ismodule, signature
from typing import Any, Iterable, Optional, Tuple

Expand Down Expand Up @@ -106,8 +107,17 @@ def _get_signature(self, name: str, obj: Any) -> Optional[Text]:
signature_text = self.highlighter(_signature)

qualname = name or getattr(obj, "__qualname__", name)

# If obj is a module, there may be classes (which are callable) to display
if inspect.isclass(obj):
darrenburns marked this conversation as resolved.
Show resolved Hide resolved
prefix = "class"
else:
prefix = "def"

qual_signature = Text.assemble(
("def ", "inspect.def"), (qualname, "inspect.callable"), signature_text
(f"{prefix} ", f"inspect.{prefix}"),
(qualname, "inspect.callable"),
signature_text,
)

return qual_signature
Expand Down
2 changes: 1 addition & 1 deletion rich/default_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from .style import Style


DEFAULT_STYLES: Dict[str, Style] = {
"none": Style.null(),
"reset": Style(
Expand Down Expand Up @@ -41,6 +40,7 @@
"inspect.attr.dunder": Style(color="yellow", italic=True, dim=True),
"inspect.callable": Style(bold=True, color="red"),
"inspect.def": Style(italic=True, color="bright_cyan"),
"inspect.class": Style(italic=True, color="bright_cyan"),
"inspect.error": Style(bold=True, color="red"),
"inspect.equals": Style(),
"inspect.help": Style(color="cyan"),
Expand Down