@@ -169,6 +169,18 @@ def format_internal_tuple(t: tuple[Any, ...], config: Config) -> str:
169169 return f"({ ', ' .join (fmt )} )"
170170
171171
172+ def fixup_module_name (config : Config , module : str ) -> str :
173+ if getattr (config , "typehints_fixup_module_name" , None ):
174+ module = config .typehints_fixup_module_name (module )
175+
176+ if module == "typing_extensions" :
177+ module = "typing"
178+
179+ if module == "_io" :
180+ module = "io"
181+ return module
182+
183+
172184def format_annotation (annotation : Any , config : Config ) -> str : # noqa: C901, PLR0911, PLR0912, PLR0915, PLR0914
173185 """
174186 Format the annotation.
@@ -204,13 +216,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
204216 except ValueError :
205217 return str (annotation ).strip ("'" )
206218
207- # Redirect all typing_extensions types to the stdlib typing module
208- if module == "typing_extensions" :
209- module = "typing"
210-
211- if module == "_io" :
212- module = "io"
213-
219+ module = fixup_module_name (config , module )
214220 full_name = f"{ module } .{ class_name } " if module != "builtins" else class_name
215221 fully_qualified : bool = getattr (config , "typehints_fully_qualified" , False )
216222 prefix = "" if fully_qualified or full_name == class_name else "~"
@@ -967,6 +973,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
967973 app .add_config_value ("typehints_formatter" , None , "env" )
968974 app .add_config_value ("typehints_use_signature" , False , "env" ) # noqa: FBT003
969975 app .add_config_value ("typehints_use_signature_return" , False , "env" ) # noqa: FBT003
976+ app .add_config_value ("typehints_fixup_module_name" , None , "env" )
970977 app .add_role ("sphinx_autodoc_typehints_type" , sphinx_autodoc_typehints_type_role )
971978 app .connect ("env-before-read-docs" , validate_config ) # config may be changed after “config-inited” event
972979 app .connect ("autodoc-process-signature" , process_signature )
0 commit comments