@@ -404,29 +404,32 @@ def get_all_type_hints(autodoc_mock_imports: list[str], obj: Any, name: str) ->
404
404
_TYPE_GUARD_IMPORTS_RESOLVED_GLOBALS_ID = set ()
405
405
406
406
407
- def _resolve_type_guarded_imports (autodoc_mock_imports : list [str ], obj : Any ) -> None :
408
- if (hasattr (obj , "__module__" ) and obj .__module__ not in _TYPE_GUARD_IMPORTS_RESOLVED ) or (
409
- hasattr (obj , "__globals__" ) and id (obj .__globals__ ) not in _TYPE_GUARD_IMPORTS_RESOLVED_GLOBALS_ID
410
- ):
411
- _TYPE_GUARD_IMPORTS_RESOLVED .add (obj .__module__ )
412
- if obj .__module__ not in sys .builtin_module_names :
413
- if hasattr (obj , "__globals__" ):
414
- _TYPE_GUARD_IMPORTS_RESOLVED_GLOBALS_ID .add (id (obj .__globals__ ))
415
-
416
- module = inspect .getmodule (obj )
417
- if module :
418
- try :
419
- module_code = inspect .getsource (module )
420
- except (TypeError , OSError ):
421
- ... # no source code => no type guards
422
- else :
423
- for _ , part in _TYPE_GUARD_IMPORT_RE .findall (module_code ):
424
- guarded_code = textwrap .dedent (part )
425
- try :
426
- with mock (autodoc_mock_imports ):
427
- exec (guarded_code , obj .__globals__ ) # noqa: S102
428
- except Exception as exc : # noqa: BLE001
429
- _LOGGER .warning (f"Failed guarded type import with { exc !r} " )
407
+ def _resolve_type_guarded_imports (autodoc_mock_imports : list [str ], obj : Any ) -> None : # noqa: C901
408
+ if hasattr (obj , "__module__" ) and obj .__module__ in _TYPE_GUARD_IMPORTS_RESOLVED :
409
+ return # already processed module
410
+ if not hasattr (obj , "__globals__" ): # classes with __slots__ do not have this
411
+ return # if lacks globals nothing we can do
412
+ if id (obj .__globals__ ) in _TYPE_GUARD_IMPORTS_RESOLVED_GLOBALS_ID :
413
+ return # already processed object
414
+ _TYPE_GUARD_IMPORTS_RESOLVED .add (obj .__module__ )
415
+ if obj .__module__ not in sys .builtin_module_names :
416
+ if hasattr (obj , "__globals__" ):
417
+ _TYPE_GUARD_IMPORTS_RESOLVED_GLOBALS_ID .add (id (obj .__globals__ ))
418
+
419
+ module = inspect .getmodule (obj )
420
+ if module :
421
+ try :
422
+ module_code = inspect .getsource (module )
423
+ except (TypeError , OSError ):
424
+ ... # no source code => no type guards
425
+ else :
426
+ for _ , part in _TYPE_GUARD_IMPORT_RE .findall (module_code ):
427
+ guarded_code = textwrap .dedent (part )
428
+ try :
429
+ with mock (autodoc_mock_imports ):
430
+ exec (guarded_code , obj .__globals__ ) # noqa: S102
431
+ except Exception as exc : # noqa: BLE001
432
+ _LOGGER .warning (f"Failed guarded type import with { exc !r} " )
430
433
431
434
432
435
def _get_type_hint (autodoc_mock_imports : list [str ], name : str , obj : Any ) -> dict [str , Any ]:
0 commit comments