2626from sphinx .util .typing import restify , stringify_annotation
2727
2828if TYPE_CHECKING :
29- from collections .abc import Iterator
29+ from collections .abc import Callable , Iterator , Sequence
3030 from types import ModuleType
31- from typing import Any , ClassVar , Final , Literal
31+ from typing import Any , ClassVar , Final , Literal , NoReturn
3232
3333 from sphinx .config import Config
3434 from sphinx .environment import BuildEnvironment , _CurrentDocument
4343 _TypeStatementProperties ,
4444 )
4545 from sphinx .ext .autodoc .directive import DocumenterBridge
46- from sphinx .registry import SphinxComponentRegistry
4746 from sphinx .util .typing import OptionSpec , _RestifyMode
4847
4948logger = logging .getLogger ('sphinx.ext.autodoc' )
@@ -86,10 +85,6 @@ class Documenter:
8685 'noindex' : bool_option ,
8786 }
8887
89- def get_attr (self , obj : Any , name : str , * defargs : Any ) -> Any :
90- """getattr() override for types such as Zope interfaces."""
91- return autodoc_attrgetter (obj , name , * defargs , registry = self .env ._registry )
92-
9388 def __init__ (
9489 self , directive : DocumenterBridge , name : str , indent : str = ''
9590 ) -> None :
@@ -99,6 +94,7 @@ def __init__(
9994 self ._current_document : _CurrentDocument = directive .env .current_document
10095 self ._events : EventManager = directive .env .events
10196 self .options : _AutoDocumenterOptions = directive .genopt
97+ self .get_attr = directive .get_attr
10298 self .name = name
10399 self .indent : Final = indent
104100 self .module : ModuleType | None = None
@@ -618,12 +614,32 @@ class TypeAliasDocumenter(Documenter):
618614 }
619615
620616
621- def autodoc_attrgetter (
622- obj : Any , name : str , * defargs : Any , registry : SphinxComponentRegistry
623- ) -> Any :
624- """Alternative getattr() for types"""
625- for typ , func in registry .autodoc_attrgetters .items ():
626- if isinstance (obj , typ ):
627- return func (obj , name , * defargs )
617+ class _AutodocAttrGetter :
618+ """getattr() override for types such as Zope interfaces."""
619+
620+ _attr_getters : Sequence [tuple [type , Callable [[Any , str , Any ], Any ]]]
621+
622+ __slots__ = ('_attr_getters' ,)
623+
624+ def __init__ (
625+ self , attr_getters : dict [type , Callable [[Any , str , Any ], Any ]], /
626+ ) -> None :
627+ super ().__setattr__ ('_attr_getters' , tuple (attr_getters .items ()))
628+
629+ def __call__ (self , obj : Any , name : str , * defargs : Any ) -> Any :
630+ for typ , func in self ._attr_getters :
631+ if isinstance (obj , typ ):
632+ return func (obj , name , * defargs )
633+
634+ return safe_getattr (obj , name , * defargs )
635+
636+ def __repr__ (self ) -> str :
637+ return f'_AutodocAttrGetter({ dict (self ._attr_getters )!r} )'
638+
639+ def __setattr__ (self , key : str , value : Any ) -> NoReturn :
640+ msg = f'{ self .__class__ .__name__ } is immutable'
641+ raise AttributeError (msg )
628642
629- return safe_getattr (obj , name , * defargs )
643+ def __delattr__ (self , key : str ) -> NoReturn :
644+ msg = f'{ self .__class__ .__name__ } is immutable'
645+ raise AttributeError (msg )
0 commit comments