From 12f63b3a0425841f2bdcda3f08e656453ea59297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 8 May 2024 14:38:35 +0200 Subject: [PATCH] Move `defusedxml` import into the function using it Import `defusedxml` inside the `etree_parse()` function rather than in global scope of `sphinx.testing.util`. This makes it possible for reverse dependencies (such as `breathe`) to use this module without adding an unnecessary transitive dependency on `defusedxml` when it's not actually used. See also issue #12339. --- sphinx/testing/util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sphinx/testing/util.py b/sphinx/testing/util.py index d1de8ea2b74..b2df709eea8 100644 --- a/sphinx/testing/util.py +++ b/sphinx/testing/util.py @@ -11,7 +11,6 @@ from types import MappingProxyType from typing import TYPE_CHECKING -from defusedxml.ElementTree import parse as xml_parse from docutils import nodes from docutils.parsers.rst import directives, roles @@ -73,6 +72,8 @@ def assert_node(node: Node, cls: Any = None, xpath: str = "", **kwargs: Any) -> # keep this to restrict the API usage and to have a correct return type def etree_parse(path: str | os.PathLike[str]) -> ElementTree: """Parse a file into a (safe) XML element tree.""" + from defusedxml.ElementTree import parse as xml_parse + return xml_parse(path)