Created
November 13, 2025 20:09
-
-
Save mypy-play/f2d445083766682fbba090c68a0d8807 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # mypy: disable-error-code=empty-body | |
| from typing import Protocol, Any | |
| from typing_extensions import Never | |
| from collections.abc import Callable | |
| class Factory(Protocol): | |
| def __call__(self, cls: type[Any], /, **kwargs: Any) -> Never: ... | |
| class Shape(Protocol): | |
| def area(self) -> float: ... | |
| def perimeter(self) -> float: ... | |
| def kw_only_callable[F: Callable[..., object]](f: F, /) -> F | Factory: ... | |
| @kw_only_callable | |
| 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 = create_shape(Circle, radius=1) | |
| shape2 = create_shape(Square, side=2) | |
| # 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