Build a Graphtage tree from plain import statements - #165
Merged
Merged
Conversation
ASTBuilder registered an expander and a builder for ast.ImportFrom but nothing for ast.Import, so ast_to_tree raised NotImplementedError on any module containing a plain `import x` statement. Because `import x` is far more common than `from y import x`, that left most real Python sources undiffable. Register ast.Import on the existing alias expander, which both node types expose as `.names`, and add a builder that constructs the same graphtage.ast.Import node with an empty from_name. Both the node and PyImportFormatter already treat an empty from_name as the plain form, so no change to the printing side is needed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GypKU5KdLfs2Cf8kS2TzJa
# Conflicts: # test/test_pydiff.py
# Conflicts: # test/test_pydiff.py
# Conflicts: # test/test_pydiff.py
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.
Closes #151
ASTBuilderregistered an expander and a builder forast.ImportFrombut nothing forast.Import, soast_to_tree(ast.parse("import os"))raisedNotImplementedError. Sinceimport xis far more common thanfrom y import x, most real Python sources could not be diffed at all.Both
ast.Importandast.ImportFromexpose their aliases as.names, soast.Importjoins the existingexpander. The new builder produces the same
graphtage.ast.Importnode with an emptyfrom_name; the node'sprintandPyImportFormatter.print_Importalready treat an emptyfrom_nameas the plainimport xform, sonothing on the printing side changes.
The expander was renamed from
expand_import_fromtoexpand_importbecause it now handles both statement forms.Builder registration is decorator-based, so the name is not part of the dispatch.
Validation
The regression tests were run against the unfixed builder first. All seven subtests failed with
NotImplementedError: A builder for type Import is not defined for object Import(names=[alias(name='os', ...)]),and all pass with the fix.
test_plain_import_buildscoversimport os,import os.path,import os as o, andimport os, sys;test_plain_import_printingchecks the rendered statement and keepsfrom os import pathas acontrol. Both tests work at the tree-construction and unedited-printing level, so they do not depend on the
separate diff-rendering problem in #150.
ruff check graphtage test docs bindist: passespytest: 142 passed, 8 subtests passedmake -C docs html SPHINXOPTS="-W --keep-going": build succeededuv lock --check: fails identically on an unmodified checkout because of a localexclude-newersetting; nodependency changed here
Out of scope
Most other statement and expression node types still have no builder, including
FunctionDef,ClassDef,If,For,While,With,Try,BinOp,Compare,AugAssign,AnnAssign,Lambda, the comprehensions,Starred,Raise,Assert,Delete,Pass,JoinedStr,Slice,Match, and theAsync*variants. Aliasasnamevalues also render quoted (from os import path as "p"), which predates this change and affectsImportFromtoo.🤖 Generated with Claude Code
https://claude.ai/code/session_01GypKU5KdLfs2Cf8kS2TzJa