[Python] FastAPI and uvicorn not updating

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    I've got 2 separate issues with FastAPI. I'm going through a course and on the tagging part, my tags aren't showing in the docs. Additionally, for 1 endpoint that I provided status codes (default to 200), in docs it only shows a 404 & 422. Anyone have any ideas on what I might be doing wrong?

    from fastapi import FastAPI, status, Response
    from enum import Enum
    from typing import Optional

    app = FastAPI()

    class BlogType(str, Enum):
    short = 'short'
    story = 'story'
    howto = 'howto'

    @app.get('/')
    def index():
    return {"message": "Hello World!"}

    @app.get('/blog/{id}/', status_code=status.HTTP_200_OK)
    def get_blog(id: int, response: Response):
    if id > 5:
    response.status_code = status.HTTP_404_NOT_FOUND
    return {'error': f'Blog {id} not found'}
    else:
    response.status_code = status.HTTP_200_OK
    return {"message": f'Blog with id {id}'}

    @app.get('/blogs/', tags=["blog"])
    def get_all_blogs(page, page_size: Optional[int] = None):
    return {"message": 'All {page_size} blogs on page {page} provided'}

    @app.get('/blog/{id}/comments/{comment_id}/', tags=["blog", "comment"])
    def get_comment(id: int, comment_id: int, valid: bool = True, username: Optional[str] = None):
    return {'message': f'blog_id {id}, comment_id {comment_id}, valid {valid}, username {username}'}

    @app.get('/blog/type/{type}/')
    def get_blog_type(type: BlogType):
    return {'message': f'BlogType {type}'}


    FastAPI error codes

    Continue reading...

Compartilhe esta Página