Skip to content

style: enforce Google docstrings and type hints - #145

Merged
uniqueg merged 11 commits into
devfrom
style/docstrings-type-hints
Oct 21, 2026
Merged

uniqueg merged 11 commits into
devfrom
style/docstrings-type-hints

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #136.

  • Docstrings: ruff's pydocstyle rules (D) with convention = "google" are enabled for the whole repository (package, tests and the MkDocs hooks in docs/hooks.py), without per-file ignores. Every package, module, class, function and method has a Google-style docstring with a one-line summary, including private helpers and the nested fakes of the tests (which pydocstyle doesn't require); wrapped summaries were rephrased (D205), and the keyword arguments that Connexion passes to controllers are documented (**kwargs).
  • Type hints: mypy now runs with disallow_untyped_defs and disallow_any_generics, on cloud_registry, docs/hooks.py and tests. All functions are fully annotated (-> None on tests, fixture and parameter types, **kwargs: Any on controllers), and generic types are parametrized (e.g., dict[str, Any], Collection[dict[str, Any]], re.Match[str]). Annotating the tests made mypy check their bodies, which it skipped before; the errors that showed were fixed with precise types and asserts, e.g., FOCA's typed get_app_config() and get_custom_config() instead of app.config.foca, and get_json() for response bodies (Werkzeug types the json property as optional).
  • Suppressions: every type: ignore and noqa states its reason (as a comment above the line for type: ignore, in parentheses after noqa), including the ones that were there before.
  • MkDocs hooks: the settings the hook uses are typed with a Protocol, so that mypy checks it without MkDocs, which only the docs extra installs (the static analysis job installs the dev extra, on Python 3.10).
  • Both checks run in the existing pre-commit hooks (ruff, mypy), and so in CI; no workflow changes, and no docs prose changes.

The changes are behaviour-neutral: signatures, decorators and defaults of all functions are unchanged; the same 733 tests are collected and pass; test assertions are unchanged or stricter (asserts that documents exist before they are indexed).

Why mypy, not ruff ANN

mypy is already in pre-commit and CI, and with disallow_untyped_defs it reports every function with a missing parameter or return annotation, including *args and **kwargs, so it covers what ANN001-ANN206 check. It also checks that the hints are correct, and, once functions are annotated, their bodies. disallow_any_generics catches incomplete hints such as bare dict or list, which ANN doesn't. Adding ANN would report the same missing annotations a second time, and ANN401 would reject Any, which the JSON documents of the API need. Full strict mode was not used: with these settings, it reports only 61 errors more, none about missing hints: 42 from no_implicit_reexport (e.g., from foca import get_config, as FOCA's __init__.py has no __all__, and names that cloud_registry.exceptions re-exports from Werkzeug) and 19 from warn_return_any (values that are Any because Connexion, JSON or PyYAML return them untyped). Fixing those would mean casts or explicit re-exports, which add noise without making the hints more complete.

How it was verified

Locally (Python 3.12 venv with pip 24.0, ruff 0.17.1 as in pre-commit, Helm 3.18.6):

  • ruff check ., ruff format --check .: pass; mypy: no issues in 34 source files
  • A script listing definitions without docstrings (incl. private and nested ones) over cloud_registry, docs/hooks.py and tests: none
  • coverage run --source=cloud_registry -m pytest -W ignore::DeprecationWarning: 733 passed (same test IDs as on dev), coverage 100%
  • ast-grep scan, ast-grep test --skip-snapshot-tests: pass; mkdocs build --strict: passes
  • A tokenize-based comparison of the string literals of the tests with dev: only the keys of the new config helpers differ (no accidental edits in strings)

CI (run 39661644796, head 93cb865): all checks pass, including static code analysis (pre-commit with ruff and mypy, on Python 3.10), tests on Python 3.10 and 3.12, the Helm chart, the Docker Compose smoke test, the docs build, Read the Docs, codecov/patch and codecov/project.

