Skip to content

fix: Fix Python 2.7.5 compatibility by using msg= in fail_json() calls - #154

Merged
spetrosi merged 1 commit into
linux-system-roles:mainfrom
spetrosi:fix-fail-json
Aug 1, 2025
Merged

fix: Fix Python 2.7.5 compatibility by using msg= in fail_json() calls#154
spetrosi merged 1 commit into
linux-system-roles:mainfrom
spetrosi:fix-fail-json

Conversation

@spetrosi

@spetrosi spetrosi commented Aug 1, 2025

Copy link
Copy Markdown
Contributor
  • 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

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:

  • Change all module.fail_json calls in bootloader_settings and bootloader_facts to use msg= keyword argument instead of positional message

Tests:

  • Update unit tests to assert fail_json is called with msg= keyword argument

- 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
@spetrosi
spetrosi requested a review from richm as a code owner August 1, 2025 09:09
@sourcery-ai

sourcery-ai Bot commented Aug 1, 2025

Copy link
Copy Markdown

Reviewer's Guide

This 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 modules

classDiagram
    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=...)
Loading

File-Level Changes

Change Details Files
Refactored module.fail_json calls to use msg= keyword argument for error messages
  • Converted positional error message arguments in fail_json() to use msg=
  • Retained original message formatting and % interpolation
  • Applied changes across bootloader_settings and bootloader_facts modules
library/bootloader_settings.py
library/bootloader_facts.py
Updated unit tests to assert msg= in fail_json calls
  • Replaced positional fail_json() assertions with assert_called_once_with(msg=…) in tests
  • Adjusted expected error message parameters to use msg keyword
tests/unit/test_bootloader_settings.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@spetrosi

spetrosi commented Aug 1, 2025

Copy link
Copy Markdown
Contributor Author

[citest]

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 201 to 207
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"]
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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'.

Suggested change
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__)
)

Comment on lines +121 to 122
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (code-quality): Replace interpolated string formatting with f-string (replace-interpolation-with-fstring)

Suggested change
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)}"""

Comment on lines +168 to 169
msg="You cannot set a kernel as default when you are using a string kernel - %s"
% (kernel)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (code-quality): Replace interpolated string formatting with f-string (replace-interpolation-with-fstring)

Suggested change
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

codecov Bot commented Aug 1, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.90%. Comparing base (63d7c0c) to head (0475710).
⚠️ Report is 64 commits behind head on main.

Files with missing lines Patch % Lines
library/bootloader_facts.py 0.00% 1 Missing ⚠️
library/bootloader_settings.py 66.66% 1 Missing ⚠️
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     
Flag Coverage Δ
sanity ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@spetrosi
spetrosi merged commit c672ee7 into linux-system-roles:main Aug 1, 2025
35 of 36 checks passed
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.

2 participants