88from pathlib import Path
99from typing import TYPE_CHECKING
1010
11+ from docutils .nodes import Text
12+
1113
1214if TYPE_CHECKING :
1315 from docutils .nodes import TextElement , reference
8284 source_directory = "docs/" ,
8385)
8486
87+ _np_aliases = {"bool_" : "bool" }
8588_np_nocls = {"float64" : "attr" }
8689_optional_types = {
8790 "CupyArray" : "cupy.ndarray" ,
9497}
9598
9699
97- def find_type_alias (name : str ) -> tuple [str , str ] | tuple [None , None ]:
100+ def find_type_alias (name : str ) -> tuple [str , str , str | None ] | tuple [None , None , None ]:
98101 """Find a type alias."""
99102 import numpy .typing as npt
100103
101104 from fast_array_utils import types , typing
102105
103106 if name in typing .__all__ :
104- return "data" , f"fast_array_utils.typing.{ name } "
107+ return "data" , f"fast_array_utils.typing.{ name } " , None
105108 if name .startswith ("types." ) and name [6 :] in {* types .__all__ , * _optional_types }:
106109 if path := _optional_types .get (name [6 :]):
107- return "class" , path
108- return "data" , f"fast_array_utils.{ name } "
110+ return "class" , path , None
111+ return "data" , f"fast_array_utils.{ name } " , None
109112 if name .startswith ("np." ):
110- return _np_nocls .get (name [3 :], "class" ), f"numpy.{ name [3 :]} "
113+ name = _np_aliases .get (name [3 :], name [3 :])
114+ return _np_nocls .get (name , "class" ), f"numpy.{ name } " , f"np.{ name } "
111115 if name in npt .__all__ :
112- return "data" , f"numpy.typing.{ name } "
113- return None , None
116+ return "data" , f"numpy.typing.{ name } " , None
117+ return None , None , None
114118
115119
116120def resolve_type_aliases (app : Sphinx , env : BuildEnvironment , node : pending_xref , contnode : TextElement ) -> reference | None :
117121 """Resolve :class: references to our type aliases as :attr: instead."""
118122 if (node ["refdomain" ], node ["reftype" ]) != ("py" , "class" ):
119123 return None
120- typ , target = find_type_alias (node ["reftarget" ])
124+ typ , target , name = find_type_alias (node ["reftarget" ])
121125 if typ is None or target is None :
122126 return None
123127 if target .startswith ("fast_array_utils." ):
@@ -131,6 +135,8 @@ def resolve_type_aliases(app: Sphinx, env: BuildEnvironment, node: pending_xref,
131135 if ref is None :
132136 msg = f"Could not resolve { typ } { target } (from { node ['reftarget' ]} )"
133137 raise AssertionError (msg )
138+ if name :
139+ ref .children [:] = [Text (name )]
134140 return ref
135141
136142
0 commit comments