-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add classparse impl * clean up imports * limit forbiddenfruit python version * fix typing * add comment * remove types.FunctionType * update readme * fix linting * fix typing
- Loading branch information
1 parent
74a366c
commit 8a7e46e
Showing
8 changed files
with
337 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# MIT License | ||
|
||
# Copyright (c) 2022 Lunarmagpie | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from __future__ import annotations | ||
import abc | ||
import typing | ||
import sys | ||
|
||
from sigparse._pep604 import PEP604_CTX | ||
|
||
LOCALNS: dict[str, typing.Any] = { | ||
"list": typing.List, | ||
"type": typing.Type, | ||
"dict": typing.Dict, | ||
"tuple": typing.Tuple, | ||
} | ||
|
||
|
||
IN = typing.TypeVar("IN") | ||
OUT = typing.TypeVar("OUT") | ||
|
||
|
||
class Applicator(abc.ABC, typing.Generic[IN, OUT]): | ||
def __init__(self, obj: IN) -> None: | ||
if sys.version_info >= (3, 10): | ||
self.return_value = self.gt_or_eq_310(obj) | ||
|
||
elif sys.version_info >= (3, 9): | ||
with PEP604_CTX(): | ||
self.return_value = self.eq_309(obj) | ||
|
||
else: | ||
with PEP604_CTX(): | ||
self.return_value = self.lt_or_eq_308(obj, LOCALNS) | ||
|
||
def __call__(self) -> OUT: | ||
return self.return_value | ||
|
||
@abc.abstractmethod | ||
def gt_or_eq_310(self, obj: IN) -> OUT: | ||
... | ||
|
||
@abc.abstractmethod | ||
def eq_309(self, obj: IN) -> OUT: | ||
... | ||
|
||
@abc.abstractmethod | ||
def lt_or_eq_308(self, obj: IN, localns: dict[str, type]) -> OUT: | ||
... |
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,48 @@ | ||
# MIT License | ||
|
||
# Copyright (c) 2022 Lunarmagpie | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from __future__ import annotations | ||
|
||
|
||
import typing | ||
|
||
from sigparse._applicator import Applicator | ||
|
||
|
||
class Classparse(Applicator[type, "dict[str, type]"]): | ||
@typing.no_type_check | ||
def gt_or_eq_310(self, func: typing.Any) -> dict[str, type]: | ||
return typing.get_type_hints(func, include_extras=True) | ||
|
||
@typing.no_type_check | ||
def eq_309(self, func: typing.Any) -> dict[str, type]: | ||
return typing.get_type_hints(func, include_extras=True) | ||
|
||
@typing.no_type_check | ||
def lt_or_eq_308( | ||
self, func: typing.Any, localns: dict[str, type] | ||
) -> dict[str, type]: | ||
return typing.get_type_hints(func, localns=localns) | ||
|
||
|
||
def classparse(cls: type) -> dict[str, type]: | ||
return Classparse(cls)() |
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,73 @@ | ||
# MIT License | ||
|
||
# Copyright (c) 2022 Endercheif | ||
|
||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
|
||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
|
||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
|
||
import typing | ||
import sys | ||
|
||
if sys.version_info < (3, 10): | ||
import forbiddenfruit # type: ignore | ||
|
||
|
||
__all__: typing.Sequence[str] = ("PEP604_CTX",) | ||
|
||
|
||
def _apply_PEP604() -> None: | ||
""" | ||
Allow writing union types as X | Y | ||
""" | ||
|
||
if sys.version_info >= (3, 10): | ||
return | ||
|
||
def _union_or(left: typing.Any, right: typing.Any) -> typing.Any: | ||
return typing.Union[left, right] | ||
|
||
setattr(typing._GenericAlias, "__or__", _union_or) # type: ignore | ||
setattr(typing._GenericAlias, "__ror__", _union_or) # type: ignore | ||
|
||
forbiddenfruit.curse(type, "__or__", _union_or) | ||
|
||
|
||
def _revert_PEP604() -> None: | ||
if sys.version_info >= (3, 10): | ||
return | ||
|
||
forbiddenfruit.reverse(type, "__or__") | ||
|
||
|
||
GLOBAL_PEP604 = False | ||
|
||
|
||
def global_PEP604() -> None: | ||
global GLOBAL_PEP604 | ||
GLOBAL_PEP604 = True | ||
_apply_PEP604() | ||
|
||
|
||
class PEP604_CTX: | ||
def __enter__(self) -> None: | ||
_apply_PEP604() | ||
|
||
def __exit__(self, *_: typing.Any) -> None: | ||
if not GLOBAL_PEP604: | ||
_revert_PEP604() |
Oops, something went wrong.