Created
April 30, 2025 18:01
-
-
Save mypy-play/1f9176a8c2bef850b4e9a5d9af2e8211 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
from __future__ import annotations | |
import typing as t | |
_ST = t.TypeVar("_ST") | |
_GT = t.TypeVar("_GT") | |
class Field(t.Generic[_ST, _GT]): | |
def __init__(self, *, null: bool = False) -> None: | |
super().__init__() | |
_ST_CharField = t.TypeVar("_ST_CharField", default=str | int) | |
_GT_CharField = t.TypeVar("_GT_CharField", default=str) | |
class CharField(Field[_ST_CharField, _GT_CharField]): | |
@t.overload | |
def __new__( | |
cls, *, null: t.Literal[True] | |
) -> CharField[_ST_CharField | None, _GT_CharField | None]: ... | |
@t.overload | |
def __new__( | |
cls, *, null: t.Literal[False] = False | |
) -> CharField[_ST_CharField, _GT_CharField]: ... | |
def __new__( # type: ignore[misc] | |
cls, *, null: bool = False | |
) -> ( | |
CharField[_ST_CharField, _GT_CharField] | |
| CharField[_ST_CharField | None, _GT_CharField | None] | |
): | |
return super().__new__(cls) | |
x = CharField(null=False) | |
reveal_type(x) | |
y = CharField(null=True) | |
reveal_type(y) | |
z = CharField() | |
reveal_type(z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment