Float enum query parameter fails to validate but works in plain pydantic
See original GitHub issueExample
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:
- Created 2 years ago
- Comments:9 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 😃