| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
import typing_extensions |
| 5 |
from typing import Any, Type, Union, Literal, Optional |
| 6 |
from datetime import date, datetime |
| 7 |
from typing_extensions import get_args as _get_args, get_origin as _get_origin |
| 8 |
|
| 9 |
from .._types import StrBytesIntFloat |
| 10 |
from ._datetime_parse import parse_date as _parse_date, parse_datetime as _parse_datetime |
| 11 |
|
| 12 |
_LITERAL_TYPES = {Literal, typing_extensions.Literal} |
| 13 |
|
| 14 |
|
| 15 |
def get_args(tp: type[Any]) -> tuple[Any, ...]: |
| 16 |
return _get_args(tp) |
| 17 |
|
| 18 |
|
| 19 |
def get_origin(tp: type[Any]) -> type[Any] | None: |
| 20 |
return _get_origin(tp) |
| 21 |
|
| 22 |
|
| 23 |
def is_union(tp: Optional[Type[Any]]) -> bool: |
| 24 |
if sys.version_info < (3, 10): |
| 25 |
return tp is Union # type: ignore[comparison-overlap] |
| 26 |
else: |
| 27 |
import types |
| 28 |
|
| 29 |
return tp is Union or tp is types.UnionType # type: ignore[comparison-overlap] |
| 30 |
|
| 31 |
|
| 32 |
def is_literal_type(tp: Type[Any]) -> bool: |
| 33 |
return get_origin(tp) in _LITERAL_TYPES |
| 34 |
|
| 35 |
|
| 36 |
def parse_date(value: Union[date, StrBytesIntFloat]) -> date: |
| 37 |
return _parse_date(value) |
| 38 |
|
| 39 |
|
| 40 |
def parse_datetime(value: Union[datetime, StrBytesIntFloat]) -> datetime: |
| 41 |
return _parse_datetime(value) |