-
Code sample in pyright playground from typing import Any, Callable, TypeVar
WrappedFn = TypeVar("WrappedFn", bound=Callable[..., Any])
def foo(func: WrappedFn) -> WrappedFn:
return func
reveal_type(foo)
bar: Callable[[WrappedFn], WrappedFn] = foo
reveal_type(bar)
Why does |
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Feb 6, 2025
Replies: 1 comment 1 reply
-
For context, this is my use case: from collections.abc import Callable
from tenacity import WrappedFn, retry, stop_after_attempt
retry_policy: Callable[[WrappedFn], WrappedFn] = retry(stop=stop_after_attempt(3))
@retry_policy
def foo() -> None:
print("foo!")
raise Exception
foo() |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This behavior is mandated by the Python typing spec. A type variable is not valid unless it is associated with a generic class, function, or type alias declaration. You're using the type variable here for a variable declaration, which isn't a valid context for a type variable.
Historically, type variable scoping in Python has been very confusing for users, so you're not alone here. PEP 695 (which was introduced in Python 3.12) modernizes the syntax and eliminates most of the confusion around scoping because the scope is explicit with the new syntax.