[+] improve typing

This commit is contained in:
Siarhei Siniak 2025-05-19 12:24:51 +03:00
parent 9f4b426229
commit 591c893909

@ -13,7 +13,7 @@ import logging
from typing import (Optional, Any, cast, Type, TypeVar,)
from typing_extensions import (
Self, BinaryIO,
Self, BinaryIO, overload,
)
logger = logging.getLogger(__name__)
@ -61,21 +61,45 @@ class PyProject:
Key = TypeVar('Key')
Value = TypeVar('Value')
@overload
def check_dict(
value: Any,
KT: Type[Key],
VT: Type[Value],
) -> dict[Key, Value]: ...
@overload
def check_dict(
value: Any,
KT: Type[Key],
) -> dict[Key, Any]: ...
def check_dict(
value: Any,
KT: Type[Key],
VT: Optional[Type[Value]] = None,
) -> dict[Key, Value]:
assert isinstance(value, dict)
value2 = cast(dict[Any, Any], value)
assert all([
isinstance(k, KT) and isinstance(v, VT)
isinstance(k, KT) and (
VT is None or
isinstance(v, VT)
)
for k, v in value2.items()
])
return cast(
dict[Key, Value],
value,
)
if VT is None:
return cast(
dict[Key, Any],
value,
)
else:
return cast(
dict[Key, Value],
value,
)
def pyproject_load(
d: pathlib.Path,
@ -97,9 +121,21 @@ def pyproject_load(
dict
)
for k, v in content['project']['optional-dependencies'].items():
assert isinstance(v, list)
assert isinstance(k, str)
for k, v in check_dict(
check_dict(
check_dict(
content,
str,
# Any,
)['project'],
str,
# Any,
)['optional-dependencies'],
str,
list[Any],
).items():
# assert isinstance(v, list)
# assert isinstance(k, str)
dependencies[k] = v