-
Notifications
You must be signed in to change notification settings - Fork 33
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
Deserialize using the default values if it exists #35
Conversation
@@ -115,9 +115,9 @@ def _default_value(x: Field): | |||
Returns: | |||
object: default value of the input Field. | |||
""" | |||
if x.default != MISSING: |
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'm not sure if it was a spelling mistake but if it wasn't I added the check for both MISSING object (_MISSING) and MISSING string(MISSING)
@@ -432,6 +437,11 @@ def deserialize_immutable(cls, data: dict) -> "Serializable": | |||
if field.name in vars(cls): |
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 check does not get the complex type (like List[int] and others). The default values of complex type fields will be ignored. To deal with that, I added here a check if the default value is not "MISSING" we use it instead of raising the error. So if we need that the user provides some param it needs to be defined as "MISSING" type (string or object).
Can you add a test that fails before this PR is applied but passed with it applied? |
Without this PR will break all times that we define a complex type (like List[int]) and do not provide it as data in .new_from_dict() or from_dict(). The default value will be ignored and it will raise an error. |
So while reviewing I noticed that you changed both diff --git a/tests/test_init_from_dict.py b/tests/test_init_from_dict.py
index 2e14d3f..2a4dc46 100644
--- a/tests/test_init_from_dict.py
+++ b/tests/test_init_from_dict.py
@@ -21,6 +21,8 @@ class Reference(Coqpit):
Person(name="Ceren", age=15),
]
)
+ people_ids: List[int] = field(
+ default_factory=lambda: [1, 2, 3])
@dataclass
@@ -31,6 +33,9 @@ class WithRequired(Coqpit):
def test_new_from_dict():
ref_config = Reference(name="Fancy", size=3 ** 10, people=[Person(name="Anonymous", age=42)])
+ in_place_deserialize = Reference()
+ in_place_deserialize.from_dict({"name": "Fancy", "size": 3 ** 10, "people": [{"name": "Anonymous", "age": 42}]})
+
new_config = Reference.new_from_dict(
{"name": "Fancy", "size": 3 ** 10, "people": [{"name": "Anonymous", "age": 42}]}
) But it passes even before this change. Do you have a good idea of what's going on? Should we keep the changes only to |
Yeah, on my side it only broke in |
Then let's take it out since we don't understand it, better to add if if a real need is identified and understood. Other than that LGTM! Thanks for the PR |
Done :) |
No description provided.