Skip to content

Commit

Permalink
fix: Update imports on handle_value
Browse files Browse the repository at this point in the history
  • Loading branch information
sizmailov committed Nov 25, 2023
1 parent 8941fde commit 37feafd
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pybind11_stubgen/parser/mixins/fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 37feafd

Please sign in to comment.