Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-morel committed Jun 6, 2024
1 parent 883e5db commit 8938a7b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
Expand Down
18 changes: 11 additions & 7 deletions src/pyoload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
from typing import Any
from typing import Callable
from typing import GenericAlias
from typing import Type
from typing import Union

try:
from types import NoneType
except ImportError:
NoneType = type(None)
NoneType = type(None)


class AnnotationError(ValueError):
Expand Down Expand Up @@ -98,6 +94,7 @@ def get_name(funcOrCls: Any) -> str:
:returns: modulename + qualname
"""
while not isinstance(funcOrCls, type) and type(funcOrCls)
return funcOrCls.__module__ + "." + funcOrCls.__qualname__


Expand Down Expand Up @@ -159,7 +156,10 @@ def check(cls: Any, name: str, params: Any, val: Any) -> None:
check = cls.checks_list.get(name)
if check is None:
raise Check.CheckDoesNotExistError(name)
check(params, val)
try:
check(params, val)
except (AssertionError, TypeError) as e:
raise Check.CheckError from e

class CheckNameAlreadyExistsError(ValueError):
"""
Expand Down Expand Up @@ -191,6 +191,8 @@ def len_check(params, val):
if ma is not None:
if not len(val) < ma:
raise Check.CheckError(f"length of {val!r} not lt {mi!r}")
else:
raise Check.CheckError(f'wrong {params=!r} for len')


@Check.register("lt")
Expand Down Expand Up @@ -352,7 +354,9 @@ def cast(val: Any, totype: Any) -> Any:
kt, vt = args
elif len(args) == 1:
kt, vt = args[0], Any
return {Cast.cast(k, kt): Cast.cast(v, vt) for k, v in val.items()}
return {
Cast.cast(k, kt): Cast.cast(v, vt) for k, v in val.items()
}
else:
sub = args[0]
return get_origin(totype)([Cast.cast(v, sub) for v in val])
Expand Down
2 changes: 1 addition & 1 deletion src/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
# "Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand Down
1 change: 1 addition & 0 deletions src/test-cov.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest --cov --cov-report term-missing:skip-covered
14 changes: 7 additions & 7 deletions src/tests/logs.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
typeMatch vs isinstance on int:True 0.26837916ms
typeMatch vs isinstance on int:False 0.24146669ms
typeMatch on dict[str, int]*50:True 29.45430688ms
typeMatch on dict[str, int]*50:False 5.970363880000001ms
Cast str->int: 2.92456138ms
Cast complex->int | str: 0.12061774399999999ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 2.7908898460000002ms
typeMatch vs isinstance on int:True 1.2414197ms
typeMatch vs isinstance on int:False 2.8583822ms
typeMatch on dict[str, int]*50:True 208.6217113ms
typeMatch on dict[str, int]*50:False 16.4641389ms
Cast str->int: 7.2549127ms
Cast complex->int | str: 0.27592566ms
Cast dict[int,list[str]*10]*10->dict[str,tuple[float]]: 9.29763984ms
89 changes: 89 additions & 0 deletions src/tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import pyoload


assert pyoload.__version__ == '2.0.0'


errors = (
pyoload.AnnotationError,
pyoload.AnnotationErrors,
pyoload.InternalAnnotationError,
pyoload.CastingError,
pyoload.OverloadError,
pyoload.AnnotationResolutionError,
)


def test_errors():
for err in errors:
try:
raise err()
except err:
pass
except Exception:
raise
else:
raise Exception()

pyoload.PyoloadAnnotation()
v = pyoload.Values((1, 2, 3))
v(2)
str(v)

assert pyoload.get_name(test_errors).endswith('test_errors')

class Custom(pyoload.Check):
def __call__(self, a, b):
assert a == b

@pyoload.annotate
def foo(a: pyoload.Checks(Custom=3)):
pass

try:
foo(2)
except pyoload.AnnotationError:
foo(3)
else:
raise Exception()

try:
pyoload.Check.register('Custom')
except pyoload.Check.CheckNameAlreadyExistsError:
pass
else:
raise Exception()

@pyoload.Check.register('c2 c3')
def _(a, b):
assert a == b

@pyoload.annotate
def foo(a: pyoload.Checks(c2=3, c3=3)):
pass

try:
foo(2)
except pyoload.AnnotationErrors:
foo(3)
else:
raise Exception()

try:
@pyoload.annotate
def bar(c: pyoload.Checks(rub=3)):
raise Exception(c)

bar(NotImplemented)
except pyoload.Check.CheckDoesNotExistError:
pass

for name, check in pyoload.Check.checks_list.items():
try:
if pyoload.get_name(check).split('.')[0] == 'tests':
continue
pyoload.Checks(**{name: NotImplemented})(24)
except pyoload.Check.CheckError:
pass
else:
raise Exception(name, check)
2 changes: 1 addition & 1 deletion src/tests/test_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from time import perf_counter_ns as nanos


N = 100000
N = 10000
NS = 10
src = Path(__file__).resolve().parent

Expand Down

0 comments on commit 8938a7b

Please sign in to comment.