GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Supporting `Self` for Free Functions? · python/typing · Discussion #2294

Supporting Self for Free Functions? #2294

Answered by RBerga06
Natuworkguy asked this question in Q&A
Supporting `Self` for Free Functions? #2294
@Natuworkguy Natuworkguy
May 23, 2026 · 4 comments
Answered by RBerga06 Return to top
Discussion options
  • Uh oh!

    There was an error while loading. Please reload this page.

Natuworkguy
May 23, 2026

Currently, typing.Self only works in class contexts.

I noticed that typing a function that returns itself requires recursive callable aliases, for example:

from typing import TypeAlias
from collections.abc import Callable

SelfFunc: TypeAlias = Callable[..., "SelfFunc"]

def f() -> SelfFunc:
    return f

Would it make sense to allow:

from typing import Self

def f() -> Self:
    return f

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 Self beyond classes/methods.

Are there reasons this should or should not be supported?

You must be logged in to vote
All reactions
Answered by RBerga06 May 23, 2026

Would a callback protocol do the trick?

from typing import Protocol, Self

class F(Protocol):
    def __call__(self, /) -> Self: ...

def factory() -> F:
    def f(): return f
    return f
View full answer

Replies: 4 comments

Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

JelleZijlstra
May 23, 2026
Maintainer

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?

You must be logged in to vote
All reactions
0 replies
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

Natuworkguy
May 23, 2026
Author

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 -> Self directly communicates intent.

Potential use cases I thought of were:

  • self-returning decorators/callback systems
  • recursive functional APIs
  • parser combinators or REPL-style command loops

Though I agree these may be too niche to justify special handling in the type system.

You must be logged in to vote
All reactions
0 replies
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

  • Uh oh!

    There was an error while loading. Please reload this page.

RBerga06
May 23, 2026

Would a callback protocol do the trick?

from typing import Protocol, Self

class F(Protocol):
    def __call__(self, /) -> Self: ...

def factory() -> F:
    def f(): return f
    return f
You must be logged in to vote
All reactions
  • 👍 1
0 replies
Answer selected by Natuworkguy
Comment options
  • Uh oh!

    There was an error while loading. Please reload this page.

Natuworkguy
May 23, 2026
Author

I didn’t think about using a protocol with Self like that.

What I was mainly thinking about was annotating the function itself, especially for nested/self-referential functions like:

def outer():
    def inner() -> ???:
        return inner

The protocol approach does work, but it feels a little verbose for something as simple as “this function returns itself”.

I do get the concern about adding another special case to the type system though, especially for something pretty niche.

You must be logged in to vote
All reactions
  • 👍 1
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
@Natuworkguy @JelleZijlstra @RBerga06
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.