33import logging
44import pickle
55from collections .abc import Iterable , Mapping , Sequence
6- from typing import Self , cast
6+ from typing import cast
77
88import anyio
99import httpx
1313
1414from .config import Config
1515
16- type JSON = str | int | float | Mapping [str , Self ] | Sequence [Self ] | None
17-
1816logger = logging .getLogger (__name__ )
1917
2018
21- async def load_document (root : anyio .Path , config : Config , cache : bool ) -> JSON :
19+ async def load_document (root : anyio .Path , config : Config , cache : bool ) -> Mapping :
2220 cache_root = anyio .Path (platformdirs .user_cache_path ('lapidary' ))
2321
2422 logger .info ('Load OpenAPI document' )
25- spec_dict = await load_parse (root , config .document_path , cache , cache_root )
23+ spec_dict : dict = cast ( dict , await load_parse (root , config .document_path , cache , cache_root ) )
2624
2725 if (patch := await load_patches (root / config .patches , cache , cache_root )) is not None :
28- spec_dict = patch .apply (cast ( dict , spec_dict ) )
26+ spec_dict = patch .apply (spec_dict )
2927
3028 return spec_dict
3129
3230
33- async def load_parse (root : anyio .Path , path : str | anyio .Path , cache : bool , cache_root : anyio .Path ) -> JSON :
31+ async def load_parse (
32+ root : anyio .Path , path : str | anyio .Path , cache : bool , cache_root : anyio .Path
33+ ) -> Mapping | Sequence :
3434 text = await document_handler_for (root , path ).load ()
3535 if cache :
3636 return await parse_cache (text , cache_root )
3737 else :
3838 return parse (text )
3939
4040
41- async def parse_cache (text : str , cache_root : anyio .Path ) -> JSON :
41+ async def parse_cache (text : str , cache_root : anyio .Path ) -> Mapping :
4242 digest = hashlib .sha224 (text .encode ()).hexdigest ()
4343 cache_path = anyio .Path ((cache_root / digest ).with_suffix ('.pickle' + str (pickle .HIGHEST_PROTOCOL )))
4444 if await cache_path .exists ():
@@ -51,7 +51,7 @@ async def parse_cache(text: str, cache_root: anyio.Path) -> JSON:
5151 return d
5252
5353
54- def parse (text : str ) -> JSON :
54+ def parse (text : str ) -> Mapping :
5555 yaml = ruamel .yaml .YAML (typ = 'safe' )
5656 return yaml .load (text )
5757
0 commit comments