Generate mlx_graph_schema.py from parsed FBS.
(schema: FBSSchema)
| 402 | |
| 403 | |
| 404 | def generate_python_schema(schema: FBSSchema) -> str: # noqa: C901 |
| 405 | """Generate mlx_graph_schema.py from parsed FBS.""" |
| 406 | lines = _file_header("#") |
| 407 | lines.extend( |
| 408 | [ |
| 409 | "", |
| 410 | "from __future__ import annotations", |
| 411 | "", |
| 412 | "from dataclasses import dataclass, field", |
| 413 | "from enum import IntEnum", |
| 414 | "from typing import List, Optional, Union", |
| 415 | "", |
| 416 | "", |
| 417 | *_section_header("#", "Enums"), |
| 418 | ] |
| 419 | ) |
| 420 | |
| 421 | # Generate enums |
| 422 | for enum in schema.enums: |
| 423 | lines.append(f"class {enum.name}(IntEnum):") |
| 424 | val = 0 |
| 425 | for name, explicit_val in enum.values: |
| 426 | if explicit_val is not None: |
| 427 | val = explicit_val |
| 428 | lines.append(f" {name} = {val}") |
| 429 | val += 1 |
| 430 | lines.append("") |
| 431 | lines.append("") |
| 432 | |
| 433 | lines.extend(_section_header("#", "Core types")) |
| 434 | |
| 435 | # Generate structs (Tid, Vid) |
| 436 | for struct in schema.structs: |
| 437 | lines.append("@dataclass") |
| 438 | lines.append(f"class {struct.name}:") |
| 439 | for fld in struct.fields: |
| 440 | py_type = _fbs_type_to_python(fld.type_str, fld.required) |
| 441 | default = _fbs_default_to_python(fld.default, fld.type_str) |
| 442 | if default: |
| 443 | lines.append(f" {fld.name}: {py_type} = {default}") |
| 444 | else: |
| 445 | lines.append(f" {fld.name}: {py_type}") |
| 446 | lines.append("") |
| 447 | lines.append("") |
| 448 | |
| 449 | # Generate compound types (IntOrVid, FloatOrVid, TidOrVid) from schema |
| 450 | for type_name in sorted(FBS_COMPOUND_TYPES): |
| 451 | table = next((t for t in schema.tables if t.name == type_name), None) |
| 452 | if table: |
| 453 | lines.extend(_generate_compound_type(table)) |
| 454 | lines.append("") |
| 455 | |
| 456 | # Generate ShapeDim, SlotVariant, NamedSlot, TensorMeta (but not Instruction/MLXGraph yet - they reference OpNode) |
| 457 | other_tables = ["ShapeDim", "SlotVariant", "NamedSlot", "TensorMeta"] |
| 458 | for table_name in other_tables: |
| 459 | table = next((t for t in schema.tables if t.name == table_name), None) |
| 460 | if table: |
| 461 | lines.extend(_generate_dataclass(table)) |
nothing calls this directly
no test coverage detected