11import base64
22import copy
33from abc import ABC , abstractmethod
4- from dataclasses import asdict , fields , is_dataclass
4+ from dataclasses import asdict , dataclass , fields , is_dataclass
55from datetime import date , datetime , time
66from enum import Enum
7- from typing import Any , List , Tuple
7+ from typing import Any , Callable , Iterable , cast
88from uuid import UUID
99
1010import yaml
@@ -18,10 +18,12 @@ class Format(Enum):
1818 JSON = "JSON"
1919
2020
21+ @dataclass
2122class OpenAPIElement :
2223 """Base class for all OpenAPI Elements"""
2324
2425
26+ @dataclass
2527class OpenAPIRoot (OpenAPIElement ):
2628 """Base class for a root OpenAPI Documentation"""
2729
@@ -67,8 +69,8 @@ def normalize_key(key: Any) -> str:
6769 return "" .join ([first .lower (), * map (str .title , others )])
6870
6971
70- def normalize_dict_factory (items : List [ Tuple [Any , Any ]]) -> Any :
71- data = {}
72+ def normalize_dict_factory (items : list [ tuple [Any , Any ]]) -> dict [ str , Any ] :
73+ data : dict [ str , Any ] = {}
7274 for key , value in items :
7375 if value is None :
7476 continue
@@ -87,8 +89,8 @@ def normalize_dict_factory(items: List[Tuple[Any, Any]]) -> Any:
8789 return data
8890
8991
90- def regular_dict_factory (items : List [ Tuple [Any , Any ]]) -> Any :
91- data = {}
92+ def regular_dict_factory (items : list [ tuple [Any , Any ]]) -> dict [ Any , Any ] :
93+ data : dict [ Any , Any ] = {}
9294 for key , value in items :
9395 for handler in TYPES_HANDLERS :
9496 value = handler .normalize (value )
@@ -100,11 +102,11 @@ def regular_dict_factory(items: List[Tuple[Any, Any]]) -> Any:
100102# replicates the asdict method from dataclasses module, to support
101103# bypassing "asdict" on child properties when they implement a `to_obj`
102104# method: some entities require a specific shape when represented
103- def _asdict_inner (obj , dict_factory ) :
105+ def _asdict_inner (obj : Any , dict_factory : Callable [[ Any ], Any ]) -> Any :
104106 if hasattr (obj , "to_obj" ):
105107 return obj .to_obj ()
106108 if isinstance (obj , OpenAPIElement ):
107- result = []
109+ result : list [ tuple [ str , Any ]] = []
108110 for f in fields (obj ):
109111 value = _asdict_inner (getattr (obj , f .name ), dict_factory )
110112 result .append ((f .name , value ))
@@ -115,11 +117,13 @@ def _asdict_inner(obj, dict_factory):
115117 if hasattr (obj , "dict" ) and callable (obj .dict ):
116118 # For Pydantic 1
117119 return obj .dict ()
118- if is_dataclass (obj ):
120+ if is_dataclass (obj ) and not isinstance ( obj , type ) :
119121 return asdict (obj , dict_factory = regular_dict_factory )
120122 elif isinstance (obj , (list , tuple )):
123+ obj = cast (Iterable [Any ], obj )
121124 return type (obj )(_asdict_inner (v , dict_factory ) for v in obj )
122125 elif isinstance (obj , dict ):
126+ obj = cast (dict [Any , Any ], obj )
123127 return type (obj )(
124128 (_asdict_inner (k , dict_factory ), _asdict_inner (v , dict_factory ))
125129 for k , v in obj .items ()
@@ -128,7 +132,7 @@ def _asdict_inner(obj, dict_factory):
128132 return copy .deepcopy (obj )
129133
130134
131- def normalize_dict (obj ) :
135+ def normalize_dict (obj : Any ) -> Any :
132136 if hasattr (obj , "dict" ) and callable (obj .dict ):
133137 return obj .dict ()
134138 if hasattr (obj , "to_obj" ):
0 commit comments