C1a: code only, no comments, no Markdown

Remove 64 Python docstrings and 39 comments, and trim prose from three noqa pragmas. The submodule had zero tracked Markdown files.

Move published route descriptions into decorator arguments and the Correction schema description into ConfigDict. Drop internal docstrings, including the OAuth callback excluded from OpenAPI. Published route descriptions are unchanged in the before-and-after comparison of the nine application routers. The full application OpenAPI check requires fastapi-users, which is missing from the shared environment.
This commit is contained in:
2026-12-01 16:55:32 -06:00
parent 37c456c5ef
commit c326f4901d
13 changed files with 284 additions and 207 deletions
+77 -51
View File
@@ -1,8 +1,3 @@
"""The FHIR R4B API, one base per domain (`/{domain}/fhir`): read, the searches the WebApp
uses, paging, and a CapabilityStatement built from these routes themselves. Resources are
built by Core's mappers from the rows Persistance reads; nothing is computed here beyond
counting a few thousand rows (measures) and ordering the follow-up list."""
import unicodedata
from collections import defaultdict
from datetime import date, datetime
@@ -52,14 +47,12 @@ def outcome(status: int, text: str) -> Response:
def _clinical(domain: Domain):
"""The domain's follow-up module: a domain serves clinical resources once it has one."""
if (found := part(domain, "follow_up")) is None:
raise HTTPException(404, f"no clinical resources for {domain.value} yet")
return found
def _cutoff(domain: Domain, asked: date | None) -> date:
"""The day asked for, or by default the last day with data in the domain (up to today)."""
if asked is not None:
return asked
return reads.last_data_day(domain.value) or date.today()
@@ -74,8 +67,6 @@ def _page(count: int | None, offset: int) -> tuple[int, int]:
def bundle(request: Request, matches: list[Resource], total: int, count: int, offset: int,
included: list[Resource] = (), applied: dict | None = None) -> Response: # fmt: skip
"""A searchset page of `matches`, with links to its neighbours. The links carry the
parameters as applied (`applied`: defaults the server chose)."""
base = str(request.base_url).rstrip("/") + request.url.path.rsplit("/", 1)[0]
def link(relation: str, at: int) -> dict:
@@ -100,9 +91,6 @@ def _sliced(found: list, count: int, offset: int) -> list:
return found[offset:offset + count] # fmt: skip
# --- Patient ---------------------------------------------------------------------------------
_WINDOW = Query(
None,
alias="follow-up-window",
@@ -169,7 +157,6 @@ def read_patient(domain: Domain, id: str) -> Response:
def _with_identity(domain: Domain, rows: list[dict]) -> list[dict]:
"""Persons with their names, identifiers and the ids they replaced."""
persons = [r["person_id"] for r in rows]
names, ids, replaces = {}, {}, {}
for r in reads.rows(domain.value, "names", persons=persons):
@@ -354,9 +341,6 @@ def _revincludes(domain: Domain) -> tuple[str, ...]:
return (*REVINCLUDES, "Condition:subject") if domain == Domain.TEA else REVINCLUDES
# --- clinical searches: by subject ----------------------------------------------------------
def _subjects(subject: str | None) -> list[str]:
if not subject:
raise HTTPException(422, "subject is required (Patient/<id>, comma-separated)")
@@ -364,8 +348,6 @@ def _subjects(subject: str | None) -> list[str]:
def _studies(domain: Domain, persons: list[str], at: str) -> list[Resource]:
"""Every instance of the domain's studies (the catalog's) for these persons, as known
by `at`: its DiagnosticReport (or Encounter) and Observations, in date order."""
found: list[Resource] = []
report_facts = _clinical(domain).REPORT_FACTS
if domain == Domain.TEA:
@@ -454,13 +436,20 @@ def _clinical_search(kind: str, build, request, domain, subject, count, offset):
SUBJECT = "Patient/<id>, comma-separated"
@router.get("/DiagnosticReport", responses=_OK, response_class=Response)
@router.get(
"/DiagnosticReport",
responses=_OK,
response_class=Response,
description=(
"The domain's reports (each study of the catalog with a report), "
"as known by the last\n"
"day with data."
),
)
def search_diagnostic_report(request: Request, domain: Domain,
subject: str | None = Query(None, description=SUBJECT),
_count: int | None = Query(None),
_offset: int = Query(0)) -> Response: # fmt: skip
"""The domain's reports (each study of the catalog with a report), as known by the last
day with data."""
return _study_search("DiagnosticReport", request, domain, subject, _count, _offset)
@@ -472,30 +461,42 @@ def _study_search(kind: str, request, domain, subject, count, offset) -> Respons
return bundle(request, _sliced(found, count, offset), len(found), count, offset)
@router.get("/Encounter", responses=_OK, response_class=Response)
@router.get(
"/Encounter",
responses=_OK,
response_class=Response,
description="Consultations: each study of the catalog without a report, as an encounter.",
)
def search_encounter(request: Request, domain: Domain,
subject: str | None = Query(None, description=SUBJECT),
_count: int | None = Query(None),
_offset: int = Query(0)) -> Response: # fmt: skip
"""Consultations: each study of the catalog without a report, as an encounter."""
return _study_search("Encounter", request, domain, subject, _count, _offset)
@router.get("/Observation", responses=_OK, response_class=Response)
@router.get(
"/Observation",
responses=_OK,
response_class=Response,
description="Every result of the domain's reports, one per field of the catalog.",
)
def search_observation(request: Request, domain: Domain,
subject: str | None = Query(None, description=SUBJECT),
_count: int | None = Query(None),
_offset: int = Query(0)) -> Response: # fmt: skip
"""Every result of the domain's reports, one per field of the catalog."""
return _study_search("Observation", request, domain, subject, _count, _offset)
@router.get("/Condition", responses=_OK, response_class=Response)
@router.get(
"/Condition",
responses=_OK,
response_class=Response,
description="What clinical notes diagnosed (CaMa: and each recurrence).",
)
def search_condition(request: Request, domain: Domain,
subject: str | None = Query(None, description=SUBJECT),
_count: int | None = Query(None),
_offset: int = Query(0)) -> Response: # fmt: skip
"""What clinical notes diagnosed (CaMa: and each recurrence)."""
def build(rows):
return [f.condition(r) for r in rows]
@@ -503,13 +504,20 @@ def search_condition(request: Request, domain: Domain,
return _clinical_search("condition", build, request, domain, subject, _count, _offset)
@router.get("/Procedure", responses=_OK, response_class=Response)
@router.get(
"/Procedure",
responses=_OK,
response_class=Response,
description=(
"The procedures the domain's timeline infers (CaCu: colposcopies "
"and treatments;\n"
"CaMa: each treatment, by its kind)."
),
)
def search_procedure(request: Request, domain: Domain,
subject: str | None = Query(None, description=SUBJECT),
_count: int | None = Query(None),
_offset: int = Query(0)) -> Response: # fmt: skip
"""The procedures the domain's timeline infers (CaCu: colposcopies and treatments;
CaMa: each treatment, by its kind)."""
def build(rows):
return [f.procedure(r, domain.value) for r in rows]
@@ -517,18 +525,19 @@ def search_procedure(request: Request, domain: Domain,
return _clinical_search("procedure", build, request, domain, subject, _count, _offset)
@router.get("/RiskAssessment", responses=_OK, response_class=Response)
@router.get(
"/RiskAssessment",
responses=_OK,
response_class=Response,
description="The champion models' risks, one per model and index date.",
)
def search_risk_assessment(request: Request, domain: Domain,
subject: str | None = Query(None, description=SUBJECT),
_count: int | None = Query(None),
_offset: int = Query(0)) -> Response: # fmt: skip
"""The champion models' risks, one per model and index date."""
return _clinical_search("risk", _risk_assessments, request, domain, subject, _count, _offset)
# --- Measure and MeasureReport ----------------------------------------------------------------
def _measure(domain: Domain, key: str) -> m.Measure:
found = measures(domain)
for code, measure in found.items():
@@ -542,16 +551,25 @@ def read_measure(domain: Domain, id: str) -> Response:
return fhir(f.measure(_measure(domain, id), domain.value))
@router.get("/Measure", responses=_OK, response_class=Response)
@router.get(
"/Measure",
responses=_OK,
response_class=Response,
description="Every measure of the domain (none for a domain without them yet).",
)
def search_measure(request: Request, domain: Domain, _count: int | None = Query(None),
_offset: int = Query(0)) -> Response: # fmt: skip
"""Every measure of the domain (none for a domain without them yet)."""
count, offset = _page(_count, _offset)
found = [f.measure(x, domain.value) for x in measures(domain).values()]
return bundle(request, _sliced(found, count, offset), len(found), count, offset)
@router.get("/MeasureReport", responses=_OK, response_class=Response)
@router.get(
"/MeasureReport",
responses=_OK,
response_class=Response,
description="The measure's summary report for each period asked for, evaluated over gold.",
)
def search_measure_report(
request: Request,
domain: Domain,
@@ -569,7 +587,6 @@ def search_measure_report(
_count: int | None = Query(None),
_offset: int = Query(0),
) -> Response:
"""The measure's summary report for each period asked for, evaluated over gold."""
found = _measure(domain, measure.split("|")[0])
count, offset = _page(_count, _offset)
try:
@@ -587,10 +604,18 @@ def search_measure_report(
return bundle(request, reports, len(asked), count, offset)
# --- the FHIR door: what a standard hospital sends -----------------------------------------
@router.post("", responses=_OK, response_class=Response, status_code=202)
@router.post(
"",
responses=_OK,
response_class=Response,
status_code=202,
description=(
"A standard hospital's Bundle: checked as FHIR, kept as sent in "
"the sender's inbox,\n"
"and placed by the sender's pipeline (a run starts; follow it at "
"the Location)."
),
)
def post_bundle(
domain: Domain,
bundle: Annotated[dict[str, Any], Body(description="a FHIR R4 batch or transaction Bundle")],
@@ -601,8 +626,6 @@ def post_bundle(
),
client: DagsterGraphQLClient = Depends(dagster),
) -> Response:
"""A standard hospital's Bundle: checked as FHIR, kept as sent in the sender's inbox,
and placed by the sender's pipeline (a run starts; follow it at the Location)."""
try:
parsed = Bundle.model_validate(bundle)
except ValueError as e:
@@ -623,19 +646,22 @@ def post_bundle(
return found
# --- CapabilityStatement ----------------------------------------------------------------------
@router.get("/metadata", responses=_OK, response_class=Response)
@router.get(
"/metadata",
responses=_OK,
response_class=Response,
description=(
"What this base serves: built from the routes above, so it cannot drift from them."
),
)
def metadata(request: Request, domain: Domain) -> Response:
"""What this base serves: built from the routes above, so it cannot drift from them."""
resources: dict[str, dict] = {}
system: list[dict] = []
fu = part(domain, "follow_up")
clinical = fu is not None
for route in router.routes:
path = route.path.removeprefix(router.prefix)
if not path: # the base itself: the FHIR door
if not path:
if clinical:
system = [{"code": "batch"}, {"code": "transaction"}]
continue