Skip to content

Commit cfe1bda

Browse files
committed
Fix autodoc_type_aliases when they appear in PEP 604 union syntax (Alias | Type)
Fix #14004
1 parent e347e59 commit cfe1bda

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Contributors
109109
* Szymon Karpinski -- intersphinx improvements
110110
* \T. Powers -- HTML output improvements
111111
* Taku Shimizu -- epub3 builder
112+
* Tamika Nomara -- bug fixes
112113
* Thomas Lamb -- linkcheck builder
113114
* Thomas Waldmann -- apidoc module fixes
114115
* Till Hoffmann -- doctest option to exit after first failed test

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ Bugs fixed
134134
Patch by Jeremy Maitin-Shepard.
135135
* #13939: LaTeX: page break can separate admonition title from contents.
136136
Patch by Jean-François B.
137+
* #14004: Fix `autodoc_type_aliases` when they appear in PEP 604 union syntax (`Alias | Type`).
138+
Patch by Tamika Nomara.
137139

138140

139141
Testing

sphinx/util/inspect.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,14 @@ def __hash__(self) -> int:
616616
def __repr__(self) -> str:
617617
return f'{self.__class__.__name__}({self.name!r})'
618618

619+
def __or__(self, value: Any):
620+
# When evaluating type hints, our forward ref can appear in type expressions,
621+
# i.e. `Alias | None`. It should, therefore, support `__or__` and `__ror__`.
622+
return typing.Union[self, value]
623+
624+
def __ror__(self, value: Any):
625+
return typing.Union[value, self]
626+
619627

620628
class TypeAliasModule:
621629
"""Pseudo module class for :confval:`autodoc_type_aliases`."""

tests/test_util/test_util_inspect.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ def test_TypeAliasForwardRef():
115115
sig_str = stringify_annotation(alias, 'fully-qualified-except-typing')
116116
assert sig_str == "TypeAliasForwardRef('example') | None"
117117

118+
alias = alias | None
119+
sig_str = stringify_annotation(alias, 'fully-qualified-except-typing')
120+
assert sig_str == "TypeAliasForwardRef('example') | None"
121+
122+
alias = None | alias
123+
sig_str = stringify_annotation(alias, 'fully-qualified-except-typing')
124+
assert sig_str == "None | TypeAliasForwardRef('example')"
125+
118126

119127
def test_TypeAliasNamespace() -> None:
120128
import logging.config

0 commit comments

Comments
 (0)