Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created January 25, 2026 13:00
Show Gist options
  • Select an option

  • Save mypy-play/25e56ee12277a770066deaedd66dea54 to your computer and use it in GitHub Desktop.

Select an option

Save mypy-play/25e56ee12277a770066deaedd66dea54 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from collections.abc import Callable
from typing import reveal_type
class Foo:
def __init__(self, size: str) -> None:
self.size = size
def create_big_foo() -> Foo:
return Foo('big')
def create_small_foo() -> Foo:
return Foo('small')
def create_method[**P](factory: Callable[P, Foo]):
def wrapper(self, *args: P.args, **kwargs: P.kwargs):
foo = factory(*args, **kwargs)
self.add_foo(foo)
return foo
return wrapper
class Bar:
def add_foo(self, foo: Foo) -> None:
pass
add_big_foo = create_method(create_big_foo)
add_small_foo = create_method(create_small_foo)
bar = Bar()
reveal_type(bar.add_big_foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment