Skip to content

Commit 7038ace

Browse files
committed
Introspection: Use 'get_annotations' rather than 'typing.get_type_hints'.
(python/typing_extensions#597)
1 parent 0f34e30 commit 7038ace

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

sources/dynadoc/introspection.py

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ def is_attribute_visible(
4848
) -> bool:
4949
return (
5050
bool( description )
51-
or not name.startswith( '_' )
52-
or ( name.startswith( '__' )
53-
and name.endswith( '__' )
54-
and len( name ) > 4 ) ) # noqa: PLR2004
51+
or ( name.startswith( '__' ) and name.endswith( '__' ) )
52+
or not name.startswith( '_' ) )
5553

5654

5755
def reduce_annotation(
@@ -81,7 +79,11 @@ def _introspect_class(
8179
context: _interfaces.Context,
8280
cache: _interfaces.AnnotationsCache,
8381
) -> __.cabc.Sequence[ _interfaces.InformationBase ]:
84-
annotations = _access_annotations( possessor, context )
82+
annotations: dict[ str, __.typx.Any ] = { }
83+
# Descendant annotations override ancestor annotations.
84+
for class_ in reversed( possessor.__mro__ ):
85+
annotations_b = _access_annotations( class_, context )
86+
annotations.update( annotations_b )
8587
if not annotations: return ( )
8688
informations: list[ _interfaces.InformationBase ] = [ ]
8789
for name, annotation in annotations.items( ):
@@ -134,16 +136,7 @@ def _introspect_module(
134136
context: _interfaces.Context,
135137
cache: _interfaces.AnnotationsCache,
136138
) -> __.cabc.Sequence[ _interfaces.InformationBase ]:
137-
nomargs: _nomina.Variables = dict( eval_str = True )
138-
nomargs[ 'globals' ] = context.resolver_globals
139-
nomargs[ 'locals' ] = context.resolver_locals
140-
try:
141-
annotations = __.types.MappingProxyType(
142-
__.inspect.get_annotations( possessor, **nomargs ) )
143-
except TypeError as exc:
144-
emessage = f"Cannot access annotations for {possessor!r}: {exc}"
145-
context.notifier( 'error', emessage )
146-
annotations = ( )
139+
annotations = _access_annotations( possessor, context )
147140
if not annotations: return ( )
148141
informations: list[ _interfaces.InformationBase ] = [ ]
149142
for name, annotation in annotations.items( ):
@@ -211,27 +204,18 @@ def _introspect_function_valences(
211204

212205

213206
def _access_annotations(
214-
possessor: _nomina.Decoratable, context: _interfaces.Context
207+
possessor: _nomina.Documentable, context: _interfaces.Context
215208
) -> __.cabc.Mapping[ str, __.typx.Any ]:
216-
nomargs: _nomina.Variables = dict( include_extras = True )
217-
nomargs[ 'globalns' ] = context.resolver_globals
218-
nomargs[ 'localns' ] = context.resolver_locals
209+
nomargs: _nomina.Variables = dict( eval_str = True )
210+
nomargs[ 'globals' ] = context.resolver_globals
211+
nomargs[ 'locals' ] = context.resolver_locals
219212
try:
220213
return __.types.MappingProxyType(
221-
__.typx.get_type_hints( possessor, **nomargs ) )
222-
except TypeError:
223-
# Fallback to lower-level resolution which sometimes works better.
224-
# Note: No inheritance merges for classes.
225-
nomargs = dict( eval_str = True )
226-
nomargs[ 'globals' ] = context.resolver_globals
227-
nomargs[ 'locals' ] = context.resolver_locals
228-
try:
229-
return __.types.MappingProxyType(
230-
__.inspect.get_annotations( possessor, **nomargs ) )
231-
except TypeError as exc:
232-
emessage = f"Cannot access annotations for {possessor!r}: {exc}"
233-
context.notifier( 'error', emessage )
234-
return __.dictproxy_empty
214+
__.inspect.get_annotations( possessor, **nomargs ) )
215+
except TypeError as exc:
216+
emessage = f"Cannot access annotations for {possessor!r}: {exc}"
217+
context.notifier( 'error', emessage )
218+
return __.dictproxy_empty
235219

236220

237221
def _classes_sequence_to_union(

0 commit comments

Comments
 (0)