Skip to content

Commit

Permalink
feat: exclude inherited by default
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Nov 4, 2023
1 parent 8325765 commit a346c30
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
[![CI](https://github.com/tlambert03/griffe-fieldz/actions/workflows/ci.yml/badge.svg)](https://github.com/tlambert03/griffe-fieldz/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/tlambert03/griffe-fieldz/branch/main/graph/badge.svg)](https://codecov.io/gh/tlambert03/griffe-fieldz)

Griffe extension adding support for data-class like things (pydantic, attrs, etc...). This extension will inject the fields of the data-class into the documentation, preventing you from duplicating field metadata in your docstrings.
Griffe extension adding support for data-class like things (pydantic, attrs,
etc...). This extension will inject the fields of the data-class into the
documentation, preventing you from duplicating field metadata in your
docstrings.

It supports anything that [fieldz](https://github.com/pyapp-kit/fieldz) supports, which is currently:
It supports anything that [fieldz](https://github.com/pyapp-kit/fieldz)
supports, which is currently:

- [`dataclasses.dataclass`](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass)
- [`pydantic.BaseModel`](https://docs.pydantic.dev/latest/)
Expand All @@ -35,3 +39,19 @@ plugins:
extensions:
- griffe_fieldz
```
You may use any of the following options, provided as a dictionary under the
`griffe_fieldz` key.

| Option | Description | Default |
|---------------------|--------------------------------------------------|---------|
| `include_inherited` | Include inherited fields in class parameters. | `False` |
| `include_private` | Include private fields in the documentation. | `False` |

For example:

```yml
options:
extensions:
- griffe_fieldz: {include_inherited: true}
```
11 changes: 10 additions & 1 deletion src/griffe_fieldz/_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import inspect
from operator import ge
from typing import TYPE_CHECKING, Any, Iterable, Sequence

import fieldz
Expand Down Expand Up @@ -33,11 +34,13 @@ def __init__(
self,
object_paths: list[str] | None = None,
include_private: bool = False,
include_inherited: bool = False,
**kwargs: Any,
) -> None:
self.object_paths = object_paths
self._kwargs = kwargs
self.include_private = include_private
self.include_inherited = include_inherited

def on_class_instance(self, *, node: ast.AST | ObjectNode, cls: Class) -> None:
if isinstance(node, ObjectNode):
Expand Down Expand Up @@ -68,6 +71,10 @@ def _inject_fields(self, obj: Object, runtime_obj: type[BaseModel]) -> None:

# collect field info
fields = fieldz.fields(runtime_obj)
if not self.include_inherited:
annotations = getattr(runtime_obj, "__annotations__", {})
fields = tuple(f for f in fields if f.name in annotations)

params, attrs = _fields_to_params(fields, obj.docstring, self.include_private)

# merge/add field info to docstring
Expand Down Expand Up @@ -104,7 +111,9 @@ def _default_repr(field: fieldz.Field) -> str | None:


def _fields_to_params(
fields: Iterable[fieldz.Field], docstring: Docstring, include_private: bool = False
fields: Iterable[fieldz.Field],
docstring: Docstring,
include_private: bool = False,
) -> tuple[list[DocstringParameter], list[DocstringAttribute]]:
"""Get all docstring attributes and parameters for fields."""
params: list[DocstringParameter] = []
Expand Down

0 comments on commit a346c30

Please sign in to comment.