Created
January 25, 2026 15:21
-
-
Save mypy-play/de70629731c8a0a65af9fc43009d0510 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 dataclasses import dataclass, field | |
| from typing import TYPE_CHECKING | |
| @dataclass | |
| class NormalDataClass(): | |
| a : str | |
| b : int | |
| c : int = field(init=False) | |
| NormalDataClass(a='', b=0, c=0) | |
| # Type checker shows for NormalDataClass(): | |
| # class NormalDataClass( | |
| # a: str, | |
| # b: int | |
| # ) | |
| if TYPE_CHECKING: | |
| custom_field = field | |
| else: | |
| def custom_field(*args, **kwargs): | |
| return field(*args, **kwargs, metadata={'example_metadata': 42}) | |
| @dataclass | |
| class CustomDataClass(): | |
| a : str | |
| b : int | |
| c : int = custom_field(init=False) | |
| CustomDataClass(a='', b=0, c=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment