Coverage for src/lexigram/web/docs/type_registry.py: 0%
34 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-25 04:37 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-25 04:37 +0800
1"""Registry for OpenAPI type documenters."""
3from __future__ import annotations
5from typing import Any, Protocol
8class TypeDocumenterProtocol(Protocol):
9 """Protocol for type documenters."""
11 def can_document(self, annotation: Any) -> bool:
12 """Check if this documenter can handle the given annotation."""
13 ...
15 def document(self, parameter: dict[str, Any]) -> None:
16 """Apply documentation schema to the parameter."""
17 ...
20class IntDocumenter:
21 """Documenter for integer types."""
23 def can_document(self, annotation: Any) -> bool:
24 return annotation is int
26 def document(self, parameter: dict[str, Any]) -> None:
27 parameter["schema"] = {"type": "integer"}
30class BoolDocumenter:
31 """Documenter for boolean types."""
33 def can_document(self, annotation: Any) -> bool:
34 return annotation is bool
36 def document(self, parameter: dict[str, Any]) -> None:
37 parameter["schema"] = {"type": "boolean"}
40class FloatDocumenter:
41 """Documenter for float types."""
43 def can_document(self, annotation: Any) -> bool:
44 return annotation is float
46 def document(self, parameter: dict[str, Any]) -> None:
47 parameter["schema"] = {"type": "number"}
50class StringDocumenter:
51 """Documenter for string types (default)."""
53 def can_document(self, annotation: Any) -> bool:
54 return annotation is str
56 def document(self, parameter: dict[str, Any]) -> None:
57 parameter["schema"] = {"type": "string"}
60class TypeDocumenterRegistry:
61 """Registry for type documenters."""
63 def __init__(self) -> None:
64 self._documenters: list[TypeDocumenterProtocol] = [
65 IntDocumenter(),
66 BoolDocumenter(),
67 FloatDocumenter(),
68 StringDocumenter(),
69 ]
71 def get_documenter(self, annotation: Any) -> TypeDocumenterProtocol | None:
72 """Find a documenter for the given annotation."""
73 for documenter in self._documenters:
74 if documenter.can_document(annotation):
75 return documenter
76 return None
79_type_documenter_registry = TypeDocumenterRegistry()