Skip to content
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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this also be other types of exceptions, e.g. RuntimeError

https://chatgpt.com/share/67fc0fc7-dcb0-8010-adc6-3af254dd624e

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