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.

Unexpected response with the verification of the modified Enum class

See original GitHub issue

Example

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 the Enum2, 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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Mausecommented, Nov 25, 2020

Your Enum2 doesn’t work in pydantic for the same reason the following code doesn’t work without pydantic:


from enum import Enum

class​ ​Enum2​(​str​,​Enum​):
    ​def​ ​__init__​(​self​):
        ​self​.​_value_​ ​=​ ​self​.​name​
        ​super​().​__init__​()
    ​A​ ​=​ ()
    ​B​ ​=​ ()

Enum2('A') # ValueError: 'A' is not a valid Enum2

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

1reaction
Mausecommented, Nov 25, 2020

Do you mean to be doing

A = auto()
B = auto()

Instead?

Read more comments on GitHub >

github_iconTop 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 >

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