testdriven.io: Test-Driven Development with FastAPI and Docker: config.testing is coming as a string, not bool

71 views Asked by At

Not far into the course, at "Getting Started" I am running with main.py and config.py and seem to get config.testing back as a str and not a bool.

Repo/branch here: https://github.com/dycw/tutorial-test-driven-development-with-fastapi-and-docker/blob/getting-started/

or with source

# src/app/main.py

from fastapi import Depends, FastAPI

from app.config import Settings, get_settings

app = FastAPI()


@app.get("/ping")
async def pong(*, settings: Settings = Depends(get_settings)) -> dict[str, str | bool]:
    return {
        "ping": "pong!",
        "environment": settings.environment,
        "testing": settings.testing,
    }
# src/app/config.py

from functools import lru_cache
from logging import getLogger
from typing import cast

from pydantic import BaseSettings

_LOGGER = getLogger("uvicorn")


class Settings(BaseSettings):
    environment: str = "dev"
    testing: bool = cast(bool, 0)


@lru_cache
def get_settings() -> Settings:
    _LOGGER.info("Loading config settings from the environment...")
    return Settings()

My JSON returns:

enter image description here

1

There are 1 answers

0
David Reno On

Your code looks a bit different than what I see in the tutorial today, but mine returns a non-quoted false value. The relevant line for me is testing: bool = 0 in config.py.

I'd suggest looking at the current version of the tutorial and updating your code to look like what they have now.