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

[Feature Request] @beartype + functools.partial support #373

Open
leycec opened this issue Apr 23, 2024 · 1 comment
Open

[Feature Request] @beartype + functools.partial support #373

leycec opened this issue Apr 23, 2024 · 1 comment

Comments

@leycec
Copy link
Member

leycec commented Apr 23, 2024

This feature request comes courtesy @danielward27 at issue #368, who astutely noted that the @beartype decorator currently fails to support functools.partial objects. Notably, this questionable minimal example coercing the functools.partial class into a decorator:

from beartype import beartype
from functools import partial

@beartype
@partial(partial, b=2)  # <-- WTH!?!? no way that actually works. no way...
def fn(a, b=2):
    return a + b

fn(1)  # <-- OMG!!!! @partial(partial, b=2) totally works. Mind. Is. Blown.

...currently raises this mostly unreadable exception:

AssertionError: <method-wrapper '__call__' of functools.partial object at
0x7f70b1957a60> not builtin bound method descriptor.

Just thinking about supporting this makes @leycec vomit all over himself. 🤮

@danielward27
Copy link

FWIW I promise I don't actually write code like the above example.

A pattern that is used a lot in jax is something like

@beartype
@partial(jax.jit, static_argnames=["length"])
def fn(a, length=2):
    return a + jnp.ones(length)
fn(1)

i.e. to partially apply arguments to a decorator before wrapping. My double partial was a lazy proxy for that (since jit leads to an error anyway), but perhaps I chose a bad proxy, since this seems to work

from beartype import beartype
from functools import partial

def add_decorator(fn, add):
    return lambda *args, **kwargs: fn(*args, **kwargs) + add

@beartype
@partial(add_decorator, add=5)
def fn(a):
    return a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants