2

I am using these classes as part of a FastAPI response:

class SomeRules(str, Enum): a_rule = "something" b_rule = "something_else" class RuleChooser(BaseModel): rule: SomeRules = List[SomeRules] 

In the JSON response, I want to get rule as:

{ rule: ["something", "something_else"] } 

How do I achieve this?

Right now I am getting the output as:

rule: type.literal["something", "something_else"] 

or

literal.type["A", "B", "C"] 

I have tested multiple versions and none of them return the desired result.

  • rules = Literal["A", "B", "C"]
  • rule: SomeRules = Somerules.rules

Any help will be appreciated.

I am using Pydantic version 1.9.1

4
  • It's not clear what output you exactly need or how is this related to the type. Since you tagged this with [fastapi], I assume it's for a JSON response. If so, do you want to get something like {rule: ["something", "something_else"]}? Maybe it would help to show the "multiple versions" you tried. Commented Jul 13, 2022 at 13:41
  • Sorry, yes i am receiving JSON response. That is what i want. Now i am getting {rule: type.literal["something", "something_else"]} Commented Jul 13, 2022 at 13:56
  • I have tried with TypeVar("T") and GenericModel, also with a root_validator, and also it's mentioned in code comments. Commented Jul 13, 2022 at 13:59
  • 1
    You can edit your post by clicking on the Edit link under the question. I've tried to clarify your post for you, by incorporating the previous comments, but you can improve it by yourself. Commented Jul 13, 2022 at 14:05

1 Answer 1

3

The simplest, straightforward answer is to fix your definition for rule.

This

class RuleChooser(BaseModel): rule: SomeRules = List[SomeRules] 

says that rule is of type SomeRules and its value is a typing.List of SomeRules...which is definitely wrong because the value doesn't match the type. If you want rule to simply contain the string values of the enums, you can type it as List[str] then get all the values of the enums:

from pydantic import Field class SomeRules(str, Enum): a_rule = "something" b_rule = "something_else" class RuleChooser(BaseModel): rule: List[str] = Field(default=[rule.value for rule in SomeRules]) @app.get("/") async def root(): return RuleChooser() 
$ curl http://127.0.0.1:8000 {"rule":["something","something_else"]} 

Now, I'm guessing you are using the actual enum members in your app (not their string values), and you just want RuleChooser.rule to be strings only as part of the JSON response. Then, you need to again fix the definition of rule to:

from pydantic import Field class RuleChooser(BaseModel): rule: List[SomeRules] = Field(default=list(SomeRules)) 

which says that rule is of type typing.List of SomeRules, and its value are all the members of that Enum.

If you print an instance of RuleChooser().rule, you'll get:

[<SomeRules.a_rule: 'something'>, <SomeRules.b_rule: 'something_else'>] 

But as part of the JSON response, FastAPI can handle the conversion from enum to string, and you'll get:

@app.get("/") async def root(): return RuleChooser() 
$ curl http://127.0.0.1:8000 {"rule":["something","something_else"]} 

which is the same as what you wanted.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.