Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created November 13, 2025 20:12
Show Gist options
  • Select an option

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

Select an option

Save mypy-play/2e1dcaf45c2d4670400f404f7ca0a74b to your computer and use it in GitHub Desktop.
Shared via mypy Playground
# mypy: disable-error-code=empty-body
from typing import Protocol, Any
from typing_extensions import Never
from collections.abc import Callable
class KWOnlyFactory(Protocol):
def __call__(self, cls: type[Any], /, **kwargs: Any) -> Never: ...
class Shape(Protocol):
def area(self) -> float: ...
def perimeter(self) -> float: ...
def kw_only_factory[F: Callable[..., object]](f: F, /) -> F | KWOnlyFactory: ...
@kw_only_factory
def create_shape[**P, R](kind: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
return kind(*args, **kwargs)
class Circle:
def __init__(self, radius: float): ...
def area(self) -> float: ...
def perimeter(self) -> float: ...
class Square:
def __init__(self, side: float): ...
def area(self) -> float: ...
def perimeter(self) -> float: ...
# OK
shape1 = reveal_type(create_shape(Circle, radius=1)) # Revealed type is "__main__.Circle"
shape2 = reveal_type(create_shape(Square, side=2)) # Revealed type is "__main__.Square"
# Error: Positional arguments not allowed
create_shape(Circle, 1)
create_shape(Square, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment