Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #29

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ a typechecker function which typechecks the passed value on each assignment.
It also calls annotate on each of it's methods, except the class has a
`__annotate_norecur__` attribute.

But if the assigned attribute does not yet have annotations, it gets them using
`type(val)` and adds them to the annotations.
Newer from version 1.1.3, pyoload ignores attributes with no annotations and does not check
them.

```python
from pyoload import *
Expand Down Expand Up @@ -76,8 +76,8 @@ And on each call it simply loops through each function entry, while
it catches a `pyoload.InternalAnnotationError` which is raised when
the special `is_overload` is set to true

> tip
you may raise `pyoload.InternalAnnotationError` inside an overloaded
> [!TIP]
> you may raise `pyoload.InternalAnnotationError` inside an overloaded
function after carrying out some other checks and pyoload will switch to the
next oveload.

Expand All @@ -95,6 +95,11 @@ def foo_hello(d: dict[str, list[int]]):
...
```

> [!TIP]
> you may raise `pyoload.InternalAnnotationError` inside an overloaded
function after carrying out some other checks and pyoload will switch to the
next oveload.

## type checking with `pyoload.typeMatch(val, type)`

this function simply finds type compatibility between the passes arguments
Expand All @@ -121,7 +126,7 @@ def foo(a: str):
foo(3.5) # '3.5'
```

### casting recursiion
### casting recursion

Using recursion it supports Generic Aliases of `dict` and builtin iterable
types as `list` and `tuple`.
Expand Down
18 changes: 10 additions & 8 deletions src/pyoload/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ class Checks(PyoloadAnnotation):
Pyoload annotation holding several checks
"""

def __init__(self: PyoloadAnnotation, **checks):
def __init__(
self: PyoloadAnnotation,
**checks: dict[str, Callable[Any, Any]]
):
"""def __init__(self: PyoloadAnnotation, **checks)
crates the check object,e.g

Expand Down Expand Up @@ -364,7 +367,7 @@ def __set__(self: Any, obj: Any, value: Any):
self.value = self(value)


def typeMatch(val: Any, spec: type) -> bool:
def typeMatch(val: Any, spec: Any) -> bool:
"""
recursively checks if type matches
:param val: The value to typecheck
Expand Down Expand Up @@ -535,7 +538,7 @@ def wrapper(*args, **kw):
__overloads__: dict[str, list[Callable]] = {}


def overload(func: callable, name: str | None = None):
def overload(func: Callable, name: str | None = None):
"""
returns a wrapper over the passed function
which typechecks arguments on each call
Expand All @@ -554,7 +557,7 @@ def overload(func: callable, name: str | None = None):
__overloads__[name] = []
__overloads__[name].append(annotate(func, True))

@wraps(func)
@wraps(func: Callable)
def wrapper(*args, **kw):
for f in __overloads__[name]:
try:
Expand All @@ -577,7 +580,7 @@ def wrapper(*args, **kw):
return wrapper


def annotateClass(cls):
def annotateClass(cls: Any):
"""
Annotates a class object, wrapping and replacing over it's __setattr__
and typechecking over each attribute assignment.
Expand Down Expand Up @@ -611,8 +614,7 @@ def new_setter(self: Any, name: str, value: Any) -> Any:
resolveAnnotations(self)

if name not in self.__annotations__:
if value is not None:
self.__annotations__[name] = type(value)
return setter(self, name, value) # do not check if no annotations
elif isinstance(self.__annotations__[name], Cast):
return setter(self, name, self.__annotations__[name](value))
elif not typeMatch(value, self.__annotations__[name]):
Expand All @@ -626,5 +628,5 @@ def new_setter(self: Any, name: str, value: Any) -> Any:
return cls


__version__ = '1.1.2'
__version__ = '1.1.3'
__author__ = 'ken-morel'
Loading