Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exception raised by inspect #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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