Skip to content

Commit

Permalink
ci: Integrate mypy type checking (#259)
Browse files Browse the repository at this point in the history
refactor: restructure sphinx app cfg values in `__init__`
refactor: rename `PydanticDirectiveBase` to `PydanticDirectiveMixin` for better clarity
ci: fix broken tox environment naming convention preventing correct version selection
  • Loading branch information
mansenfranzen authored Apr 22, 2024
1 parent 71e1c60 commit 134c3cb
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 144 deletions.
4 changes: 3 additions & 1 deletion .github/actions/invoke-tox/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ runs:
shell: bash

- name: Invoke Tox
run: tox -e ${{ inputs.tox-environment }}
run: |
TOX_ENV_WITHOUT_DOTS=$(echo ${{ inputs.tox-environment }} | sed 's/\.//g')
tox -e $TOX_ENV_WITHOUT_DOTS
shell: bash

- name: Code Coverage
Expand Down
16 changes: 14 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
uses: ./.github/actions/invoke-tox
with:
python-version: ${{ matrix.python_version }}
tox-environment: pydantic${{ matrix.pydantic_version }}-sphinx${{ matrix.sphinx_version }}
tox-environment: py${{ matrix.python_version }}-pydantic${{ matrix.pydantic_version }}-sphinx${{ matrix.sphinx_version }}
install-graphviz: true
codacy: ${{ secrets.CODACY_PROJECT_TOKEN }}

Expand Down Expand Up @@ -122,4 +122,16 @@ jobs:
- name: Setup Test Environment and Run Tox
uses: ./.github/actions/invoke-tox
with:
tox-environment: formatter
tox-environment: formatter

type-checker:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Test Environment and Run Tox
uses: ./.github/actions/invoke-tox
with:
tox-environment: type-checker
install-graphviz: true
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ myst-parser = {version = "^2.0", optional = true }
pytest = {version = "^8.0.0", optional = true }
coverage = { version ="^7", optional = true }

# extras type checking
mypy = { version = "^1.9", optional = true }
types-docutils = { version = "^0.20", optional = true }
typing-extensions = { version = "^4.11", markers = "python_version <= '3.9'", optional = true }

# extras linting/formatting
ruff = { version = "^0.3", optional = true }

Expand All @@ -59,6 +64,10 @@ test = ["pytest",

linting = ["ruff"]

type_checking = ["mypy",
"types-docutils",
"typing-extensions"]

erdantic = ["erdantic"]

[build-system]
Expand Down
145 changes: 92 additions & 53 deletions sphinxcontrib/autodoc_pydantic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Literal

try:
from importlib.metadata import version
except ModuleNotFoundError:
from importlib_metadata import version
from importlib_metadata import version # type: ignore[no-redef,import-not-found]

from pydantic import BaseModel
from sphinx.domains import ObjType

from sphinxcontrib.autodoc_pydantic.directives.autodocumenters import (
Expand Down Expand Up @@ -37,6 +38,88 @@
from sphinx.application import Sphinx


PRE = 'autodoc_pydantic_'


class AppConfig(BaseModel):
name: str
default: Any
types: type
rebuild: Literal['env'] = 'env'


APP_CONFIGURATIONS = [
# settings
AppConfig(name=f'{PRE}settings_show_json', default=True, types=bool),
AppConfig(
name=f'{PRE}settings_show_json_error_strategy',
default=OptionsJsonErrorStrategy.WARN,
types=str,
),
AppConfig(name=f'{PRE}settings_show_config_summary', default=True, types=bool),
AppConfig(name=f'{PRE}settings_show_validator_members', default=True, types=bool),
AppConfig(name=f'{PRE}settings_show_validator_summary', default=True, types=bool),
AppConfig(name=f'{PRE}settings_show_field_summary', default=True, types=bool),
AppConfig(
name=f'{PRE}settings_summary_list_order',
default=OptionsSummaryListOrder.ALPHABETICAL,
types=str,
),
AppConfig(name=f'{PRE}settings_hide_paramlist', default=True, types=bool),
AppConfig(name=f'{PRE}settings_hide_reused_validator', default=True, types=bool),
AppConfig(name=f'{PRE}settings_undoc_members', default=True, types=bool),
AppConfig(name=f'{PRE}settings_members', default=True, types=bool),
AppConfig(name=f'{PRE}settings_member_order', default='groupwise', types=str),
AppConfig(
name=f'{PRE}settings_signature_prefix',
default='pydantic settings',
types=str,
),
# model
AppConfig(name=f'{PRE}model_show_json', default=True, types=bool),
AppConfig(
name=f'{PRE}model_show_json_error_strategy',
default=OptionsJsonErrorStrategy.WARN,
types=str,
),
AppConfig(name=f'{PRE}model_show_config_summary', default=True, types=bool),
AppConfig(name=f'{PRE}model_show_validator_members', default=True, types=bool),
AppConfig(name=f'{PRE}model_show_validator_summary', default=True, types=bool),
AppConfig(name=f'{PRE}model_show_field_summary', default=True, types=bool),
AppConfig(
name=f'{PRE}model_summary_list_order',
default=OptionsSummaryListOrder.ALPHABETICAL,
types=str,
),
AppConfig(name=f'{PRE}model_hide_paramlist', default=True, types=bool),
AppConfig(name=f'{PRE}model_hide_reused_validator', default=True, types=bool),
AppConfig(name=f'{PRE}model_undoc_members', default=True, types=bool),
AppConfig(name=f'{PRE}model_members', default=True, types=bool),
AppConfig(name=f'{PRE}model_member_order', default='groupwise', types=str),
AppConfig(name=f'{PRE}model_signature_prefix', default='pydantic model', types=str),
AppConfig(name=f'{PRE}model_erdantic_figure', default=False, types=bool),
AppConfig(name=f'{PRE}model_erdantic_figure_collapsed', default=True, types=bool),
# validator
AppConfig(name=f'{PRE}validator_signature_prefix', default='validator', types=str),
AppConfig(name=f'{PRE}validator_replace_signature', default=True, types=bool),
AppConfig(name=f'{PRE}validator_list_fields', default=False, types=bool),
# field
AppConfig(name=f'{PRE}field_list_validators', default=True, types=bool),
AppConfig(
name=f'{PRE}field_doc_policy', default=OptionsFieldDocPolicy.BOTH, types=str
),
AppConfig(name=f'{PRE}field_show_constraints', default=True, types=bool),
AppConfig(name=f'{PRE}field_show_alias', default=True, types=bool),
AppConfig(name=f'{PRE}field_show_default', default=True, types=bool),
AppConfig(name=f'{PRE}field_show_required', default=True, types=bool),
AppConfig(name=f'{PRE}field_show_optional', default=True, types=bool),
AppConfig(name=f'{PRE}field_swap_name_and_alias', default=False, types=bool),
AppConfig(name=f'{PRE}field_signature_prefix', default='field', types=str),
# general
AppConfig(name=f'{PRE}add_fallback_css_class', default=True, types=bool),
]


def add_css_file(app: Sphinx, *_) -> None: # noqa: ANN002
"""Adds custom css to HTML output."""

Expand Down Expand Up @@ -71,57 +154,13 @@ def add_domain_object_types(app: Sphinx) -> None:
def add_configuration_values(app: Sphinx) -> None:
"""Adds all configuration values to sphinx application."""

stem = 'autodoc_pydantic_'
add = app.add_config_value
json_strategy = OptionsJsonErrorStrategy.WARN
summary_list_order = OptionsSummaryListOrder.ALPHABETICAL

# ruff: noqa: FBT003
add(f'{stem}settings_show_json', True, True, bool)
add(f'{stem}settings_show_json_error_strategy', json_strategy, True, str)
add(f'{stem}settings_show_config_summary', True, True, bool)
add(f'{stem}settings_show_validator_members', True, True, bool)
add(f'{stem}settings_show_validator_summary', True, True, bool)
add(f'{stem}settings_show_field_summary', True, True, bool)
add(f'{stem}settings_summary_list_order', summary_list_order, True, str)
add(f'{stem}settings_hide_paramlist', True, True, bool)
add(f'{stem}settings_hide_reused_validator', True, True, bool)
add(f'{stem}settings_undoc_members', True, True, bool)
add(f'{stem}settings_members', True, True, bool)
add(f'{stem}settings_member_order', 'groupwise', True, str)
add(f'{stem}settings_signature_prefix', 'pydantic settings', True, str)

add(f'{stem}model_show_json', True, True, bool)
add(f'{stem}model_show_json_error_strategy', json_strategy, True, str)
add(f'{stem}model_show_config_summary', True, True, bool)
add(f'{stem}model_show_validator_members', True, True, bool)
add(f'{stem}model_show_validator_summary', True, True, bool)
add(f'{stem}model_show_field_summary', True, True, bool)
add(f'{stem}model_summary_list_order', summary_list_order, True, str)
add(f'{stem}model_hide_paramlist', True, True, bool)
add(f'{stem}model_hide_reused_validator', True, True, bool)
add(f'{stem}model_undoc_members', True, True, bool)
add(f'{stem}model_members', True, True, bool)
add(f'{stem}model_member_order', 'groupwise', True, str)
add(f'{stem}model_signature_prefix', 'pydantic model', True, str)
add(f'{stem}model_erdantic_figure', False, True, bool)
add(f'{stem}model_erdantic_figure_collapsed', True, True, bool)

add(f'{stem}validator_signature_prefix', 'validator', True, str)
add(f'{stem}validator_replace_signature', True, True, bool)
add(f'{stem}validator_list_fields', False, True, bool)

add(f'{stem}field_list_validators', True, True, bool)
add(f'{stem}field_doc_policy', OptionsFieldDocPolicy.BOTH, True, str)
add(f'{stem}field_show_constraints', True, True, bool)
add(f'{stem}field_show_alias', True, True, bool)
add(f'{stem}field_show_default', True, True, bool)
add(f'{stem}field_show_required', True, True, bool)
add(f'{stem}field_show_optional', True, True, bool)
add(f'{stem}field_swap_name_and_alias', False, True, bool)
add(f'{stem}field_signature_prefix', 'field', True, str)

add(f'{stem}add_fallback_css_class', True, True, bool)
for config in APP_CONFIGURATIONS:
app.add_config_value(
name=config.name,
default=config.default,
types=config.types,
rebuild=config.rebuild,
)


def add_directives_and_autodocumenters(app: Sphinx) -> None:
Expand Down
Loading

0 comments on commit 134c3cb

Please sign in to comment.