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

In Python 3.12 xml.etree.ElementTree will raise DeprecationWarning: Testing an element's truth value will raise an exception in future versions #2453

Closed
matthewfeickert opened this issue Mar 12, 2024 · 0 comments · Fixed by #2459
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed / contributions welcome tests pytest

Comments

@matthewfeickert
Copy link
Member

matthewfeickert commented Mar 12, 2024

While testing Python 3.12 in CI

pyhf/tests/test_export.py

Lines 438 to 450 in adddb07

def test_integer_data(datadir, mocker):
"""
Test that a spec with only integer data will be written correctly
"""
with open(
datadir.joinpath("workspace_integer_data.json"), encoding="utf-8"
) as spec_file:
spec = json.load(spec_file)
channel_spec = spec["channels"][0]
mocker.patch("pyhf.writexml._ROOT_DATA_FILE")
channel = pyhf.writexml.build_channel(spec, channel_spec, {})
assert channel

raised

>       assert channel
E       DeprecationWarning: Testing an element's truth value will raise an exception in future versions.  Use specific 'len(elem)' or 'elem is not None' test instead.

This comes from python/cpython#83122 which landed in Python 3.12. This should get fixed before Python 3.12 support is added.

From the Python 3.12 docs: https://docs.python.org/3.12/library/xml.etree.elementtree.html#element-objects

Caution: Elements with no subelements will test as False. Testing the truth value of an Element is deprecated and will raise an exception in Python 3.14. Use specific len(elem) or elem is None test instead.:

element = root.find('foo')

if not element:  # careful!
    print("element not found, or element has no subelements")

if element is None:
    print("element not found")

Changed in version 3.12: Testing the truth value of an Element emits DeprecationWarning.

@matthewfeickert matthewfeickert added bug Something isn't working help wanted Extra attention is needed / contributions welcome tests pytest labels Mar 12, 2024
@matthewfeickert matthewfeickert self-assigned this Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed / contributions welcome tests pytest
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant