def _member_objects(self) -> dict[str, Any]: unsorted: dict[str, Any] = {} for cls in self.obj.__mro__: for name, obj in cls.__dict__.items(): unsorted.setdefault(name, obj) for name in self._var_annotations: unsorted.setdefault(name, empty) for name in self._var_docstrings: unsorted.setdefault(name, empty) # Do a dance similar to # https://github.com/python/cpython/blob/9feae41c4f04ca27fd2c865807a5caeb50bf4fc4/Lib/inspect.py#L2359-L2376 call = _safe_getattr(type(self.obj), "__call__", object.__call__) if not isinstance(call, NonUserDefinedCallables): # Does the metaclass define a custom __call__ method? unsorted["__init__"] = call if issubclass(self.obj, enum.Enum): # Special case: do not show a constructor for enums. They are typically not constructed by users. # The alternative would be showing __new__, as __call__ is too verbose. del unsorted["__init__"] else: # Does our class define a custom __new__ method? new = _safe_getattr(self.obj, "__new__", object.__new__) if not isinstance(new, NonUserDefinedCallables): # we only want to pick up __new__ if it has a non-default docstring. prefer_init_over_new = new.__doc__ in (None, object.__new__.__doc__) if not prefer_init_over_new: unsorted["__init__"] = new sorted: dict[str, Any] = {} for cls in self.obj.__mro__: sorted, unsorted = doc_ast.sort_by_source(cls, sorted, unsorted) sorted.update(unsorted) return sorted
def _member_objects(self) -> dict[str, Any]: unsorted: dict[str, Any] = {} for cls in self.obj.__mro__: for name, obj in cls.__dict__.items(): unsorted.setdefault(name, obj) for name in self._var_annotations: unsorted.setdefault(name, empty) for name in self._var_docstrings: unsorted.setdefault(name, empty) sorted: dict[str, Any] = {} for cls in self.obj.__mro__: sorted, unsorted = doc_ast.sort_by_source(cls, sorted, unsorted) sorted.update(unsorted) return sorted
def _member_objects(self) -> dict[str, Any]: members = {} all = _safe_getattr(self.obj, "__all__", False) if all: for name in all: if name in self.obj.__dict__: val = self.obj.__dict__[name] elif name in self._var_annotations: val = empty else: # this may be an unimported submodule, try importing. # (https://docs.python.org/3/tutorial/modules.html#importing-from-a-package) try: val = extract.load_module(f"{self.modulename}.{name}") except RuntimeError as e: warnings.warn( f"Found {name!r} in {self.modulename}.__all__, but it does not resolve: {e}", RuntimeWarning, ) val = empty members[name] = val else: for name, obj in self.obj.__dict__.items(): # We already exclude everything here that is imported, only a TypeVar, # or a variable without annotation and docstring. # If one needs to document one of these things, __all__ is the correct way. if isinstance(obj, TypeVar): continue obj_module = inspect.getmodule(obj) declared_in_this_module = self.obj.__name__ == _safe_getattr( obj_module, "__name__", None) if declared_in_this_module or name in self._documented_members: members[name] = obj for name in self._var_annotations: members.setdefault(name, empty) members, notfound = doc_ast.sort_by_source(self.obj, {}, members) members.update(notfound) return members
def _member_objects(self) -> dict[str, Any]: unsorted: dict[str, Any] = {} for cls in self.obj.__mro__: for name, obj in cls.__dict__.items(): unsorted.setdefault(name, obj) for name in self._var_annotations: unsorted.setdefault(name, empty) for name in self._var_docstrings: unsorted.setdefault(name, empty) is_namedtuple = ("_fields" in unsorted and issubclass(self.obj, tuple) and unsorted.get("__init__", None) == object.__init__) if is_namedtuple: # namedtuple have object.__init__ as their default constructor, # which renders wrong. We just remove it. del unsorted["__init__"] sorted: dict[str, Any] = {} for cls in self.obj.__mro__: sorted, unsorted = doc_ast.sort_by_source(cls, sorted, unsorted) sorted.update(unsorted) return sorted
members[name] = val else: for name, obj in self.obj.__dict__.items(): if isinstance(obj, TypeVar): continue obj_module = inspect.getmodule(obj) declared_in_this_module = self.obj.__name__ == _safe_getattr( obj_module, "__name__", None ) if declared_in_this_module or name in self._documented_members: members[name] = obj for name in self._var_annotations: members.setdefault(name, empty) members, notfound = doc_ast.sort_by_source(self.obj, {}, members) members.update(notfound) return members @cached_property def variables(self) -> list["Variable"]: """ A list of all documented module level variables. """ return [x for x in self.members.values() if isinstance(x, Variable)] @cached_property def classes(self) -> list["Class"]: """ A list of all documented module level classes.