resolved RESOLVED PASS · 13 tool calls · 163 s · Textualize/rich
Handle unusual __qualname__ in inspect
<!--
Please note that Rich isn't accepting any new features at this point.
If a feature can be implemented without modifying the core library, then
they should be released as a third-party module. I can accept updates to the
core library that make it easier to extend (think hooks).
Bugfixes are always welcome of course.
Sometimes it is not clear what is a feature and what is a bug fix.
If there is any doubt, please open a discussion first.
-->
<!--
*Are you contributing typo fixes?*
If your PR solely consists of typos, at least one must be in the docs to warrant an addition to CONTRIBUTORS.md
-->
## Type of changes
- [x] Bug fix
- [ ] New feature
- [ ] Documentation / docstrings
- [x] Tests
- [ ] Other
## Checklist
- [x] I've run the latest [black](https://github.com/psf/black) with default args on new code.
- [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate (see note about typos above).
- [x] I've added tests for new code.
- [x] I accept that @willmcgugan may be pedantic in the code review.
## Description
I discovered, that an object with an unusual `__qualname__` attribute (not a `str`, e.g. a descriptor) causes `inspect` to fail.
This can happen if an object is created this way intentionally, but I discovered it, when decorating a class with `@functools.lru_cache` which forwards the underlying attributes using a descriptor when it can't add the to it's `__dict__` because the class uses `__slots__`:
```python
from functools import lru_cache
from rich import inspect
@lru_cache
class Klaas:
__slots__ = ("__qualname__",)
if __name__ == "__main__":
from rich.traceback import install
install()
inspect(Klaas.__qualname__)
inspect(Klaas, all=True)
```
I fixed it by checking the type of `__qualname__` falling back to `__name__` and if that isn't a `str` using the already provided fallback.
| # | Tool | Arguments | Result |
|---|---|---|---|
| 1 | run_command | {
"command": "python3 - <<'PY'\nfrom functools import lru_cache\nfrom rich import inspect\n\n@lru_cache\nclass Klaas:\n __slots__ = (\"__qualname__\",)\n\nif __name__ == \"__main__\":\n try:\n inspect(Klaas.__qualname__)\n inspect(Klaas, all=True)\n print(\"Success\")\n except Exception as e:\n print(f\"Failed: {e}\")\n import traceback\n traceback.print_exc()\nPY\n"
} | {"status": "ok", "stdout": "\u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 <class 'member_descriptor'> \full{"status": "ok", "stdout": "\u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 <class 'member_descriptor'> \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n\u2502 \u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e \u2502\n\u2502 \u2502 <member '__qualname__' of 'Klaas' objects> \u2502 \u2502\n\u2502 \u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f \u2502\n\u2502 \u2502\n\u2502 30 attribute(s) not shown. Run inspect(inspect) for options. \u2502\n\u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f\nFailed: Only str or Text can be appended to Text\n", "stderr": "Traceback (most recent call last):\n File \"<stdin>\", line 11, in <module>\n File \"/private/workspace/rich/__init__.py\", line 173, in inspect\n _console.print(_inspect)\n ~~~~~~~~~~~~~~^^^^^^^^^^\n File \"/private/workspace/rich/console.py\", line 1698, in print\n renderables |
| 2 | run_command | {
"command": "grep -n \"qualname\" rich/_inspect.py"
} | {"status": "ok", "stdout": "103: qualname = name or getattr(obj, \"__qualname__\", name)\n115: (qualname, \"inspect.callable\"),\n253: f'{getattr(type_, \"__modfull{"status": "ok", "stdout": "103: qualname = name or getattr(obj, \"__qualname__\", name)\n115: (qualname, \"inspect.callable\"),\n253: f'{getattr(type_, \"__module__\", \"\")}.{getattr(type_, \"__qualname__\", \"\")}'\n", "stderr": "", "exit_code": 0} |
| 3 | run_command | {
"command": "sed -n '100,120p' rich/_inspect.py"
} | {"status": "ok", "stdout": " callable_name.stylize(f\"link file://{source_filename}\")\n signature_text = self.highlighter(_signature)\n\n qualname = name or gefull{"status": "ok", "stdout": " callable_name.stylize(f\"link file://{source_filename}\")\n signature_text = self.highlighter(_signature)\n\n qualname = name or getattr(obj, \"__qualname__\", name)\n\n # If obj is a module, there may be classes (which are callable) to display\n if inspect.isclass(obj):\n prefix = \"class\"\n elif inspect.iscoroutinefunction(obj):\n prefix = \"async def\"\n else:\n prefix = \"def\"\n\n qual_signature = Text.assemble(\n (f\"{prefix} \", f\"inspect.{prefix.replace(' ', '_')}\"),\n (qualname, \"inspect.callable\"),\n signature_text,\n )\n\n return qual_signature\n\n", "stderr": "", "exit_code": 0} |
| 4 | run_command | {
"command": "sed -n '240,260p' rich/_inspect.py"
} | {"status": "ok", "stdout": " # some types of classes, such as the ones that use abc.ABCMeta.\n obj = type(obj)\n return getattr(obj, \"__mro__\", ())\n\n\ndef gfull{"status": "ok", "stdout": " # some types of classes, such as the ones that use abc.ABCMeta.\n obj = type(obj)\n return getattr(obj, \"__mro__\", ())\n\n\ndef get_object_types_mro_as_strings(obj: object) -> Collection[str]:\n \"\"\"\n Returns the MRO of an object's class as full qualified names, or of the object itself if it's a class.\n\n Examples:\n `object_types_mro_as_strings(JSONDecoder)` will return `['json.decoder.JSONDecoder', 'builtins.object']`\n \"\"\"\n return [\n f'{getattr(type_, \"__module__\", \"\")}.{getattr(type_, \"__qualname__\", \"\")}'\n for type_ in get_object_types_mro(obj)\n ]\n\n\ndef is_object_one_of_types(\n obj: object, fully_qualified_types_names: Collection[str]\n) -> bool:\n", "stderr": "", "exit_code": 0} |
| 5 | run_command | {
"command": "python3 - <<'PY'\nfrom functools import lru_cache\nfrom rich import inspect\n\n@lru_cache\nclass Klaas:\n __slots__ = (\"__qualname__\",)\n\nif __name__ == \"__main__\":\n try:\n # This is where it fails\n inspect(Klaas, all=True)\n print(\"Success\")\n except Exception as e:\n print(f\"Failed: {e}\")\n import traceback\n traceback.print_exc()\nPY\n"
} | {"status": "ok", "stdout": "Failed: Only str or Text can be appended to Text\n", "stderr": "Traceback (most recent call last):\n File \"<stdin>\", line 11, in <full{"status": "ok", "stdout": "Failed: Only str or Text can be appended to Text\n", "stderr": "Traceback (most recent call last):\n File \"<stdin>\", line 11, in <module>\n File \"/private/workspace/rich/__init__.py\", line 173, in inspect\n _console.print(_inspect)\n ~~~~~~~~~~~~~~^^^^^^^^^^\n File \"/private/workspace/rich/console.py\", line 1698, in print\n renderables = self._collect_renderables(\n objects,\n ...<5 lines>...\n highlight=highlight,\n )\n File \"/private/workspace/rich/console.py\", line 1555, in _collect_renderables\n renderable = rich_cast(renderable)\n File \"/private/workspace/rich/protocol.py\", line 36, in rich_cast\n renderable = cast_method()\n File \"/private/workspace/rich/_inspect.py\", line 76, in __rich__\n Group(*self._render()),\n ~~~~~^^^^^^^^^^^^^^^^^\n File \"/private/workspace/rich/_inspect.py\", line 153, in _render\n signature = self._get_signature(\"\", obj)\n File \"/private/workspace/rich/_inspect.py\", line 113, in _get_signature\n qual_signature = Text.assemble(\n (f\"{prefix} \", f\"inspect.{prefix.replace(' ', '_')}\"),\n (qualname, \"inspect.callable\"),\n signature_text,\n )\n File \"/private/workspace/rich/text.py\", line 397, in assemble\n append(*part)\n ~~~~~~^^^^^^^\n File \"/private/workspace/rich/text.py\", line 979, in append\n raise TypeError(\"Only str or Text can be appended to Text\")\nTypeError: Only str or Text can be appended to Text\n", "exit_code": 0} |
| 6 | run_command | {
"command": "python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"rich/_inspect.py\")\ns = p.read_text()\nold = \"\"\" qualname = name or getattr(obj, \"__qualname__\", name)\"\"\"\nnew = \"\"\" qualname = name or getattr(obj, \"__qualname__\", name)\n if not isinstance(qualname, str):\n qualname = getattr(obj, \"__name__\", name)\n if not isinstance(qualname, str):\n qualname = str(qualname)\"\"\"\nassert s.count(old) == 1, (\"old text found this many times:\", s.count(old))\nt = s.replace(old, new)\np.write_text(t)\nPY\n"
} | {"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0} |
| 7 | run_command | {
"command": "python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"rich/_inspect.py\")\ns = p.read_text()\nold = \"\"\" f'{getattr(type_, \"__module__\", \"\")}.{getattr(type_, \"__qualname__\", \"\")}'\"\"\"\nnew = \"\"\" qualname = getattr(type_, \"__qualname__\", \"\")\n if not isinstance(qualname, str):\n qualname = getattr(type_, \"__name__\", \"\")\n if not isinstance(qualname, str):\n qualname = str(qualname)\n f'{getattr(type_, \"__module__\", \"\")}.{qualname}'\"\"\"\n# Wait, the old was inside a list comprehension. I cannot just replace it with multiple lines.\nPY\n"
} | {"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0} |
| 8 | run_command | {
"command": "sed -n '250,255p' rich/_inspect.py"
} | {"status": "ok", "stdout": " \"\"\"\n Returns the MRO of an object's class as full qualified names, or of the object itself if it's a class.\n\n Examples:\n `obfull{"status": "ok", "stdout": " \"\"\"\n Returns the MRO of an object's class as full qualified names, or of the object itself if it's a class.\n\n Examples:\n `object_types_mro_as_strings(JSONDecoder)` will return `['json.decoder.JSONDecoder', 'builtins.object']`\n \"\"\"\n", "stderr": "", "exit_code": 0} |
| 9 | run_command | {
"command": "sed -n '256,260p' rich/_inspect.py"
} | {"status": "ok", "stdout": " return [\n f'{getattr(type_, \"__module__\", \"\")}.{getattr(type_, \"__qualname__\", \"\")}'\n for type_ in get_object_types_mro(ofull{"status": "ok", "stdout": " return [\n f'{getattr(type_, \"__module__\", \"\")}.{getattr(type_, \"__qualname__\", \"\")}'\n for type_ in get_object_types_mro(obj)\n ]\n\n", "stderr": "", "exit_code": 0} |
| 10 | run_command | {
"command": "python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"rich/_inspect.py\")\ns = p.read_text()\nold = \"\"\" return [\n f'{getattr(type_, \"__module__\", \"\")}.{getattr(type_, \"__qualname__\", \"\")}'\n for type_ in get_object_types_mro(obj)\n ]\"\"\"\nnew = \"\"\" mro_strings = []\n for type_ in get_object_types_mro(obj):\n qualname = getattr(type_, \"__qualname__\", \"\")\n if not isinstance(qualname, str):\n qualname = getattr(type_, \"__name__\", \"\")\n if not isinstance(qualname, str):\n qualname = str(qualname)\n mro_strings.append(f'{getattr(type_, \"__module__\", \"\")}.{qualname}')\n return mro_strings\"\"\"\nassert s.count(old) == 1, (\"old text found this many times:\", s.count(old))\nt = s.replace(old, new)\np.write_text(t)\nPY\n"
} | {"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}full{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0} |
| 11 | run_command | {
"command": "python3 - <<'PY'\nfrom functools import lru_cache\nfrom rich import inspect\n\n@lru_cache\nclass Klaas:\n __slots__ = (\"__qualname__\",)\n\nif __name__ == \"__main__\":\n try:\n inspect(Klaas.__qualname__)\n inspect(Klaas, all=True)\n print(\"Success\")\n except Exception as e:\n print(f\"Failed: {e}\")\n import traceback\n traceback.print_exc()\nPY\n"
} | {"status": "ok", "stdout": "\u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 <class 'member_descriptor'> \full{"status": "ok", "stdout": "\u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 <class 'member_descriptor'> \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n\u2502 \u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e \u2502\n\u2502 \u2502 <member '__qualname__' of 'Klaas' objects> \u2502 \u2502\n\u2502 \u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f \u2502\n\u2502 \u2502\n\u2502 30 attribute(s) not shown. Run inspect(inspect) for options. \u2502\n\u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f\n\u256d\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 <functools._lru_cache_wrapper object at 0x105f21850> \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n\u2502 def Klaas(): \u2502\n\u2502 |
| 12 | run_command | {
"command": "git diff"
} | {"status": "ok", "stdout": "diff --git a/rich/_inspect.py b/rich/_inspect.py\nindex 27d65cec..e4a2ff92 100644\n--- a/rich/_inspect.py\n+++ b/rich/_inspect.py\n@full{"status": "ok", "stdout": "diff --git a/rich/_inspect.py b/rich/_inspect.py\nindex 27d65cec..e4a2ff92 100644\n--- a/rich/_inspect.py\n+++ b/rich/_inspect.py\n@@ -101,6 +101,10 @@ class Inspect(JupyterMixin):\n signature_text = self.highlighter(_signature)\n \n qualname = name or getattr(obj, \"__qualname__\", name)\n+ if not isinstance(qualname, str):\n+ qualname = getattr(obj, \"__name__\", name)\n+ if not isinstance(qualname, str):\n+ qualname = str(qualname)\n \n # If obj is a module, there may be classes (which are callable) to display\n if inspect.isclass(obj):\n@@ -249,10 +253,15 @@ def get_object_types_mro_as_strings(obj: object) -> Collection[str]:\n Examples:\n `object_types_mro_as_strings(JSONDecoder)` will return `['json.decoder.JSONDecoder', 'builtins.object']`\n \"\"\"\n- return [\n- f'{getattr(type_, \"__module__\", \"\")}.{getattr(type_, \"__qualname__\", \"\")}'\n- for type_ in get_object_types_mro(obj)\n- ]\n+ mro_strings = []\n+ for type_ in get_object_types_mro(obj):\n+ qualname = getattr(type_, \"__qualname__\", \"\")\n+ if not isinstance(qualname, str):\n+ qualname = getattr(type_, \"__name__\", \"\")\n+ if not isinstance(qualname, str):\n+ qualname = str(qualname)\n+ mro_strings.append(f'{getattr(type_, \"__module__\", \"\")}.{qualname}')\n+ return mro_strings\n \n \n def is_object_one_of_types(\n", "stderr": "", "exit_code": 0} |
| 13 | run_command | {
"command": "python3 -m pytest tests/test_inspect.py"
} | {"status": "ok", "stdout": "============================= test session starts ==============================\nplatform darwin -- Python 3.13.15, pytest-8.3.4, pfull{"status": "ok", "stdout": "============================= test session starts ==============================\nplatform darwin -- Python 3.13.15, pytest-8.3.4, pluggy-1.6.0\nrootdir: /private/workspace/tests\nconfigfile: pytest.ini\ncollected 45 items\n\ntests/test_inspect.py ...s....sss.................................. [100%]\n\n======================== 41 passed, 4 skipped in 0.13s =========================\n", "stderr": "", "exit_code": 0} |
| 14 | submit_patch | {} | {"status": "ok", "patch_size": 1430, "files_changed": 1}full{"status": "ok", "patch_size": 1430, "files_changed": 1} |
diff --git a/rich/_inspect.py b/rich/_inspect.py
index 27d65cec..e4a2ff92 100644
--- a/rich/_inspect.py
+++ b/rich/_inspect.py
@@ -101,6 +101,10 @@ class Inspect(JupyterMixin):
signature_text = self.highlighter(_signature)
qualname = name or getattr(obj, "__qualname__", name)
+ if not isinstance(qualname, str):
+ qualname = getattr(obj, "__name__", name)
+ if not isinstance(qualname, str):
+ qualname = str(qualname)
# If obj is a module, there may be classes (which are callable) to display
if inspect.isclass(obj):
@@ -249,10 +253,15 @@ def get_object_types_mro_as_strings(obj: object) -> Collection[str]:
Examples:
`object_types_mro_as_strings(JSONDecoder)` will return `['json.decoder.JSONDecoder', 'builtins.object']`
"""
- return [
- f'{getattr(type_, "__module__", "")}.{getattr(type_, "__qualname__", "")}'
- for type_ in get_object_types_mro(obj)
- ]
+ mro_strings = []
+ for type_ in get_object_types_mro(obj):
+ qualname = getattr(type_, "__qualname__", "")
+ if not isinstance(qualname, str):
+ qualname = getattr(type_, "__name__", "")
+ if not isinstance(qualname, str):
+ qualname = str(qualname)
+ mro_strings.append(f'{getattr(type_, "__module__", "")}.{qualname}')
+ return mro_strings
def is_object_one_of_types(
...s....sss................................... [100%] 42 passed, 4 skipped in 0.13s