[+] improve typing
This commit is contained in:
parent
9f4b426229
commit
591c893909
@ -13,7 +13,7 @@ import logging
|
|||||||
|
|
||||||
from typing import (Optional, Any, cast, Type, TypeVar,)
|
from typing import (Optional, Any, cast, Type, TypeVar,)
|
||||||
from typing_extensions import (
|
from typing_extensions import (
|
||||||
Self, BinaryIO,
|
Self, BinaryIO, overload,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -61,21 +61,45 @@ class PyProject:
|
|||||||
Key = TypeVar('Key')
|
Key = TypeVar('Key')
|
||||||
Value = TypeVar('Value')
|
Value = TypeVar('Value')
|
||||||
|
|
||||||
|
@overload
|
||||||
def check_dict(
|
def check_dict(
|
||||||
value: Any,
|
value: Any,
|
||||||
KT: Type[Key],
|
KT: Type[Key],
|
||||||
VT: Type[Value],
|
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]:
|
) -> dict[Key, Value]:
|
||||||
assert isinstance(value, dict)
|
assert isinstance(value, dict)
|
||||||
value2 = cast(dict[Any, Any], value)
|
value2 = cast(dict[Any, Any], value)
|
||||||
|
|
||||||
assert all([
|
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()
|
for k, v in value2.items()
|
||||||
])
|
])
|
||||||
return cast(
|
|
||||||
dict[Key, Value],
|
if VT is None:
|
||||||
value,
|
return cast(
|
||||||
)
|
dict[Key, Any],
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return cast(
|
||||||
|
dict[Key, Value],
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
|
||||||
def pyproject_load(
|
def pyproject_load(
|
||||||
d: pathlib.Path,
|
d: pathlib.Path,
|
||||||
@ -97,9 +121,21 @@ def pyproject_load(
|
|||||||
dict
|
dict
|
||||||
)
|
)
|
||||||
|
|
||||||
for k, v in content['project']['optional-dependencies'].items():
|
for k, v in check_dict(
|
||||||
assert isinstance(v, list)
|
check_dict(
|
||||||
assert isinstance(k, str)
|
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
|
dependencies[k] = v
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user