question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Float enum query parameter fails to validate but works in plain pydantic

See original GitHub issue

Example

from enum import Enum

from fastapi import FastAPI
from pydantic import BaseModel


app = FastAPI()


class Number(float, Enum):
    ten = 10.0
    twelve = 12.0


class MyModel(BaseModel):
    num: Number


MyModel(num=10.0)  # <- This works!


@app.get("/")
async def root(num: Number):
    return {"message": f"Hello {num}"}
$ uvicorn test:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [176218] using statreload
INFO:     Started server process [176220]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
$ curl -s http://localhost:8000?num=10.0
{"detail":[{"loc":["query","num"],"msg":"value is not a valid enumeration member; permitted: 10.0, 12.0","type":"type_error.enum","ctx":{"enum_values":[10.0,12.0]}}]}

Description

  • Open the browser and call the endpoint /?num=10.0.
  • It returns a JSON with {"detail":[{"loc":["query","num"],"msg":"value is not a valid enumeration member; permitted: 10.0, 12.0","type":"type_error.enum","ctx":{"enum_values":[10.0,12.0]}}]}.
  • But I expected it to return {"message": "Hello 10.0"}.

Note how MyModel(num=10.0) in the example above does not throw a ValidationError, so pydantic seems to validate this fine - it is only when calling the / FastAPI endpoint with 10.0 passed for the num: Number parameter that a validation error is returned.

Environment

  • OS: Linux
  • FastAPI Version: 0.61.1
  • Python version: 3.8.5

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
estancommented, May 19, 2021

Wow. Let me digest this in a while.

0reactions
Hultnercommented, May 19, 2021

Wow. Let me digest this in a while.

Yes, hope it solves your problems. Tell me if you want me to elaborate on anything. On a side note, nice to see a fellow Swedish FastAPI user 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pydantic enum field does not get converted to string
I am trying to restrict one field in a class to an enum. However, when I try to get a dictionary out of...
Read more >
Path parameters with types - FastAPI
You can declare path "parameters" or "variables" with the same syntax used ... So, with the same Python type declaration, FastAPI gives you...
Read more >
Field Types - pydantic
Callable fields only perform a simple check that the argument is callable; no validation of arguments, their types, or the return type is...
Read more >
Pydantic in a Nutshell - Python in Plain English
The power of type annotation-based parsing and validation. ... While pydantic will work well with any IDE out of the box but there...
Read more >
Pandas DataFrame Validation with Pydantic - Part 2
We show how to create a simple, yet flexible and powerful way to do ... is not an int but a float, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found