-
Notifications
You must be signed in to change notification settings - Fork 16.3k
sphinx: avoid repeated isinstance checks #53367
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
Conversation
3d30202 to
8bb623b
Compare
There was a problem hiding this 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 refactors the substitution extension to perform isinstance checks at an earlier scope, reduces redundant branching, and removes a type ignore on the rawsource assignment.
- Move
isinstancecheck fornodes.Elementbefore attribute lookup - Consolidate per-child
isinstance(nodes.Text)logic - Remove
# type: ignore[attr-defined]onnode.rawsource
Comments suppressed due to low confidence (1)
devel-common/src/sphinx_exts/substitution_extensions.py:85
- [nitpick] The removal of the type ignore on
rawsourceassignment changes behavior for some node types. Add tests to verify thatrawsourceis correctly set for all relevant nodes to prevent regressions in highlighting.
node.rawsource = node.astext()
8bb623b to
5672f32
Compare
amoghrajesh
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, looks good! Let's wait for CI
|
Nice... I've been fighting with it in my Python 3.13 PR as well :D |
Trying to fix failing mypy devel-common
This is a different fix for the same issue addressed in #53364, which was developed in parallel.
The main improvement are:
# type: ignore[attr-defined]isinstancebranching happens at an earlier scope (so fewer times overall).Also (according to copilot):
isinstance(node, nodes.Element), which is correct because onlyElementnodes have.attributes,.children, and.rawsource.node.get(...)and then checksisinstance(node, nodes.Element)later, which is less clear and could allow non-Elementnodes to be processed incorrectly.Elementnodes with the substitution option are processed, matching thedocutilsnode model.Element, leading to possible attribute errors or incorrect behavior.