This decorator returns a "Callable" not a "FunctionType" #770
-
Hi, If anyone can help me here to understand what is the issue that would be great :) The code in question that is giving error from __future__ import annotations
import random
from collections.abc import Callable
from functools import wraps
from typing import Concatenate, ParamSpec, TypeVar
class SomeClass:
"""Some random class."""
pass
P = ParamSpec("P")
T = TypeVar("T", bound=SomeClass)
R = bool | None
def validate(func: Callable[Concatenate[T, P], R]) -> Callable[Concatenate[T, P], R]:
"""Validate something."""
@wraps(func)
def inner(first: T, *args: P.args, **kwargs: P.kwargs) -> R:
# do some crazy evaluation.
ret = func(*args, **kwargs)
return ret
return inner
class NewClass(SomeClass):
@validate # This decorator returns a "Callable" not a "FunctionType"
def foo(self) -> bool:
"""Foo func."""
# doing some stuff
return random.choice([True, False]) The code that works, """Test validating decorators."""
from __future__ import annotations
import random
from collections.abc import Callable
from functools import wraps
from typing import ParamSpec
P = ParamSpec("P")
R = bool | None
def validate(func: Callable[P, R]) -> Callable[P, R]:
"""Validate something."""
@wraps(func)
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
# do some crazy evaluation.
ret = func(*args, **kwargs)
return ret
return inner
@validate
def foo() -> bool:
"""Foo func."""
# doing some stuff
return random.choice([True, False]) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
thaks so much for reaching out, and sorry that you have run into a confusing situation. tl;dr you are seeing #690 in this situation, basedmypy is warning you about an ambiguous situation regarding functions in classes: class Call:
def __call__(self, a: A):
pass
def decorate(c: Callable[[A], None]) -> Callable[[A], None]:
return Call()
class A:
@decorate
def foo(self):
pass
A().foo() # runtime error: Call.__call__() missing 1 required positional argument: 'a' the intent of this error, is that a non-function functions bind their first parameter, plain callables don't unfortunately, I can see that this issue is causing you confusion and problems, so I will prioritise the resolution |
Beta Was this translation helpful? Give feedback.
thaks so much for reaching out, and sorry that you have run into a confusing situation.
tl;dr you are seeing #690
in this situation, basedmypy is warning you about an ambiguous situation regarding functions in classes:
the intent of this error, is that a non-function
Callable
appears where a 'method' would normally be expected.functions bind their first parameter, plain callables don't
unfortunately,
functools…