Skip to content

Fix Element truth-value deprecation warnings in salt.utils.xmlutil - #69904

Open
bogdanr wants to merge 1 commit into
saltstack:masterfrom
bogdanr:fix-xmlutil-truthiness-deprecation
Open

Fix Element truth-value deprecation warnings in salt.utils.xmlutil#69904
bogdanr wants to merge 1 commit into
saltstack:masterfrom
bogdanr:fix-xmlutil-truthiness-deprecation

Conversation

@bogdanr

@bogdanr bogdanr commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #69903.

salt.utils.xmlutil._to_dict, _to_full_dict, and clean_node tested the truth value of xml.etree.ElementTree.Element objects directly (e.g. if not xmltree:, if item:, if ... and parent:). An Element's truth value is defined by whether it has children, which Python deprecates in favor of explicit len(elem)/elem is not None checks, emitting:

DeprecationWarning: Testing an element's truth value will always return True in future versions.  Use specific 'len(elem)' or 'elem is not None' test instead.

salt.utils.aws.query() calls xmlutil.to_dict() on essentially every AWS API response, so this fires on every single AWS API call made through the ec2 cloud driver (recently extracted to saltext-cloud), and anywhere else parsing XML via this module (e.g. the virt module's change_xml).

This is the same class of issue as #56475 (getchildren() deprecation in the same file), which addressed iteration but missed truth-value testing.

What issues does this PR fix or reference?

Fixes #69903
Related to #56475

Previous Behavior

salt.utils.xmlutil.to_dict()/change_xml() emit DeprecationWarning: Testing an element's truth value... on every call.

New Behavior

No warning is emitted; behavior of to_dict()/change_xml() is unchanged (verified against all existing to_dict test cases in both old- and new-style test suites, plus a new regression test asserting no such warning is raised).

`_to_dict`, `_to_full_dict`, and `clean_node` tested the truth value of
`xml.etree.ElementTree.Element` objects directly (e.g. `if not
xmltree:`, `if item:`, `if ... and parent:`). An Element's truth value
is defined by whether it has children, which Python deprecates in favor
of explicit `len(elem)`/`elem is not None` checks:

    DeprecationWarning: Testing an element's truth value will always
    return True in future versions. Use specific 'len(elem)' or 'elem
    is not None' test instead.

`salt.utils.aws.query()` calls `xmlutil.to_dict()` on essentially every
AWS API response, so this warning fires on every single AWS API call
made through the `ec2` cloud driver (and anywhere else parsing XML via
this module, e.g. the virt module's `change_xml`).

This is the same underlying issue as saltstack#56475 (getchildren() deprecation
in the same file), which addressed iteration but missed truth-value
testing.

Fixes saltstack#69903

@twangboy twangboy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please rebase this on the earliest branch where this but exists, probably 3006.x.

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.

salt.utils.xmlutil relies on deprecated Element truth-value testing

2 participants