-
Notifications
You must be signed in to change notification settings - Fork 154
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
Narrow down implicit boolean conversion introduced in 0.5.8 #468
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,7 +244,16 @@ def _support_extended_types(field_type, field_value): | |
res = (field_value | ||
if isinstance(field_value, UUID) | ||
else UUID(field_value)) | ||
elif _issubclass_safe(field_type, (int, float, str, bool)): | ||
elif _issubclass_safe(field_type, bool): | ||
# issubclass(bool, int) -> True | ||
# thus values >1 will always convert to True in the next clause, unless intercepted | ||
if field_value in ('True', 'true', True, 1): | ||
res = True | ||
elif field_value in ('False', 'false', False, 0): | ||
res = False | ||
matt035343 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
raise ValueError(f"Value {field_value} of input type {field_type.__name__} cannot be decoded as boolean") | ||
elif _issubclass_safe(field_type, int) or _issubclass_safe(field_type, float) or _issubclass_safe(field_type, str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep the tuple notation here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct because that method doesn't accept tuple. It was an error in original implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument is passed directly to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, however, there is one twist there: https://docs.python.org/3.11/library/functions.html#issubclass
I'll see if this affects anything |
||
res = (field_value | ||
if isinstance(field_value, field_type) | ||
else field_type(field_value)) | ||
|
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.
I do not see >1 ints being set to true, or am I blind?
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.
Run code from the issue author with 0.5.14 and you will see the 1234 int field being set to True when running
from_dict
, if target field hasbool
type.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.
How is that caught by your if-statements below?
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.
It is not caught, we just do not allow the invocation to happen in the first place, if target type is bool.
field_type
is the type of a dataclass field.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.
As discussed offline, None should become False, otherwise I am good