This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch v3-0-test
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/v3-0-test by this push:
new be1cd2f2d07 sphinx: avoid repeated isinstance checks (#53367) (#53783)
be1cd2f2d07 is described below
commit be1cd2f2d07edd541c5a439f6453e6e72e1fce65
Author: GPK <[email protected]>
AuthorDate: Sat Jul 26 11:28:43 2025 +0100
sphinx: avoid repeated isinstance checks (#53367) (#53783)
Trying to fix failing mypy devel-common
Co-authored-by: Dev-iL <[email protected]>
---
.../src/sphinx_exts/substitution_extensions.py | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/devel-common/src/sphinx_exts/substitution_extensions.py
b/devel-common/src/sphinx_exts/substitution_extensions.py
index 6009186c9d8..730d97ffd64 100644
--- a/devel-common/src/sphinx_exts/substitution_extensions.py
+++ b/devel-common/src/sphinx_exts/substitution_extensions.py
@@ -63,7 +63,8 @@ class SubstitutionCodeBlockTransform(SphinxTransform):
return isinstance(node, (nodes.literal_block, nodes.literal))
for node in self.document.traverse(condition):
- if not node.get(_SUBSTITUTION_OPTION_NAME):
+ # Guard: Only process Element nodes with a truthy substitution
option
+ if not (isinstance(node, nodes.Element) and
node.attributes.get(_SUBSTITUTION_OPTION_NAME)):
continue
# Some nodes don't have a direct document property, so walk up
until we find it
@@ -76,15 +77,20 @@ class SubstitutionCodeBlockTransform(SphinxTransform):
substitution_defs = document.substitution_defs
for child in node.children:
old_child = child
- for name, value in substitution_defs.items():
- replacement = value.astext()
- if isinstance(child, nodes.Text):
- child = nodes.Text(child.replace(f"|{name}|",
replacement))
- if isinstance(node, nodes.Element):
- node.replace(old_child, child)
+ # Only substitute for Text nodes
+ if isinstance(child, nodes.Text):
+ new_text = str(child)
+ for name, value in substitution_defs.items():
+ replacement = value.astext()
+ new_text = new_text.replace(f"|{name}|", replacement)
+ # Only replace if the text actually changed
+ if new_text != str(child):
+ child = nodes.Text(new_text)
+ node.replace(old_child, child)
+ # For non-Text nodes, do not replace
# The highlighter checks this -- without this, it will refuse to
apply highlighting
- node.rawsource = node.astext() # type: ignore[attr-defined]
+ node.rawsource = node.astext()
def substitution_code_role(*args, **kwargs) -> tuple[list, list[Any]]: