Skip to content

Fix ImportError with lxml==5.2.1 (Cython 3.0)#243

Merged
medley56 merged 2 commits intomainfrom
copilot/fix-import-error-lxml-5-2-1
Mar 31, 2026
Merged

Fix ImportError with lxml==5.2.1 (Cython 3.0)#243
medley56 merged 2 commits intomainfrom
copilot/fix-import-error-lxml-5-2-1

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

In lxml 5.2.1 (Cython 3.0.10), lxml.etree.ElementTree and lxml.etree.Element are compiled as Cython functions rather than Python classes. This breaks two things at import time: the | union type annotation syntax (e.g. ElementTree.ElementTree | None) and isinstance() checks using those names.

Changes

  • from __future__ import annotations added to all affected modules (common.py, xtce/calibrators.py, xtce/comparisons.py, xtce/encodings.py, xtce/parameter_types.py, xtce/parameters.py, xtce/validation.py). This defers annotation evaluation to strings, so the | operator is never called on a Cython function at module load time.

  • isinstance() fix in xtce/validation.py: replaced isinstance(xml_source, ElementTree.ElementTree) with isinstance(xml_source, ElementTree._ElementTree). _ElementTree is the actual underlying class (available across all supported lxml versions); ElementTree.ElementTree is a Cython function in 5.2.1 and cannot be used as an isinstance target.

  • Removed redundant quoted annotations (e.g. -> "Calibrator") that became unnecessary once from __future__ import annotations was added.

# Before (fails at import with lxml 5.2.1):
tree: ElementTree.ElementTree | None  # TypeError: unsupported operand type(s) for |

# After (deferred to string; never evaluated at load time):
from __future__ import annotations
tree: ElementTree.ElementTree | None  # fine

Summary

Fixes a TypeError on import when using lxml==5.2.1 caused by Cython 3.0 changing lxml.etree.ElementTree from a class to a Cython function. The fix uses from __future__ import annotations to avoid runtime evaluation of type annotations, and switches the one isinstance() check to use the concrete _ElementTree class.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • www.w3.org
    • Triggering command: /usr/bin/python python -m pytest tests/unit/test_xtce/test_validation.py -v --no-header (dns block)
    • Triggering command: /usr/bin/python python -m pytest tests/unit/ -x -q --no-header k/_temp/ghcca-node/node/bin/git (dns block)
    • Triggering command: /usr/bin/python python -m pytest tests/integration/ -x -q --no-header ndor/bin/git (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI linked an issue Mar 27, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix import error with lxml version 5.2.1 Fix ImportError with lxml==5.2.1 (Cython 3.0) Mar 27, 2026
Copilot AI requested a review from medley56 March 27, 2026 23:44
@medley56 medley56 marked this pull request as ready for review March 30, 2026 21:36
Copilot AI review requested due to automatic review settings March 30, 2026 21:36
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.82%. Comparing base (f8dcb04) to head (916a976).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #243   +/-   ##
=======================================
  Coverage   94.81%   94.82%           
=======================================
  Files          48       48           
  Lines        3861     3868    +7     
=======================================
+ Hits         3661     3668    +7     
  Misses        200      200           

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes import-time failures when using lxml==5.2.1 (Cython 3), where lxml.etree.ElementTree/Element are compiled as Cython functions, breaking |-style union annotations and isinstance() checks.

Changes:

  • Added from __future__ import annotations to defer type annotation evaluation in affected modules.
  • Updated validate_xtce() to use ElementTree._ElementTree for isinstance() checks (works across supported lxml versions).
  • Removed now-redundant quoted return type annotations (e.g., -> "ParameterType").

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
space_packet_parser/common.py Defers annotations to avoid import-time evaluation issues involving lxml element types; removes redundant quoted annotations.
space_packet_parser/xtce/calibrators.py Defers annotations and removes redundant quoted annotations in from_xml return types.
space_packet_parser/xtce/comparisons.py Defers annotations and removes redundant quoted annotations in from_xml return types.
space_packet_parser/xtce/encodings.py Defers annotations and removes redundant quoted annotations in from_xml return types.
space_packet_parser/xtce/parameter_types.py Defers annotations and removes redundant quoted annotations in from_xml return types.
space_packet_parser/xtce/parameters.py Defers annotations and removes redundant quoted annotations in from_xml return types.
space_packet_parser/xtce/validation.py Fixes isinstance() against ElementTree._ElementTree to avoid TypeError under lxml==5.2.1.

@medley56 medley56 merged commit df90741 into main Mar 31, 2026
25 checks passed
@medley56 medley56 deleted the copilot/fix-import-error-lxml-5-2-1 branch March 31, 2026 17:31
medley56 added a commit that referenced this pull request Mar 31, 2026
* Initial plan

* Fix import error with lxml==5.2.1 (Cython 3.0)

Agent-Logs-Url: https://github.com/lasp/space_packet_parser/sessions/7c56f61f-469d-40ba-abd9-96a2e2a1e2c2

Co-authored-by: medley56 <7018964+medley56@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: medley56 <7018964+medley56@users.noreply.github.com>
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.

Import Error with lxml==5.2.1

3 participants