Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give parsing abilities to datetime types (#234)
Closes #86.
- Loading branch information
1 parent
1eee5f3
commit 43c9ff5
Showing
18 changed files
with
381 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
from typing import Callable | ||
from typing import Iterable | ||
from typing import Sequence | ||
from typing import TypeVar | ||
from typing import cast | ||
|
||
from typing_extensions import Final | ||
from typing_extensions import get_args | ||
|
||
from ._utils.misc import is_union | ||
from .errors import BoundError | ||
from .predicates.boolean import all_of | ||
from .predicates.generic import of_complex_type | ||
from .predicates.generic import of_type | ||
|
||
__all__ = ("get_bound_parser", "parse_str") | ||
|
||
T = TypeVar("T", covariant=True) | ||
|
||
|
||
def display_bound(bound: Any) -> str: | ||
if isinstance(bound, Iterable): | ||
return f"Intersection[{', '.join(display_bound(part) for part in bound)}]" | ||
if is_union(bound): | ||
return ( | ||
f"typing.Union[" | ||
f"{', '.join(display_bound(part) for part in get_args(bound))}" | ||
f"]" | ||
) | ||
return str(getattr(bound, "__name__", bound)) | ||
|
||
|
||
def get_bound_parser(bound: Any) -> Callable[[object], T]: | ||
within_bound = ( | ||
# Interpret sequence as intersection | ||
all_of(of_type(t) for t in bound) | ||
if isinstance(bound, Sequence) | ||
else of_complex_type(bound) | ||
) | ||
|
||
def parser(instance: object) -> T: | ||
if not within_bound(instance): | ||
raise BoundError( | ||
f"Value is not within bound of {display_bound(bound)!r}: {instance!r}" | ||
) | ||
return cast(T, instance) | ||
|
||
return parser | ||
|
||
|
||
parse_str: Final[Callable[[object], str]] = get_bound_parser(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class BoundError(TypeError): | ||
... | ||
|
||
|
||
class MissingDependency(Exception): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.