seandstewart / typical

Typical: Fast, simple, & correct data-validation using Python 3 typing.
https://python-typical.org
MIT License
183 stars 9 forks source link

callable instance should be the treated as a function or class? #173

Closed xbanke closed 2 years ago

xbanke commented 2 years ago

Description

Hey, @seandstewart . By checking typic.protocals, I found that it check type of the obj by inspect.ismethod, inspect.isfunction and inspect.isclass. Personally, I think callable instace of a class with __call__ should be treated as a function, and the protocals should be redirected to the __call__.

A function decorated by a class decorator will be treated as class and the typic.al could not validate the arguments.

from functools import wraps
import typic

class Wrapped:
    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)

def wrapped(func):
    return wraps(func)(Wrapped(func))

@typic.al
@wrapped
def foo(a: int):
    return a

foo('42')  # '42'
foo('abc')  # 'abc'
seandstewart commented 2 years ago

@xbanke -

I will look into this for a patch release next. If you have a preferred implementation, please feel free to submit a PR 👍