Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created January 25, 2026 15:21
Show Gist options
  • Select an option

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

Select an option

Save mypy-play/de70629731c8a0a65af9fc43009d0510 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
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