From a346c300b88d00ae9fa41dd459d2decb551ff9f1 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Sat, 4 Nov 2023 15:54:32 -0400 Subject: [PATCH] feat: exclude inherited by default --- README.md | 24 ++++++++++++++++++++++-- src/griffe_fieldz/_extension.py | 11 ++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c2fcb25..e6fe3eb 100644 --- a/README.md +++ b/README.md @@ -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/) @@ -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} +``` diff --git a/src/griffe_fieldz/_extension.py b/src/griffe_fieldz/_extension.py index 400570b..ecbfd06 100644 --- a/src/griffe_fieldz/_extension.py +++ b/src/griffe_fieldz/_extension.py @@ -2,6 +2,7 @@ from __future__ import annotations import inspect +from operator import ge from typing import TYPE_CHECKING, Any, Iterable, Sequence import fieldz @@ -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): @@ -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 @@ -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] = []