Resolve postponed annotations before validation and schema creation

This commit is contained in:
Andras Schmelczer 2026-10-27 19:53:40 +00:00
commit 5e819525db

View file

@ -1,6 +1,8 @@
import sys
from types import ModuleType
import pytest
from fastapi.testclient import TestClient
from great_ai import GreatAI
@ -32,3 +34,25 @@ def test_positional_only_inputs_keep_their_bindings(isolated_context: None) -> N
response = client.post("/predict", json={"first": "b", "second": 3})
assert response.status_code == 200
assert response.json()["output"] == "bbb"
def test_postponed_model_annotations_resolve_in_their_module(
isolated_context: None, monkeypatch: pytest.MonkeyPatch
) -> None:
module = ModuleType("great_ai_test_annotations")
monkeypatch.setitem(sys.modules, module.__name__, module)
exec(
"from __future__ import annotations\n"
"from pydantic import BaseModel\n"
"class Payload(BaseModel):\n value: int\n"
"def predict(data: Payload) -> int:\n return data.value\n",
module.__dict__,
)
service = GreatAI.create(module.predict)
with pytest.raises(TypeError):
service("wrong")
with TestClient(service.app) as client:
response = client.post("/predict", json={"data": {"value": 7}})
assert response.status_code == 200
assert response.json()["output"] == 7
assert client.get("/openapi.json").status_code == 200