I'm building a Fast API server that serves code on behalf of my customers.
So my directory structure is:
project
| main.py
|--customer_code (mounted at runtime)
| blah.py
Within main.py
I have:
from customer_code import blah
from fastapi import FastAPI
app = FastAPI()
...
@app.post("/do_something")
def bar(# I want this to have the same type as blah.foo()):
blah.foo()
and within blah.py
I have:
from pydantic import BaseModel
class User(BaseModel):
id: int
name = 'John Doe'
def foo(data : User):
# does something
I don't know a priori what types my customers' code (in blah.py
) will expect. But I'd like to use FastAPI's built-in generation of Open API schemas (rather than requiring my customers to accept and parse JSON inputs).
Is there a way to set the types of the arguments to bar
to be the same as the types of the arguments to foo
?
Seems like one way would be to do exec
with fancy string interpolation but I worry that that's not really Pythonic and I'd also have to sanitize my users' inputs. So if there's another option, would love to learn.