Created
August 30, 2026 13:21
-
-
Save mypy-play/0a566781bc7a2ecc08325211fdc956d3 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 typing import TypeVar, Literal, Generic, overload, reveal_type | |
| from typing import Any | |
| _ST = TypeVar("_ST") | |
| _GT = TypeVar("_GT") | |
| class Field(Generic[_ST, _GT]): | |
| def __init__(self, *, null: bool = False) -> None: | |
| super().__init__() | |
| def __get__(self, instance: object, owner: object) -> _GT: | |
| ... | |
| _ST_1 = TypeVar("_ST_1", default=int) | |
| _GT_1 = TypeVar("_GT_1", default=int) | |
| class IntegerField(Field[_ST_1, _GT_1]): | |
| @overload | |
| def __new__(cls, *, null: Literal[True]) -> "IntegerField[_ST_1 | None, _GT_1 | None]": ... | |
| @overload | |
| def __new__(cls, *, null: Literal[False] = False) -> "IntegerField[_ST_1, _GT_1]": ... | |
| def __new__(cls, *, null: bool = False) -> "IntegerField[Any, Any]": | |
| return super().__new__(cls) | |
| class Model: | |
| pass | |
| class A(Model): | |
| field1 = IntegerField(null=False) | |
| field2 = IntegerField(null=True) | |
| a = A() | |
| reveal_type(a.field1) # expect: int | |
| reveal_type(a.field2) # expect: int | None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment