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_extradictionary.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_truetests/test_configuration_fields.py::test_autodoc_pydantic_field_show_examples_falsetests/test_configuration_fields.py::test_autodoc_pydantic_field_show_examples_ignore_extraYou can run them collectively with the other tests using
poetry run pytestor individually by usingpoetry run pytest tests/test_configuration_fields.py::name_of_classThe
conftest.pywas also updated to include theFalsedefault configuration and keep the behaviour ofCONF_DEACTIVATEas expected.4. Add configuration settings
A new boolean configuration was added called
field_show_examples. The configuration behaves like all others*show*configurations accepting eitherTrueorFalseand it defaults toTrue.5. Implement behaviour
The implemented behaviour was achieved with the support of
autodoc_pydantic/inspection.py::FieldInspector.has_examplesandautodoc_pydantic/inspection.py::FieldInspector.get_examples. The string to add the examples is generated byautodoc_pydantic/directives/autodocumented.py::PydanticFieldDocumenter.add_exampleswhich is now ran inautodoc_pydantic/directives/autodocumented.py::PydanticFieldDocumenter.add_content6. Update document
The documentation was updated to include the description in the
configuration.rstand the default usage_automodule was also updated inusage.rstto showcase a simple field example with examples.