Unexpected response with the verification of the modified Enum class
See original GitHub issueExample
For string enum generation, there is a convenient way as the Python official docs said:
from enum import Enum,auto
class Enum0(str,Enum):
def _generate_next_value_(name, start, count, last_values):
return name
A = auto()
B = auto()
And this enum class is equivalent to the following:
class Enum1(str,Enum):
A = 'A'
B = 'B'
Both of them work with the below FastAPI code(change type hint to each class before run):
from fastapi import FastAPI
app = FastAPI()
@app.get("/model/{mod}")
async def model(mod:Enum0):
return {'msg':mod,'val':mod.value}
However, when I write a self-defined enum class like this:
class Enum2(str,Enum):
def __init__(self):
self._value_ = self.name
super().__init__()
A = ()
B = ()
It doesn’t work with the previous model(mod)
callable.
Description
- Open the browser and call the endpoint
/model/A
. - It returns a JSON with
{"detail":[{"loc":["path","mod"],"msg":"value is not a valid enumeration member; permitted: 'A', 'B'","type":"type_error.enum","ctx":{"enum_values":["A","B"]}}]}
. - But I expected it to return
{"msg":"A","val":"A"}
. - I compared the
Enum1
and theEnum2
, and can not find any valuable difference.
Environment
- OS: Windows 10 x64, 10.0.19042.630
- FastAPI Version: 0.61.2
- Python Version: 3.8.5 x64
Additional context
- The style of generating enum member like
A = ()
is come from Python docs(enum#using-a-custom-new)
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Mocking Java enum to add a value to test fail case
Here is a complete example. The code is almost like your original (just simplified better test validation): public enum MyEnum {A, ...
Read more >Comparing a numerical value with extensible enum ...
Comparing a numerical value with extensible enum 'Extensible Enumeration(PriceDiscProductCodeType)' will yield unexpected results. Verified.
Read more >Validations for Enum Types - Baeldung
For this, we can create an annotation that checks if the String is valid for a specific enum. This annotation can be added...
Read more >How to Validate Enumeration Values in Property Setters
Below is the class which can answer the question whether enumeration value is valid or not, for any given enumeration type. It is...
Read more >How to make the most of Java enums - Oracle Blogs
Enumerations—or “enums” for short—are a restricted Java data type that lets a ... The answer lies in the modified syntax previously used for ......
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 Free
Top 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
Your Enum2 doesn’t work in pydantic for the same reason the following code doesn’t work without pydantic:
You could argue that this is a bug in either your code, or a bug in the enum library, but either way it doesn’t work
Do you mean to be doing
Instead?