Supporting Self for Free Functions?
#2294
Replies: 2 comments
-
|
This would require adding a special case to the type system. We generally only add special cases for things that are very common and useful, and I'm not sure "functions returning themselves" fall in that category. What is the use case? |
Beta Was this translation helpful? Give feedback.
-
|
That's fair. I don't think this is a critical missing feature, but I was wondering whether the readability and expressiveness benefits could justify it. Right now, recursive callable aliases work, but they feel indirect for the specific case of a function returning itself: from typing import TypeAlias
from collections.abc import Callable
SelfFunc: TypeAlias = Callable[..., "SelfFunc"]whereas Potential use cases I thought of were:
Though I agree these may be too niche to justify special handling in the type system. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently,
typing.Selfonly works in class contexts.I noticed that typing a function that returns itself requires recursive callable aliases, for example:
Would it make sense to allow:
for standalone functions?
This seems more readable and directly expresses intent, but I’m not sure whether there are ambiguity or implementation issues with extending
Selfbeyond classes/methods.Are there reasons this should or should not be supported?
Beta Was this translation helpful? Give feedback.
All reactions