File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22# Change to import.metadata when minimum python>=3.8
33from importlib_metadata import version as _version
44
5- from .prototype import vetiver_create_prototype , vetiver_create_ptype # noqa
5+ from .prototype import ( # noqa
6+ vetiver_create_prototype , # noqa
7+ vetiver_create_ptype , # noqa
8+ InvalidPTypeError , # noqa
9+ ) # noqa
610from .vetiver_model import VetiverModel # noqa
711from .server import VetiverAPI , vetiver_endpoint , predict # noqa
812from .mock import get_mock_data , get_mock_model # noqa
Original file line number Diff line number Diff line change @@ -175,6 +175,22 @@ def _(data: dict):
175175 data : dict
176176 Dictionary
177177 """
178+
179+ # if it comes from vetiver's /prototype endpoint
180+ dict_data = {}
181+ if data .keys () >= {"properties" , "title" , "type" }:
182+ # automatically create for simple prototypes
183+ try :
184+ for key , value in data ["properties" ].items ():
185+ dict_data .update ({key : (value ["type" ], value ["default" ])})
186+ # error for complex objects
187+ except KeyError :
188+ raise InvalidPTypeError (
189+ "Failed to create dict prototype for this data. "
190+ "Please use pandas DataFrame or pydantic BaseModel."
191+ )
192+ return create_prototype (** dict_data )
193+
178194 return create_prototype (** _to_field (data ))
179195
180196
Original file line number Diff line number Diff line change 1- from vetiver import mock , VetiverModel , VetiverAPI
1+ from vetiver import (
2+ mock ,
3+ VetiverModel ,
4+ VetiverAPI ,
5+ vetiver_create_prototype ,
6+ InvalidPTypeError ,
7+ )
28from pydantic import BaseModel , conint
39from fastapi .testclient import TestClient
410import numpy as np
@@ -76,7 +82,7 @@ def test_get_metadata(client):
7682 }
7783
7884
79- def test_get_prototype (client ):
85+ def test_get_prototype (client , vetiver_model ):
8086 response = client .get ("/prototype" )
8187 assert response .status_code == 200 , response .text
8288 assert response .json () == {
@@ -89,6 +95,11 @@ def test_get_prototype(client):
8995 "type" : "object" ,
9096 }
9197
98+ assert (
99+ vetiver_model .prototype .construct ()
100+ == vetiver_create_prototype (response .json ()).construct ()
101+ )
102+
92103
93104def test_complex_prototype (complex_prototype_model ):
94105 response = complex_prototype_model .get ("/prototype" )
@@ -103,3 +114,6 @@ def test_complex_prototype(complex_prototype_model):
103114 "title" : "CustomPrototype" ,
104115 "type" : "object" ,
105116 }
117+
118+ with pytest .raises (InvalidPTypeError ):
119+ vetiver_create_prototype (response .json ())
You can’t perform that action at this time.
0 commit comments