This is an automated email from the ASF dual-hosted git repository.

amoghdesai pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 12c317b8c50 sphinx: avoid repeated isinstance checks (#53367)
12c317b8c50 is described below

commit 12c317b8c509461eacab6bc4fb7e9e2c96243abb
Author: Dev-iL <[email protected]>
AuthorDate: Tue Jul 15 17:33:49 2025 +0300

    sphinx: avoid repeated isinstance checks (#53367)
    
    Trying to fix failing mypy devel-common
---
 .../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 699dbffa30f..16ae5d128e5 100644
--- a/devel-common/src/sphinx_exts/substitution_extensions.py
+++ b/devel-common/src/sphinx_exts/substitution_extensions.py
@@ -61,7 +61,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
@@ -74,15 +75,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]]:

Reply via email to