Skip to content

Commit 7cd52d5

Browse files
authored
Allow overriding exceptions (#16)
1 parent 875b441 commit 7cd52d5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

scanpydoc/elegant_typehints/formatting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def _format_full(annotation: Type[Any], fully_qualified: bool = False):
3838
if inspect.isclass(annotation) or inspect.isclass(origin):
3939
full_name = f"{annotation.__module__}.{annotation.__qualname__}"
4040
override = elegant_typehints.qualname_overrides.get(full_name)
41+
role = "exception" if issubclass(annotation_cls, BaseException) else "class"
4142
if override is not None:
42-
return f":py:class:`{tilde}{override}`"
43+
return f":py:{role}:`{tilde}{override}`"
4344

4445
return _format_orig(annotation, fully_qualified)
4546

tests/test_elegant_typehints.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
TestCls = type("Class", (), {})
2323
TestCls.__module__ = "_testmod"
24+
TestExc = type("Excep", (RuntimeError,), {})
25+
TestExc.__module__ = "_testmod"
2426

2527

2628
@pytest.fixture
@@ -31,7 +33,10 @@ def app(make_app_setup) -> Sphinx:
3133
"sphinx_autodoc_typehints",
3234
"scanpydoc.elegant_typehints",
3335
],
34-
qualname_overrides={"_testmod.Class": "test.Class"},
36+
qualname_overrides={
37+
"_testmod.Class": "test.Class",
38+
"_testmod.Excep": "test.Excep",
39+
},
3540
)
3641

3742

@@ -137,11 +142,16 @@ def test_literal(app):
137142
)
138143

139144

140-
def test_qualname_overrides(app):
145+
def test_qualname_overrides_class(app):
141146
assert TestCls.__module__ == "_testmod"
142147
assert _format_terse(TestCls) == ":py:class:`~test.Class`"
143148

144149

150+
def test_qualname_overrides_exception(app):
151+
assert TestExc.__module__ == "_testmod"
152+
assert _format_terse(TestExc) == ":py:exception:`~test.Excep`"
153+
154+
145155
def test_qualname_overrides_recursive(app):
146156
assert _format_terse(t.Union[TestCls, str]) == (
147157
r":py:class:`~test.Class`, :py:class:`str`"

0 commit comments

Comments
 (0)