Skip to content
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

#104: Skip serialization of empty INI properties when configured #336

Merged
merged 12 commits into from
Oct 13, 2022

Conversation

priscavdsluis
Copy link
Contributor

@priscavdsluis priscavdsluis commented Sep 6, 2022

Fixes #104

@priscavdsluis priscavdsluis linked an issue Sep 6, 2022 that may be closed by this pull request
@priscavdsluis priscavdsluis marked this pull request as ready for review September 8, 2022 12:25
tests/io/test_ini.py Show resolved Hide resolved

def _serialize(self, _: dict) -> None:
config = SerializerConfig(skip_empty_properties=False)
write_ini(self._resolved_filepath, self._to_document(), config=config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visual Studio gives the following error for self._resolved_filepath:

Argument of type "Path | None" cannot be assigned to parameter "path" of type "Path" in function "write_ini" Type "Path | None" cannot be assigned to type "Path" Type "None" cannot be assigned to type "Path"

I think it is because _resolved_filepath is Optional and write_ini() expects a non-optional path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this method is called, the _resolved_filepath property is checked for None, so it cannot be None here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that so? I see such a check only in ParsableFileModel._serialize(), but that will not be called if a subclass overrides that method, for example in INIModel._serialize().

I think the place that now makes sure everything runs well is this:

class FileModel(BaseModel, ABC):
# ...
    def _save_instance(self) -> None:
        if self.filepath is None:

Should we not add documentation that _serialize() should always only be called via save() and never directly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@priscavdsluis wrote that the latter is implicitly so, because _serialize() is a private function. That is true for end-user APIdocs. But amongst core-developers, it is good to know that other private functions should probably also not call _serialize directly. Maybe some one-liner docs in the function body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some docs and will close this PR.

Copy link
Member

@arthurvd arthurvd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@priscavdsluis: almost done, please see one remaining comment.

@sonarcloud
Copy link

sonarcloud bot commented Oct 13, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@priscavdsluis priscavdsluis merged commit 9679df6 into main Oct 13, 2022
@priscavdsluis priscavdsluis deleted the fix/104-skip-serialization-empty-properties branch October 13, 2022 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional location-related fields should not be written when empty
3 participants