mkdocstrings / pytkdocs

Load Python objects documentation.
https://mkdocstrings.github.io/pytkdocs
ISC License
50 stars 32 forks source link

[BUG] 'builtin_function_or_method' object is not subscriptable #144

Closed demberto closed 2 years ago

demberto commented 2 years ago

To Reproduce

class BaseIterator(abc.ABC):
    """Base iterator class for all `*Iterator` classes."""

    _first_fn: Callable[[wt.HANDLE, ctypes.pointer[SizedStructure]], wt.BOOL]
    """Retrieves the first element from an iterator, akin to `iter()`."""

    _next_fn: Callable[[wt.HANDLE, ctypes.pointer[SizedStructure]], wt.BOOL]
    """Retrives the next element from an iterator every time until the end."""

    _dt_type: Type[DataclassMixin]
    """The type of dataclass returned by this iterator."""

    def __init__(self, handle: wt.HANDLE, first_struct: SizedStructure) -> None:
        """Create an iterator wrapping `_first_fn` and `_next_fn`.

        Args:
            handle (wt.HANDLE): Handle to the snapshot.
            first_struct (SizedStructure): Handle to the first instance of the
                underlying structure.
        """
        self._first = True
        self._handle = handle
        self._initial = first_struct

    def _build_dataclass(self) -> _dt_type:
        d = self._dt_type.build(self._initial)
        self._initial.reset_size()
        return d

    def __next__(self) -> _dt_type:
        if self._first:
            if not self._first_fn(
                self._handle, ctypes.byref(self._initial)
            ):  # pragma: no cover
                raise RuntimeError
            self._first = False
            return self._build_dataclass()
        if not self._next_fn(
            self._handle, ctypes.byref(self._initial)
        ):  # pragma: no cover
            if ctypes.get_last_error() == ERROR_NO_MORE_FILES:
                raise StopIteration
            raise RuntimeError
        return self._build_dataclass()

    def __iter__(self) -> Self:
        return self

fails with:

mkdocstrings: 'builtin_function_or_method' object is not subscriptable
  Traceback (most recent call last):
    File "\pytkdocs\cli.py", line 205, in main
      output = json.dumps(process_json(line))
    File "\pytkdocs\cli.py", line 114, in process_json
      return process_config(json.loads(json_input))
    File "\pytkdocs\cli.py", line 91, in process_config
      obj = loader.get_object_documentation(path, members)
    File "\pytkdocs\loader.py", line 360, in get_object_documentation
      root_object = self.get_class_documentation(leaf, members)
    File "\pytkdocs\loader.py", line 483, in get_class_documentation
      merge(attributes_data, get_class_attributes(parent_class))
    File "\pytkdocs\parsers\attributes.py", line 115, in get_class_attributes
      type_hints = get_type_hints(cls)
    File "\Python310\lib\typing.py", line 1804, in get_type_hints
      value = _eval_type(value, base_globals, base_locals)
    File "\Python310\lib\typing.py", line 324, in _eval_type
      return t._evaluate(globalns, localns, recursive_guard)
    File "\Python310\lib\typing.py", line 688, in _evaluate
      eval(self.__forward_code__, globalns, localns),
    File "<string>", line 1, in <module>
  TypeError: 'builtin_function_or_method' object is not subscriptable

Expected behavior No errors

System (please complete the following information):

pawamoy commented 2 years ago

It seems that ctypes.pointer is simply not subscriptable: you can't use it like that.

demberto commented 2 years ago

Ahh yes, I changed ctypes.POINTER to ctypes.pointer trying to fix a mypy error, my bad. I think ctypes._PointerLike is what I should use there, my bad