-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use_enum_values=True has no effect if another model with the same enum has use_enum_values=False #8858
Comments
Yep, looks like a bug. Thanks for reporting the regression. You're welcome to submit a PR with a fix, if you'd like. I'll add this to the 2.7 milestone! |
@sydney-runkle I can try and look into this if it's still available 😄 |
For sure! Go for it! |
Ok, I think I found the source of the problem. When trying to debug this I wrote a test def test_use_enum_values_in_single_submodel():
class MyEnum(str, Enum):
A = 'a'
class MyModel(BaseModel, use_enum_values=True):
my_enum: MyEnum
class OtherModel(BaseModel):
my_enum: MyEnum
class Model(BaseModel):
other_model: OtherModel
my_model: MyModel
obj = Model.model_validate({'my_model': {'my_enum': 'a'}, 'other_model': {'my_enum': 'a'}})
assert not isinstance(obj.my_model.my_enum, MyEnum)
assert isinstance(obj.other_model.my_enum, MyEnum) When running this test, class Model(BaseModel):
my_model: MyModel
other_model: OtherModel Then After debugging a little, I saw that when generating schemas for The problem seemed to be when generating the schema of def simplify_schema_references(schema: core_schema.CoreSchema) -> core_schema.CoreSchema: # noqa: C901
definitions: dict[str, core_schema.CoreSchema] = {}
ref_counts: dict[str, int] = defaultdict(int)
involved_in_recursion: dict[str, bool] = {}
current_recursion_ref_count: dict[str, int] = defaultdict(int)
def collect_refs(s: core_schema.CoreSchema, recurse: Recurse) -> core_schema.CoreSchema:
if s['type'] == 'definitions':
for definition in s['definitions']:
ref = get_ref(definition)
assert ref is not None
if ref not in definitions:
definitions[ref] = definition
recurse(definition, collect_refs)
return recurse(s['schema'], collect_refs)
else:
ref = get_ref(s)
if ref is not None:
new = recurse(s, collect_refs)
new_ref = get_ref(new)
if new_ref:
definitions[new_ref] = new
return core_schema.definition_reference_schema(schema_ref=ref)
else:
return recurse(s, collect_refs) As far as I understood, when if new_ref:
definitions[new_ref] = new which would basically overwrite the previous schema of I hope that explanation makes sense. Unfortunately, with my current limited understating of the core logic of the library, I couldn't think of a good solution for this problem. @sydney-runkle do you perhaps have an idea that could point me in the right direction? |
Great job looking into this. Funnily enough, I just spent some time battling with how I'll take a closer look at this today (maybe finding a solution to one of these problems will help us with the other). I'm guessing that we might need to do some better equality checks when we overwrite the ref values in the definitions dict. Thanks again - I'll keep you updated! |
@sydney-runkle Awesome! Let me know if there's anything I can do to help out |
Initial Checks
Description
If two separate models with different
use_enum_values
settings both have fields with the same enum, pydantic v2 doesn't use that enum's values in either model.This is a regression from pydantic v1.
To make
MyModel
receive enum's value in pydantic v2, I'd have to enableuse_enum_values
forOtherModel
as well.Example Code
No response
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: