-
|
I know about I think the So I see that Raw Code: from collections.abc import Awaitable
import functools
from typing import Any, Callable, ParamSpec, TypeVar, overload
T = TypeVar("T")
P = ParamSpec("P")
WrappedFn = TypeVar("WrappedFn", bound=Callable[..., Any])
def decorator(fn: Callable[P, T]) -> Callable[P, T]:
def wrapped(*args: P.args, **kwargs: P.kwargs) -> T:
return fn(*args, **kwargs)
return wrapped
def decorator_with_type_var(fn: WrappedFn) -> WrappedFn:
# The error become even more cursed if you add @functools.wraps(fn) here
def wrapped(*args: Any, **kwargs: Any) -> Any:
return fn(*args, **kwargs)
return wrapped
# ^^^^^^^
# Type "(...) -> Any" is not assignable to return type "WrappedFn@decorator_with_type_var"
# Type "(...) -> Any" is not assignable to type "WrappedFn@decorator_with_type_var" (reportReturnType)
@overload
def f(x: int) -> int:
"""Docstring for f"""
@overload
def f(x: str) -> str:
"""Docstring for f"""
def f(x: int | str) -> int | str:
"""Docstring for f"""
return x
g1 = decorator(f) # Has overload information, no docstring
g2 = decorator_with_type_var(f) # Has overload information and docstring |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Pyright preserves the docstring of the decorated function if it's not overloaded. It should do the same in the case that it's overloaded. That appears to be a bug. Could you please file a bug report to that effect so it is tracked? Thanks! The diagnostic you're seeing here also appears to be a bug. The type |
Beta Was this translation helpful? Give feedback.
Pyright preserves the docstring of the decorated function if it's not overloaded. It should do the same in the case that it's overloaded. That appears to be a bug. Could you please file a bug report to that effect so it is tracked? Thanks!
The diagnostic you're seeing here also appears to be a bug. The type
(...) -> Anyis the gradual form of a callable. It's the equivalent ofAny, and it should be assignable to any callable (and vice versa), sowrappedshould be assignable to the return type. Please file a separate bug for that issue. You can use acastcall or a# pyright: ignorecomment to work around this issue in the meantime.