Skip to content

Commit fe9612a

Browse files
author
Raphael Krupinski
committed
πŸ§‘β€πŸ’» Simplify type hints in json loading code.
1 parent a3e6196 commit fe9612a

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

β€Žsrc/lapidary/render/load.pyβ€Ž

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import pickle
55
from collections.abc import Iterable, Mapping, Sequence
6-
from typing import Self, cast
6+
from typing import cast
77

88
import anyio
99
import httpx
@@ -13,32 +13,32 @@
1313

1414
from .config import Config
1515

16-
type JSON = str | int | float | Mapping[str, Self] | Sequence[Self] | None
17-
1816
logger = 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

Comments
Β (0)