{"record":{"id":"f75a683d66bd02e5","repo":"sgl-project/sglang","slug":"cls-name-f-name-expected-expected-name","errorCode":null,"errorMessage":"{cls_name}.{f.name}: expected {expected.__name__}, got {type(value).__name__}","messagePattern":"(.+?)\\.(.+?): expected (.+?), got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/debug_utils/dumper.py","lineNumber":45,"sourceCode":"\n# -------------------------------------- config base ------------------------------------------\n\n\n@dataclass(frozen=True)\nclass _BaseConfig(ABC):\n    def __post_init__(self) -> None:\n        self._verify_types()\n\n    def _verify_types(self) -> None:\n        hints = get_type_hints(type(self))\n        cls_name = type(self).__name__\n        for f in fields(self):\n            value = getattr(self, f.name)\n            if value is None:\n                continue\n            expected = self._unwrap_type(hints[f.name])\n            if not isinstance(value, expected):\n                raise TypeError(\n                    f\"{cls_name}.{f.name}: expected {expected.__name__}, \"\n                    f\"got {type(value).__name__}\"\n                )\n\n    @classmethod\n    @abstractmethod\n    def _env_prefix(cls) -> str: ...\n\n    @classmethod\n    def _env_name(cls, field_name: str) -> str:\n        return f\"{cls._env_prefix()}{field_name.upper()}\"\n\n    @classmethod\n    def from_env(cls) -> \"_BaseConfig\":\n        return cls(\n            **{\n                f.name: cls._parse_env_field(cls._env_name(f.name), f.default)\n                for f in fields(cls)","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/debug_utils/dumper.py#L27-L63","documentation":"_verify_types (run from a dataclass's __post_init__) raises this TypeError when an Optional field's non-None value's runtime type doesn't match the declared (unwrapped) type hint. It enforces strict runtime type checking on construction of the dumper config/record classes.","triggerScenarios":"Constructing the dataclass with a wrong-typed value, e.g. a field typed Optional[int] receiving a string '3', or Optional[list[str]] receiving a tuple; None values are skipped.","commonSituations":"Loading config from JSON/YAML where numbers arrive as strings, passing Path vs str interchangeably, or CLI parsing that yields strings for numeric fields.","solutions":["Coerce values to the declared types before constructing (int(...), str(...), list(...), Path(...))","If loading from JSON, add a normalization step for numeric/boolean fields","If the stricter contract is wrong for your use, change the field's type hint rather than bypassing the check"],"exampleFix":"# before\ncfg = DumpConfig(top_k=\"5\", out=Path('d'))\n# after\ncfg = DumpConfig(top_k=5, out=Path('d'))","handlingStrategy":"type-guard","validationCode":"from dataclasses import fields, get_type_hints\n\ndef coerce(cfg_cls, **kwargs):\n    hints = get_type_hints(cfg_cls)\n    out = {}\n    for f in fields(cfg_cls):\n        v = kwargs.get(f.name)\n        t = hints[f.name]\n        if v is not None and not isinstance(v, t):\n            v = t(v)  # naive coercion for primitives/Path/list\n        out[f.name] = v\n    return cfg_cls(**out)","typeGuard":"def is_optional_type(value, tp) -> bool:\n    return value is None or isinstance(value, tp)","tryCatchPattern":"try:\n    cfg = DumpConfig(**raw)\nexcept TypeError as e:\n    print(e); raise  # fix the offending field named in the message","preventionTips":["Coerce JSON/YAML-loaded scalars (int(...), bool(...)) before constructing","Run mypy on config-building code to catch mismatches statically"],"tags":["type-check","dataclass","runtime-validation","config"],"backgroundTag":"type-validation-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}