-
Notifications
You must be signed in to change notification settings - Fork 254
Cannot anotate type of generic functions. #1938
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
Comments
The class GenericCallable(Protocol):
def __call__[T](self, f: Callable[[bool], T]) -> T: ...
fx_2_alias: GenericCallable = fx_2 When the typing community was working on the design for PEP 695, we had some long and involved discussions about whether we wanted to extend the type system to support type variables scoped to a If this use case is sufficiently common to justify a less-verbose alternative to callback protocols, we could revisit this decision. If you want to drive that, I think you'd need to overcome the original obstacles that stopped us from adding this in the past:
|
One more note that may be of historical interest... There was an effort a few years back to create a more modern syntax for |
I think this issue should be moved from the |
Yes, good point. This should be moved to the |
The current scoping rules for type variables seems to deny type annotations over generic functions.
The issue
define two functions as follows:
If a function alias is required, these functions can be assigned to variables. However, if type annotations are used,
fx_2
cannot be annotated using compliant type checkers (i.e. pyright).Justification
def
is a syntactic sugar equivalent to a variable assignation over a lambda function. As a result, it should be possible to define generic functions withoutdef
statements e.g.e.g.
Even when using the same type var
fx_2_alias
andfx_2_alias_2
each var is an independent one.A more clear syntax (like the introduced on def statements on python312) could be
This marks explicitly that the scope of the var is over the type itself. However, this issue does not aim to propose a new syntax, but rather aims to adjust the scoping rules of type vars.
Motivation
This issue was derived from a pyright issue where generic functions as class instance variables are denied by the checker because of the same scope issues described before.
The text was updated successfully, but these errors were encountered: