Skip to content

Commit

Permalink
Add get_controller_schema function
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls committed Jan 24, 2025
1 parent cf891fe commit b0cae31
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/fastcs/launch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import json
from pathlib import Path
from typing import Annotated, TypeAlias, get_type_hints
from typing import Annotated, Any, TypeAlias, get_type_hints

import typer
from pydantic import BaseModel, create_model
Expand Down Expand Up @@ -205,3 +205,9 @@ def _extract_options_model(controller_class: type[Controller]) -> type[BaseModel
f".__init__' but received {len(args)} as `{sig}`"
)
return fastcs_options


def get_controller_schema(target: type[Controller]) -> dict[str, Any]:
options_model = _extract_options_model(target)
target_schema = options_model.model_json_schema()
return target_schema
9 changes: 8 additions & 1 deletion tests/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import pytest
from pydantic import create_model
from pytest_mock import MockerFixture
from ruamel.yaml import YAML
from typer.testing import CliRunner

from fastcs.__main__ import __version__
from fastcs.controller import Controller
from fastcs.exceptions import LaunchError
from fastcs.launch import TransportOptions, _launch, launch
from fastcs.launch import TransportOptions, _launch, get_controller_schema, launch


@dataclass
Expand Down Expand Up @@ -143,3 +144,9 @@ def test_launch_full(mocker: MockerFixture, data):
run.assert_called_once()
gui.assert_called_once()
docs.assert_called_once()


def test_get_schema(data):
ref_schema = YAML(typ="safe").load(data / "schema.json")
target_schema = get_controller_schema(IsHinted)
assert target_schema == ref_schema

0 comments on commit b0cae31

Please sign in to comment.