Skip to content

Commit

Permalink
Fix display of union "short" type annotations in Sphinx
Browse files Browse the repository at this point in the history
This applies to `a | b` annotations in Python before 3.10. See jbms/sphinx-immaterial#161
  • Loading branch information
mhostetter committed Sep 14, 2022
1 parent bee9b75 commit bc72d18
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,26 @@ def classproperty(obj):
setattr(galois._fields._meta.FieldArrayMeta, p, FieldArrayMeta_property)


def autodoc_process_signature(app, what, name, obj, options, signature, return_annotation):
signature = modify_type_hints(signature)
return_annotation = modify_type_hints(return_annotation)
return signature, return_annotation


def modify_type_hints(signature):
"""
Fix shortening numpy type annotations in string annotations created with
`from __future__ import annotations` that Sphinx can't process before Python
3.10.
See https://github.com/jbms/sphinx-immaterial/issues/161
"""
if signature:
signature = signature.replace("np", "~numpy")
return signature


def setup(app):
app.connect("autodoc-skip-member", autodoc_skip_member)
app.connect("autodoc-process-bases", autodoc_process_bases)
app.connect("autodoc-process-signature", autodoc_process_signature)

0 comments on commit bc72d18

Please sign in to comment.