def check_type(value: typing.Any, hint: typing.Optional[type]) -> bool:
"""Check given ``value``'s type.
:param value: given argument
:param hint: expected type of given ``value``.
as like :mod:`typing` interprets, :const:`None` is interpreted
as :class:`types.NoneType`
:type hint: :class:`typing.Optional`[:class:`type`]
"""
if hint is None:
hint = NoneType
actual_type = type(value)
if hint is NoneType:
correct = value is None
elif hint is typing.Any:
correct = True
elif hint is typing.Pattern or hint is typing.Match:
correct = isinstance(value, hint.impl_type)
elif isinstance(hint, typing.TypeVar):
# TODO: Check generic
correct = True
elif issubclass(hint, typing.Callable):
actual_type, correct = check_callable(value, hint)
elif issubclass(hint, typing.Tuple):
actual_type, correct = check_tuple(value, hint)
elif issubclass(hint, typing.Union):
actual_type, correct = check_union(value, hint)
else:
correct = isinstance(value, hint)
return actual_type, correct
评论列表
文章目录