From 74329d9c51c16be54e1f3dece07461f67cfb8ab0 Mon Sep 17 00:00:00 2001 From: James Braza Date: Wed, 30 Aug 2023 11:49:27 -0700 Subject: [PATCH] Fail better in ``ExceptionDocumenter.can_document_member`` (#11660) Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com> --- CHANGES | 4 ++++ sphinx/ext/autodoc/__init__.py | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 64aa161dfe2..7d155ba8158 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,10 @@ Bugs fixed for sibling files in a subdirectory. Patch by Albert Shih. * #11659: Allow ``?config=...`` in :confval:`mathjax_path`. +* #11654: autodoc: Fail with a more descriptive error message + when an object claims to be an instance of ``type``, + but is not a class. + Patch by James Braza. Testing ------- diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index ac1b5cec6f6..8d68f72bcbd 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -1913,7 +1913,17 @@ class ExceptionDocumenter(ClassDocumenter): @classmethod def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any, ) -> bool: - return isinstance(member, type) and issubclass(member, BaseException) + try: + return isinstance(member, type) and issubclass(member, BaseException) + except TypeError as exc: + # It's possible for a member to be considered a type, but fail + # issubclass checks due to not being a class. For example: + # https://github.com/sphinx-doc/sphinx/issues/11654#issuecomment-1696790436 + msg = ( + f'{cls.__name__} failed to discern if member {member} with' + f' membername {membername} is a BaseException subclass.' + ) + raise ValueError(msg) from exc class DataDocumenterMixinBase: