RussBaz / enforce

Python 3.5+ runtime type checking for integration testing and data validation
543 stars 21 forks source link

Callables Not Handled Correctly #6

Closed TheDataLeek closed 8 years ago

TheDataLeek commented 8 years ago

Callables are not currently working, as referenced in #3.

Thinking about callable objects, I think the best way to handle them is as follows.

Let's say we have some function that takes another function as an argument with correct type hinting syntax.

def foo(func: Callable[[int, int], float], s: str) -> str:
    return s + str(func(5, 5))

I think that the best way to handle this is to also require that the argument passed in func needs to also have complete type annotation.

def func(x: int, y: int) -> float:
    return float(x * y)

With func possibly also checked at runtime depending on whether or not the enforce.runtime_validation decorator is applied. The only thing that the high level function foo checks is that the call signature of func matches the type hinting specified.

How's that sound?

RussBaz commented 8 years ago

Sounds good to me. Started working on it. However, can't do much or respond frequently due to certain personal issues.

TheDataLeek commented 8 years ago

No worries. I've been working on this for the last few days. I'll get back to you when I have something more concrete.

RussBaz commented 8 years ago

Callables should be working now. It expects a callable with annotations. If it is a safe callable (runtime validation was previously applied to it), it will only check if it has an expected signature. Otherwise, it has to be wrapped in a proxy object and runtime type safety checks must be applied after that. Such a transparent proxy is needed because we don't want to mutate an object implicitly.

If there are still some corner cases, please feel free to report - I will reopen the issue.