Skip to content

Commit 0c271d5

Browse files
Handle unusual __qualname__ in inspect
1 parent 4d6d631 commit 0c271d5

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Python3.14 compatibility https://github.com/Textualize/rich/pull/3861
1313

14+
### Fixed
15+
16+
- Fixed exception when callling `inspect` on objects with unusual `__qualname__` attribute https://github.com/Textualize/rich/pull/3894
17+
1418
## [14.1.0] - 2025-06-25
1519

1620
### Changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,4 @@ The following people have contributed to the development of Rich:
9494
- [Jonathan Helmus](https://github.com/jjhelmus)
9595
- [Brandon Capener](https://github.com/bcapener)
9696
- [Alex Zheng](https://github.com/alexzheng111)
97+
- [Sebastian Speitel](https://github.com/SebastianSpeitel)

rich/_inspect.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def _get_signature(self, name: str, obj: Any) -> Optional[Text]:
101101
signature_text = self.highlighter(_signature)
102102

103103
qualname = name or getattr(obj, "__qualname__", name)
104+
if not isinstance(qualname, str):
105+
qualname = getattr(obj, "__name__", name)
106+
if not isinstance(qualname, str):
107+
qualname = name
104108

105109
# If obj is a module, there may be classes (which are callable) to display
106110
if inspect.isclass(obj):

tests/test_inspect.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ class Thing:
404404
assert render(module, methods=True) == expected
405405

406406

407+
def test_qualname_in_slots():
408+
from functools import lru_cache
409+
410+
@lru_cache
411+
class Klass:
412+
__slots__ = ("__qualname__",)
413+
414+
try:
415+
inspect(Klass)
416+
except Exception as e:
417+
assert False, f"Class with __qualname__ in __slots__ shouldn't raise {e}"
418+
419+
407420
@pytest.mark.parametrize(
408421
"special_character,expected_replacement",
409422
(

0 commit comments

Comments
 (0)