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

1"""Registry for OpenAPI type documenters.""" 

2 

3from __future__ import annotations 

4 

5from typing import Any, Protocol 

6 

7 

8class TypeDocumenterProtocol(Protocol): 

9 """Protocol for type documenters.""" 

10 

11 def can_document(self, annotation: Any) -> bool: 

12 """Check if this documenter can handle the given annotation.""" 

13 ... 

14 

15 def document(self, parameter: dict[str, Any]) -> None: 

16 """Apply documentation schema to the parameter.""" 

17 ... 

18 

19 

20class IntDocumenter: 

21 """Documenter for integer types.""" 

22 

23 def can_document(self, annotation: Any) -> bool: 

24 return annotation is int 

25 

26 def document(self, parameter: dict[str, Any]) -> None: 

27 parameter["schema"] = {"type": "integer"} 

28 

29 

30class BoolDocumenter: 

31 """Documenter for boolean types.""" 

32 

33 def can_document(self, annotation: Any) -> bool: 

34 return annotation is bool 

35 

36 def document(self, parameter: dict[str, Any]) -> None: 

37 parameter["schema"] = {"type": "boolean"} 

38 

39 

40class FloatDocumenter: 

41 """Documenter for float types.""" 

42 

43 def can_document(self, annotation: Any) -> bool: 

44 return annotation is float 

45 

46 def document(self, parameter: dict[str, Any]) -> None: 

47 parameter["schema"] = {"type": "number"} 

48 

49 

50class StringDocumenter: 

51 """Documenter for string types (default).""" 

52 

53 def can_document(self, annotation: Any) -> bool: 

54 return annotation is str 

55 

56 def document(self, parameter: dict[str, Any]) -> None: 

57 parameter["schema"] = {"type": "string"} 

58 

59 

60class TypeDocumenterRegistry: 

61 """Registry for type documenters.""" 

62 

63 def __init__(self) -> None: 

64 self._documenters: list[TypeDocumenterProtocol] = [ 

65 IntDocumenter(), 

66 BoolDocumenter(), 

67 FloatDocumenter(), 

68 StringDocumenter(), 

69 ] 

70 

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 

77 

78 

79_type_documenter_registry = TypeDocumenterRegistry()