Fix #175: Replace unsafe assert statements with explicit exceptions #61
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This pull request addresses Issue #175. It replaces two assert statements with ValueError exceptions in src/container_inspector/cli.py to make the code more reliable.
Details
This pull request addresses a well-known best practice for error handling in Python. I noticed that the code was using assert statements for runtime validation of user input, which can lead to unexpected behavior in production.
To resolve this, I replaced those assert statements with explicit if...raise statements. This provides clear, consistent error handling that isn't dependent on the Python interpreter's optimization settings.
The specific code changes are as follows:
Change 1: In the _container_inspector_squash function, I replaced the assert statement with an if...raise block to ensure the program always validates the number of images.
Change 2: In the _container_inspector_dockerfile function, I made the same change to validate that a user specifies either JSON or CSV output.
Issue: #175
Checklist
[ ] I have verified the changes locally to ensure the new exceptions are raised correctly.
[ ] The changes follow the project's code style and best practices.
[ ] This is my first contribution to this project, and I've followed the contribution guidelines.
My Contribution Process
This is my first open-source contribution, and I am excited to be a part of the AboutCode community. My process involved:
Forking the repository to create my own copy.
Cloning my fork to work on my local machine.
Locating the cli.py file to address this specific issue.
Replacing the assert statements with if...raise to improve the code's robustness.
Committing the changes to a new branch linked to this issue.
I had a great time working on this and learned a lot about the contribution process—from forking and cloning to making the PR itself.
Thanks for the opportunity to contribute!