Use native AST positions in _parse, drop source-order traversal#535
Merged
Conversation
Since Python 3.8 the built-in parser reports correct lineno/col_offset (and end_lineno/end_col_offset) for every node, including multiline string literals, which historically misreported their position as the ending line with col_offset == -1. That made a large chunk of _parse.py obsolete: - Replace the recursive _annotate_ast_startpos / _annotate_ast_nodes (which re-derived positions to work around the old quirk) with a flat ast.walk that reads native positions directly, keeping the decorator-start special case. - Delete _flatten_ast_nodes, _iter_child_nodes_in_order, _iter_child_nodes_in_order_internal_1 and _walk_ast_nodes_in_order, including the per-Python-version _fields asserts that broke on each new release. - Make string_literals / _get_docstring_nodes sort by native startpos instead of relying on the hand-maintained source-order traversal. - Remove the dead endpos branch in _split_code_lines (.endpos is never set on AST nodes) and the now-unused imports / AstNodeContext. - _autoimp: import MatchAs from ast directly rather than re-exporting it through _parse. Net -335 lines in _parse.py with no behavior change on Python >=3.12 (the only version-specific path, the <3.12 f-string clamp, never ran there). All parse/import/autoimp suites pass.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #535 +/- ##
==========================================
+ Coverage 89.92% 89.97% +0.04%
==========================================
Files 57 57
Lines 17694 17580 -114
==========================================
- Hits 15912 15818 -94
+ Misses 1782 1762 -20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since Python 3.8 the built-in parser reports correct lineno/col_offset (and end_lineno/end_col_offset) for every node, including multiline string literals, which historically misreported their position as the ending line with col_offset == -1. That made a large chunk of _parse.py obsolete:
Net -335 lines in _parse.py with no behavior change on Python >=3.12 (the only version-specific path, the <3.12 f-string clamp, never ran there). All parse/import/autoimp suites pass.