diff --git a/pybind11_stubgen/parser/mixins/fix.py b/pybind11_stubgen/parser/mixins/fix.py index 2a3a17b..4d7756f 100644 --- a/pybind11_stubgen/parser/mixins/fix.py +++ b/pybind11_stubgen/parser/mixins/fix.py @@ -144,6 +144,11 @@ def handle_value(self, value: Any) -> Value: result = super().handle_value(value) if inspect.isroutine(value) and result.is_print_safe: self._add_import(QualifiedName.from_str(result.repr)) + else: + type_ = type(value) + self._add_import( + QualifiedName.from_str(f"{type_.__module__}.{type_.__qualname__}") + ) return result def parse_annotation_str( @@ -159,7 +164,9 @@ def _add_import(self, name: QualifiedName) -> None: return if len(name) == 1 and len(name[0]) == 0: return - if hasattr(builtins, name[0]): + if len(name) == 1 and hasattr(builtins, name[0]): + return + if len(name) > 0 and name[0] == "builtins": return if self.__current_class is not None and hasattr(self.__current_class, name[0]): return @@ -171,6 +178,8 @@ def _add_import(self, name: QualifiedName) -> None: if module_name is None: self.report_error(NameResolutionError(name)) return + if self.__current_module.__name__ == str(module_name): + return self.__extra_imports.add(Import(name=None, origin=module_name)) def _get_parent_module(self, name: QualifiedName) -> QualifiedName | None: @@ -913,8 +922,6 @@ def parse_annotation_str( except ValueError: pass else: - # call `handle_type` to trigger implicit import - self.handle_type(FixedSize) return self.handle_value(FixedSize(*dimensions)) return result