-
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
Conversation
Coverage Report
|
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 |
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 has bool
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
res = False | ||
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
The argument is passed directly to issubclass
which accepts a tuple.
https://python-reference.readthedocs.io/en/latest/docs/functions/issubclass.html
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 see, however, there is one twist there: https://docs.python.org/3.11/library/functions.html#issubclass
Changed in version 3.10: classinfo can be a Union Type.
I'll see if this affects anything
Closing as this is too risky to integrate, will instead do a separate one for v1 |
Fixes #466 (at least the boolean part of it that is defo a bug)
Enhanced builtin instantiation introduced in 0.5.8 had a side effect of converting any int in json to a
True
boolean value if the destination field hasbool
type. This PR narrows allowed value range for implicit conversion to boolean