Closed diamond-fish closed 1 year ago
remove typing
from your reqs?
Removing typing
worked, until I continued with the tutorial and it broke again :(. I wish Deta was more predictable -- I cannot tell when pushing a code will break the API. Here's the current code and I can't get Deta to work even though it works in other cloud Python services:
from enum import Enum
from typing import Annotated, Union
from fastapi import FastAPI, Query
from pydantic import BaseModel
class Item(BaseModel):
description: Union[str, None] = None
name: str
price: Annotated[float, Query(gt=0)]
tax: Union[float, None] = None
class ModelName(str, Enum):
ALEXNET = 'alexnet'
LENET = 'lenet'
RESNET = 'resnet'
class BaseUser(BaseModel):
email: Union[str, None] = None
full_name: Union[str, None] = None
username: str
class UserIn(BaseUser):
password: str
app = FastAPI()
fake_items_db = [
{ 'item_name': 'Foo' },
{ 'item_name': 'Bar' },
{ 'item_name': 'Baz' },
]
@app.get('/')
def get_root():
return { 'message': 'Hello World' }
@app.get('/items/')
def get_items(skip: int = 0, limit: int = 10):
return fake_items_db[skip : skip + limit]
@app.get('/items/{item_id}')
def get_item(item_id: int, is_flag: Annotated[Union[bool, None], Query(alias='flag')] = None):
response = { 'item_id': item_id }
if is_flag:
response['is_flag'] = is_flag
return response
@app.get('/models/{model_name}')
def get_model(model_name: ModelName):
response = {}
match model_name:
case 'alexnet':
response = { 'model_name': model_name }
case 'lenet':
response = { 'model_name': model_name }
case 'resnet':
response = { 'model_name': model_name }
return response
@app.post('/items/')
def create_item(item: Item) -> Item:
return item
@app.post('/user/', response_model_exclude_unset=True)
def create_user(user: UserIn) -> BaseUser:
return user
How do you get the code above to work when deployed?
I found out the root cause: I was using Python 3.10 pattern (match/case), but Deta currently runs on Python 3.9.
I'm following this tutorial , but after
space push
I get the error below even though it works fine locally (viauvicorn main:app --reload
) and deploys without errors.https://fastapitutorial-1-j0837537.deta.app/
main.py
requirements.txt
Spacefile