Skip to content

Commit

Permalink
Handle exception raised by inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
lenard-mosys committed Oct 18, 2024
1 parent cf58aa6 commit 8e53a66
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
11 changes: 11 additions & 0 deletions pybind11_stubgen/parser/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ def __init__(self, name: QualifiedName):

def __str__(self):
return f"Can't find/import '{self.name}'"


class InspectError(ParserError):
def __init__(self, path: QualifiedName, class_: type, msg: str):
super().__init__()
self.path = path
self.class_ = class_
self.msg = msg

def __str__(self):
return f"Can't get members of type {self.class_} at {self.path}: {self.msg}"
46 changes: 26 additions & 20 deletions pybind11_stubgen/parser/mixins/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Any

from pybind11_stubgen.parser.errors import (
InspectError,
InvalidExpressionError,
InvalidIdentifierError,
NameResolutionError,
Expand Down Expand Up @@ -45,26 +46,31 @@ class ParserDispatchMixin(IParser):
def handle_class(self, path: QualifiedName, class_: type) -> Class | None:
base_classes = class_.__bases__
result = Class(name=path[-1], bases=self.handle_bases(path, base_classes))
for name, member in inspect.getmembers(class_):
obj = self.handle_class_member(
QualifiedName([*path, Identifier(name)]), class_, member
)
if isinstance(obj, Docstring):
result.doc = obj
elif isinstance(obj, Alias):
result.aliases.append(obj)
elif isinstance(obj, Field):
result.fields.append(obj)
elif isinstance(obj, Class):
result.classes.append(obj)
elif isinstance(obj, list): # list[Method]
result.methods.extend(obj)
elif isinstance(obj, Property):
result.properties.append(obj)
elif obj is None:
pass
else:
raise AssertionError()
try:
members = inspect.getmembers(class_)
except TypeError as e:
self.report_error(InspectError(path, class_, str(e)))
else:
for name, member in members:
obj = self.handle_class_member(
QualifiedName([*path, Identifier(name)]), class_, member
)
if isinstance(obj, Docstring):
result.doc = obj
elif isinstance(obj, Alias):
result.aliases.append(obj)
elif isinstance(obj, Field):
result.fields.append(obj)
elif isinstance(obj, Class):
result.classes.append(obj)
elif isinstance(obj, list): # list[Method]
result.methods.extend(obj)
elif isinstance(obj, Property):
result.properties.append(obj)
elif obj is None:
pass
else:
raise AssertionError()
return result

def handle_class_member(
Expand Down

0 comments on commit 8e53a66

Please sign in to comment.