Declined review findings

  • A "Code style" paragraph in docs/development.md (quality review): the docs prose is being reworked in parallel (docs: pre-release findings in docs and notes #134, docs: present app generally, use ELIXIR On-Cloud #137, docs: add code of conduct and contributing guide #138); the conventions are stated in pyproject.toml and enforced by the checks.
  • One shared _custom_config helper for the tests (quality review): tests/test_utils.py reads model_extra directly on purpose, as it tests get_custom_config() itself; the other helpers are small and local.
  • Renaming the Docs alias of the chart tests and moving it to tests/helm.py (quality review): only tests/test_chart.py uses it.
  • Aligning ruff's line length and rule set with FOCA (quality review): pre-existing differences, outside this issue.
  • One idiom for typed JSON bodies everywhere (quality review): bodies are read with get_json() throughout; a typed local is used only where a helper returns the body.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Standardize repository documentation and static typing with enforced Google docstrings and comprehensive mypy validation while preserving application behavior.

Enhancements:

  • Enforce Google-style docstrings across the package, documentation hooks, and tests.
  • Require complete, parameterized type annotations throughout the checked source and test code.
  • Document the rationale for and purpose of all existing type-checking and lint suppressions.
  • Add a typed protocol for MkDocs hook configuration and improve test typing around application configuration and JSON responses.

Tests:

  • Update tests and test helpers with complete type annotations and stricter type-safe assertions without changing their coverage or behavior.

Chores:

  • Enable Ruff pydocstyle checks with Google conventions and strengthen mypy checks for untyped definitions and incomplete generics.

Give every package and function of `cloud_registry` and the MkDocs hooks
a Google-style docstring with a one-line summary, document the keyword
arguments that Connexion passes to controllers, and complete the type
hints: annotated `**kwargs`, parametrized generic types, and a protocol
for the settings of MkDocs that the hooks use, so that they are checked
without MkDocs installed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR applies repository-wide Google-style docstring enforcement and complete static typing by configuring Ruff and mypy, then annotating and documenting production code, MkDocs hooks, fixtures, helpers, and tests. Type-checking exposed and prompted safer typed access to FOCA configuration, MongoDB data, and HTTP JSON responses; suppression comments now explain their rationale, while behavior, API signatures, test coverage, and CI workflows remain unchanged.

File-Level Changes

Change Details Files
Enabled repository-wide Google-style docstring linting and updated documentation to satisfy it.
  • Enabled Ruff pydocstyle rules with Google convention and no per-file ignores.
  • Added or rewrote module, class, function, method, private-helper, controller, fixture, and nested-fake docstrings with summaries and structured sections.
  • Documented Connexion-injected keyword arguments and added reasons to all existing suppression comments.
cloud_registry/app.py
cloud_registry/exceptions.py
cloud_registry/ga4gh/__init__.py
cloud_registry/ga4gh/registry/__init__.py
cloud_registry/ga4gh/registry/permissions.py
cloud_registry/ga4gh/registry/server.py
cloud_registry/ga4gh/registry/service.py
cloud_registry/ga4gh/registry/verification.py
cloud_registry/gunicorn_config.py
cloud_registry/service_models/custom_config.py
cloud_registry/utils.py
docs/hooks.py
tests/__init__.py
tests/conftest.py
tests/ga4gh/registry/test_server.py
tests/ga4gh/registry/test_service.py
tests/ga4gh/registry/test_verification.py
tests/gunicorn_settings.py
tests/helm.py
tests/mock_data.py
tests/service_models/__init__.py
tests/service_models/test_custom_config.py
tests/test_access_control.py
tests/test_app.py
tests/test_chart.py
tests/test_config.py
tests/test_docs_hooks.py
tests/test_exceptions.py
tests/test_gunicorn_config.py
tests/test_utils.py
tests/test_verification.py
tests/test_version.py
Enforced complete, checked type annotations across application, documentation hooks, and tests.
  • Configured mypy for cloud_registry, docs/hooks.py, and tests with disallow_untyped_defs and disallow_any_generics.
  • Annotated controller **kwargs, fixtures, test functions, nested callbacks, collections, mappings, regular-expression matches, responses, and generators.
  • Replaced untyped FOCA configuration access and optional Werkzeug JSON access with typed helpers, assertions, and get_json() calls.
  • Added a Protocol for the MkDocs hook configuration so hooks can be checked without installing MkDocs.
pyproject.toml
cloud_registry/exceptions.py
cloud_registry/ga4gh/registry/permissions.py
cloud_registry/ga4gh/registry/server.py
cloud_registry/ga4gh/registry/service.py
cloud_registry/ga4gh/registry/verification.py
cloud_registry/utils.py
docs/hooks.py
tests/conftest.py
tests/ga4gh/registry/test_server.py
tests/ga4gh/registry/test_service.py
tests/ga4gh/registry/test_verification.py
tests/helm.py
tests/mock_data.py
tests/test_access_control.py
tests/test_app.py
tests/test_chart.py
tests/test_config.py
tests/test_docs_hooks.py
tests/test_exceptions.py
tests/test_gunicorn_config.py
tests/test_utils.py
tests/test_verification.py
tests/test_version.py
Adjusted tests and typing-related implementation details without changing runtime behavior.
  • Introduced typed helpers for FOCA custom and application configuration, Mongo collections, rendered Helm manifests, response bodies, and verification results.
  • Strengthened assertions where static types require proving documents or configuration values exist.
  • Used inspect.unwrap() for decorated controller tests and preserved test behavior and coverage.
  • Kept ruff and mypy in the existing pre-commit and CI checks through configuration-only changes.
tests/conftest.py
tests/ga4gh/registry/test_server.py
tests/ga4gh/registry/test_service.py
tests/ga4gh/registry/test_verification.py
tests/test_access_control.py
tests/test_app.py
tests/test_chart.py
tests/test_config.py
tests/test_exceptions.py
tests/test_utils.py
tests/test_verification.py
pyproject.toml

Assessment against linked issues

Issue Objective Addressed Explanation
#136 Enable Google-style Ruff docstring checks and strict checks for missing type annotations, and run both in pre-commit and CI. ✅
#136 Add Google-style docstrings and complete type hints throughout the package, tests, and relevant documentation hooks. ✅
#136 Avoid per-file or inline exemptions unless their reasons are stated. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Type the shared fixtures and helpers of `tests/conftest.py` and give
them Google-style docstrings; read the collections of the unit test app
with FOCA's typed `get_app_config()`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7aedf13) to head (16d67a4).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #145   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          737       743    +6     
=========================================
+ Hits           737       743    +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

uniqueg and others added 8 commits October 21, 2026 21:01
Annotate the tests of the Helm chart and the config, and their helpers,
and give them Google-style docstrings with one-line summaries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Annotate the unit tests of the controllers, services, verifications and
config models, and their helpers and fakes, and give them Google-style
docstrings with one-line summaries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Annotate the API tests of access control and verifications, and their
helpers and fakes, and give them Google-style docstrings with one-line
summaries; read response bodies with `get_json()`, which is typed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Annotate the tests of the app, exceptions, utilities, Gunicorn settings,
MkDocs hooks and version, and their helpers and fakes, and give them
Google-style docstrings with one-line summaries; read response bodies
with `get_json()`, which is typed, and the config with FOCA's typed
accessors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Enable ruff's pydocstyle rules with the Google convention for the whole
repository, and make mypy reject functions without complete
annotations and generic types without parameters; it now also checks
the MkDocs hooks. Both run in pre-commit and so in CI.

Closes #136

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Give every `type: ignore` and `noqa` comment a reason, restore wording
that the shortened summaries lost, document keyword arguments as
`**kwargs` as Google style does, and note in `pyproject.toml` why mypy
checks the hints instead of ruff's `ANN` rules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg marked this pull request as ready for review October 21, 2026 19:30

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@uniqueg
uniqueg merged commit 11b8327 into dev Oct 21, 2026
12 checks passed
@uniqueg
uniqueg deleted the style/docstrings-type-hints branch October 21, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

style: enforce Google docstrings and type hints

1 participant