From d331fdd2baace7a4ffe15fdddc00f4895db5a6da Mon Sep 17 00:00:00 2001 From: Lianmin Zheng Date: Sat, 20 Jun 2026 18:43:16 -0700 Subject: [PATCH] Add project rule: prefer msgspec.Struct over dataclasses (#28816) --- .claude/rules/no-dataclasses.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .claude/rules/no-dataclasses.md diff --git a/.claude/rules/no-dataclasses.md b/.claude/rules/no-dataclasses.md new file mode 100644 index 000000000..f29c7bfb9 --- /dev/null +++ b/.claude/rules/no-dataclasses.md @@ -0,0 +1,24 @@ +--- +paths: + - "**/*.py" +--- + +# Use `msgspec.Struct`, not `@dataclass` + +Define data containers as `msgspec.Struct`. Do not add new +`dataclasses.dataclass` (or `attrs`) — they weaken strict type checking and +don't map onto Rust structs for the planned Rust migration. + +```python +import msgspec + +class LoadSnapshot(msgspec.Struct): # frozen=, kw_only=, omit_defaults= as needed + dp_rank: int = 0 + tokens: list[int] = [] # mutable defaults are safe +``` + +- Methods / `@classmethod` constructors go on the `Struct`; see + `python/sglang/srt/managers/load_snapshot.py`. +- New code only. Existing `@dataclass` is grandfathered — migrate opportunistically + while editing the file, not in drive-by sweeps. +- If a third-party API forces `@dataclass`, keep it at that boundary only.