-
Notifications
You must be signed in to change notification settings - Fork 196
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
🐛 Fix handling of bool literals #783
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #783 +/- ##
==========================================
+ Coverage 93.67% 93.70% +0.03%
==========================================
Files 99 99
Lines 14290 14292 +2
Branches 25 25
==========================================
+ Hits 13386 13393 +7
+ Misses 898 893 -5
Partials 6 6
Continue to review full report in Codecov by Sentry.
|
CodSpeed Performance ReportMerging #783 will improve performances by 11.7%Comparing Summary
Benchmarks breakdown
|
please review |
Good call, but can we special case them as well actually? I'd think it's a pretty reasonable thing to use as a discriminant so fast pathing it makes sense. I don't think we need any sort of lookup, maybe just an enum with two variants being |
I don't see why special casing bools here will be beneficial enough. There is no evidence that this is a common case and there is no special treatment necessary to make it work. |
If you asked me what I would do I would special case nothing. But we're already special casing primitive types so let's do the same for bools. |
Apologies, after reviewing the code I see that this is about serialization, not validation. Let me check what happens during validation. |
Validation works well: from typing import Literal, Union
from pydantic import BaseModel
class Model(BaseModel):
x: Literal[0, False]
y: Union[str, Literal[False]]
z: Union[Literal[False], str]
xx: Union[int, Literal[False]]
yy: Union[Literal[False], int]
print(Model(x=False, y=False, z=False, xx=False, yy=False))
#> x=False y=False z=False xx=False yy=False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGMT my earlier feedback was misguided
thanks! |
Change Summary
Treat bool literals (
Literal[False]
,Literal[True]
) as generic py objects instead ofint
s.Related issue number
See pydantic/pydantic#6601
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @dmontagu