fix: Fix Python 2.7.5 compatibility by using msg= in fail_json() calls - #154
Conversation
- Changed all module.fail_json() calls to use msg= keyword argument instead of positional arguments - Updated tests/unit/test_bootloader_settings.py: Fixed test assertions to expect msg= format This resolves TypeError: "fail_json() takes 1 positional argument but 2 were given" that occurred on older Ansible versions used with Python 2.7.5, where fail_json() requires the error message to be passed as a keyword argument rather than positional. Generated with the Curate AI assistance
Reviewer's GuideThis PR ensures compatibility with Python 2.7.5 by converting all module.fail_json() calls to use the msg= keyword argument and updating unit tests to expect this signature. Class diagram for fail_json() usage in Ansible modulesclassDiagram
class Module {
+fail_json(msg, **kwargs)
+run_command(cmd)
}
class bootloader_settings {
+validate_kernel_initrd()
+validate_default_kernel()
+validate_kernels()
+rm_kernel()
+get_default_kernel()
+run_module()
}
class bootloader_facts {
+run_module()
}
Module <|-- bootloader_settings
Module <|-- bootloader_facts
bootloader_settings : uses fail_json(msg=...)
bootloader_facts : uses fail_json(msg=...)
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
[citest] |
There was a problem hiding this comment.
Hey @spetrosi - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `library/bootloader_settings.py:205` </location>
<code_context>
):
module.fail_json(
- "kernel value in %s must be of type str or dict"
+ msg="kernel value in %s must be of type str or dict"
% bootloader_setting["kernel"]
)
</code_context>
<issue_to_address>
The error message could be more explicit about the actual type received.
Suggest including the actual type of bootloader_setting["kernel"] in the error message for easier debugging, e.g., 'kernel value in %s (type %s) must be of type str or dict'.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
if (not isinstance(bootloader_setting["kernel"], dict)) and (
not isinstance(bootloader_setting["kernel"], str)
):
module.fail_json(
msg="kernel value in %s must be of type str or dict"
% bootloader_setting["kernel"]
)
=======
if (not isinstance(bootloader_setting["kernel"], dict)) and (
not isinstance(bootloader_setting["kernel"], str)
):
module.fail_json(
msg="kernel value in %s (type %s) must be of type str or dict"
% (bootloader_setting["kernel"], type(bootloader_setting["kernel"]).__name__)
)
>>>>>>> REPLACE
</suggested_fix>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if (not isinstance(bootloader_setting["kernel"], dict)) and ( | ||
| not isinstance(bootloader_setting["kernel"], str) | ||
| ): | ||
| module.fail_json( | ||
| "kernel value in %s must be of type str or dict" | ||
| msg="kernel value in %s must be of type str or dict" | ||
| % bootloader_setting["kernel"] | ||
| ) |
There was a problem hiding this comment.
suggestion: The error message could be more explicit about the actual type received.
Suggest including the actual type of bootloader_setting["kernel"] in the error message for easier debugging, e.g., 'kernel value in %s (type %s) must be of type str or dict'.
| if (not isinstance(bootloader_setting["kernel"], dict)) and ( | |
| not isinstance(bootloader_setting["kernel"], str) | |
| ): | |
| module.fail_json( | |
| "kernel value in %s must be of type str or dict" | |
| msg="kernel value in %s must be of type str or dict" | |
| % bootloader_setting["kernel"] | |
| ) | |
| if (not isinstance(bootloader_setting["kernel"], dict)) and ( | |
| not isinstance(bootloader_setting["kernel"], str) | |
| ): | |
| module.fail_json( | |
| msg="kernel value in %s (type %s) must be of type str or dict" | |
| % (bootloader_setting["kernel"], type(bootloader_setting["kernel"]).__name__) | |
| ) |
| msg="You can use 'initrd' as a kernel key only when you must create a kernel. To modify or remove an existing kernel, use one of %s" | ||
| % ", ".join(kernel_mod_keys) |
There was a problem hiding this comment.
suggestion (code-quality): Replace interpolated string formatting with f-string (replace-interpolation-with-fstring)
| msg="You can use 'initrd' as a kernel key only when you must create a kernel. To modify or remove an existing kernel, use one of %s" | |
| % ", ".join(kernel_mod_keys) | |
| msg=f"""You can use 'initrd' as a kernel key only when you must create a kernel. To modify or remove an existing kernel, use one of {", ".join(kernel_mod_keys)}""" |
| msg="You cannot set a kernel as default when you are using a string kernel - %s" | ||
| % (kernel) |
There was a problem hiding this comment.
suggestion (code-quality): Replace interpolated string formatting with f-string (replace-interpolation-with-fstring)
| msg="You cannot set a kernel as default when you are using a string kernel - %s" | |
| % (kernel) | |
| msg=f"You cannot set a kernel as default when you are using a string kernel - {kernel}" |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #154 +/- ##
==========================================
+ Coverage 78.43% 86.90% +8.47%
==========================================
Files 2 2
Lines 255 275 +20
==========================================
+ Hits 200 239 +39
+ Misses 55 36 -19
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This resolves TypeError: "fail_json() takes 1 positional argument but 2 were given"
that occurred on older Ansible versions used with Python 2.7.5, where fail_json()
requires the error message to be passed as a keyword argument rather than positional.
Generated with the Curate AI assistance
Summary by Sourcery
Use msg= keyword in all module.fail_json calls to resolve TypeError on older Ansible versions, and update unit tests to expect the new format
Bug Fixes:
Tests: