Skip to content

Commit a33f280

Browse files
authored
Fix origin having the __qualname__ (#66)
1 parent 687428e commit a33f280

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
python-version: ['3.7', '3.8', '3.9']
16+
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
1717
steps:
1818
- uses: actions/checkout@v2
1919
- uses: actions/setup-python@v2

scanpydoc/elegant_typehints/formatting.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def _format_full(annotation: Type[Any], config: Config) -> Optional[str]:
3434

3535
# Only if this is a real class we override sphinx_autodoc_typehints
3636
if inspect.isclass(annotation) or inspect.isclass(origin):
37-
full_name = f"{annotation.__module__}.{annotation.__qualname__}"
37+
try:
38+
full_name = f"{annotation.__module__}.{annotation.__qualname__}"
39+
except AttributeError:
40+
full_name = f"{origin.__module__}.{origin.__qualname__}"
3841
override = elegant_typehints.qualname_overrides.get(full_name)
3942
role = "exc" if issubclass(annotation_cls, BaseException) else "class"
4043
if override is not None:

tests/test_elegant_typehints.py

+7
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ def test_literal(app):
150150
assert _format_full(Literal["str", 1, None], app.config) is None
151151

152152

153+
@pytest.mark.skipif(sys.version_info < (3, 10), reason="Syntax only available on 3.10+")
154+
def test_syntax_vs_typing(app):
155+
u = eval("int | str")
156+
assert _format_terse(u, app.config) == ":py:class:`int` | :py:class:`str`"
157+
assert _format_full(u, app.config) is None # this used to crash
158+
159+
153160
def test_qualname_overrides_class(app, _testmod):
154161
assert _testmod.Class.__module__ == "_testmod"
155162
assert _format_terse(_testmod.Class, app.config) == ":py:class:`~test.Class`"

0 commit comments

Comments
 (0)