Adding field-show-examples
to show the examples for a field
#295
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I will follow the adding new features guide to summarise this PR.
1. Provide rationale
This PR follows the initial discussion in #186. It implements the examples field when available in a model.
It should be enough to close #186. This PR won't handle cases where there are examples present in the
json_schema_extra
dictionary.2. Specify the feature
The examples field is available in Pydantic and part of several projects (e.g. FastAPI, transformers). It provides additional context on the usage of the field. This is particularly important for complex models, but still useful in other cases.
3. Derive tests
The tests are implemented in
tests/test_configuration_fields.py
. Three tests were added:tests/test_configuration_fields.py::test_autodoc_pydantic_field_show_examples_true
tests/test_configuration_fields.py::test_autodoc_pydantic_field_show_examples_false
tests/test_configuration_fields.py::test_autodoc_pydantic_field_show_examples_ignore_extra
You can run them collectively with the other tests using
poetry run pytest
or individually by usingpoetry run pytest tests/test_configuration_fields.py::name_of_class
The
conftest.py
was also updated to include theFalse
default configuration and keep the behaviour ofCONF_DEACTIVATE
as expected.4. Add configuration settings
A new boolean configuration was added called
field_show_examples
. The configuration behaves like all others*show*
configurations accepting eitherTrue
orFalse
and it defaults toTrue
.5. Implement behaviour
The implemented behaviour was achieved with the support of
autodoc_pydantic/inspection.py::FieldInspector.has_examples
andautodoc_pydantic/inspection.py::FieldInspector.get_examples
. The string to add the examples is generated byautodoc_pydantic/directives/autodocumented.py::PydanticFieldDocumenter.add_examples
which is now ran inautodoc_pydantic/directives/autodocumented.py::PydanticFieldDocumenter.add_content
6. Update document
The documentation was updated to include the description in the
configuration.rst
and the default usage_automodule was also updated inusage.rst
to showcase a simple field example with examples.