Handle unusual __qualname__ in inspect

This commit is contained in:
Sebastian Speitel 2025-11-23 14:38:47 +01:00
parent 4d6d631a3d
commit 0c271d5bb6
No known key found for this signature in database
GPG Key ID: 31DF96AC5619195D
4 changed files with 22 additions and 0 deletions

View File

@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Python3.14 compatibility https://github.com/Textualize/rich/pull/3861
### Fixed
- Fixed exception when callling `inspect` on objects with unusual `__qualname__` attribute https://github.com/Textualize/rich/pull/3894
## [14.1.0] - 2025-06-25
### Changed

View File

@ -94,3 +94,4 @@ The following people have contributed to the development of Rich:
- [Jonathan Helmus](https://github.com/jjhelmus)
- [Brandon Capener](https://github.com/bcapener)
- [Alex Zheng](https://github.com/alexzheng111)
- [Sebastian Speitel](https://github.com/SebastianSpeitel)

View File

@ -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 = name
# If obj is a module, there may be classes (which are callable) to display
if inspect.isclass(obj):

View File

@ -404,6 +404,19 @@ def test_inspect_module_with_class():
assert render(module, methods=True) == expected
def test_qualname_in_slots():
from functools import lru_cache
@lru_cache
class Klass:
__slots__ = ("__qualname__",)
try:
inspect(Klass)
except Exception as e:
assert False, f"Class with __qualname__ in __slots__ shouldn't raise {e}"
@pytest.mark.parametrize(
"special_character,expected_replacement",
(