I am using enums with FastAPI (see code snippets below). As the code is generated by FastAPI, I do not have an idea on how to make the enum hashable. Is there something I can add to make the enums hashable?
0.110.2
1.10.15
Python 3.9.13
DraftType:
type: string
enum:
- Type1
- Type2
- Type3
which is generated to the following Python class:
class DraftType(str, Enum):
Type1 = "Type1"
Type2 = "Type2"
Type3 = "Type3"
It is used in another type:
Draft:
type: object
properties:
draftType:
$ref: "#/components/schemas/DraftType"
version:
type: integer
format: int64
required:
- draftType
- version
The classes are then used in the FastAPI app:
@async_http_exception
async def get_draft(
draft_request: DraftRequest = Body(None, description=""),
) -> None:
draft: Draft = DraftLoader.get_draft(draft_request=draft_request)
class DraftRequest(BaseModel):
draft_type: DraftType = Field(alias="draftType")
DraftRequest.update_forward_refs()
class DraftLoader:
...
@staticmethod
@lru_cache()
def get_draft(draft_request: DraftRequest) -> Draft:
...
# fetch latest draft version...
draft: Draft = ...
...
return draft
However, the code raises an error when calling DraftLoader.get_draft
:
E File "<!- some file path ->", line 46, in get_draft
E draft: Draft = DraftLoader.get_draft(draft_request=draft_request)
E TypeError: unhashable type: 'DraftRequest'
How can I specify, that DraftRequest
is generated to be hashable?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too