Validate field metadata for a dataclass. Checks: - Tag IDs are unique (no duplicate IDs >= 0) - Optional[T] fields have nullable=True Args: cls: The dataclass type. field_metas: Dict mapping field name to ForyFieldMeta. type_hints: Dict mapping field na
(
cls: type,
field_metas: Dict[str, ForyFieldMeta],
type_hints: Dict[str, type],
)
| 228 | |
| 229 | |
| 230 | def validate_field_metas( |
| 231 | cls: type, |
| 232 | field_metas: Dict[str, ForyFieldMeta], |
| 233 | type_hints: Dict[str, type], |
| 234 | ) -> None: |
| 235 | """ |
| 236 | Validate field metadata for a dataclass. |
| 237 | |
| 238 | Checks: |
| 239 | - Tag IDs are unique (no duplicate IDs >= 0) |
| 240 | - Optional[T] fields have nullable=True |
| 241 | |
| 242 | Args: |
| 243 | cls: The dataclass type. |
| 244 | field_metas: Dict mapping field name to ForyFieldMeta. |
| 245 | type_hints: Dict mapping field name to type hint. |
| 246 | |
| 247 | Raises: |
| 248 | ValueError: If validation fails. |
| 249 | """ |
| 250 | from pyfory.type_util import is_optional_type |
| 251 | |
| 252 | # Check tag ID uniqueness |
| 253 | tag_ids_seen: Dict[int, str] = {} |
| 254 | for field_name, meta in field_metas.items(): |
| 255 | if meta.id >= 0: |
| 256 | if meta.id in tag_ids_seen: |
| 257 | raise ValueError( |
| 258 | f"Duplicate tag ID {meta.id} in class {cls.__name__}: fields '{tag_ids_seen[meta.id]}' and '{field_name}' have the same ID" |
| 259 | ) |
| 260 | tag_ids_seen[meta.id] = field_name |
| 261 | |
| 262 | # Check nullable consistency with Optional types |
| 263 | for field_name, meta in field_metas.items(): |
| 264 | if field_name not in type_hints: |
| 265 | continue |
| 266 | type_hint = type_hints[field_name] |
| 267 | if is_optional_type(type_hint) and not meta.nullable: |
| 268 | raise ValueError( |
| 269 | f"Field '{field_name}' in class {cls.__name__} is Optional[T] but nullable=False. Optional fields must have nullable=True." |
| 270 | ) |
no test coverage detected