[ruff]: Make ruff analyze graph work with jupyter notebooks
#21161
+164
−14
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.
Summary
ruff analyze graphskips jupyter notebooks. This MR addresses that issue so that jupyter notebooks are also considered in the output.Closes: #21099
Detailed Summary
ruff analyze graphhas the following lines to skip if the source is a jupyter notebook.ruff/crates/ruff/src/commands/analyze_graph.rs
Lines 130 to 133 in d9cab4d
Removing these lines alone is not enough (see this comment on the issue). The reason is because in
detect()the file under consideration is read and parsed to be a.pyfile.ruff/crates/ruff_graph/src/lib.rs
Lines 25 to 33 in d9cab4d
For notebooks, you'd expect this parsing to fail as a
.ipynband.pyare different. However that is not the case as under the hood, contents of a jupyter notebook are JSON. What instead is happening is that the jupyter notebooks get parsed asa giant nested python dictionary.
Fix
detect()now takes two more arguments;source: the raw content andsource_typeto correctly parse both.pyand.ipynbcontent.analyze_graph.rswe extract the raw content correctly for both.pyand.ipynb.Test Plan
All existing unit tests pass and added two new tests to make sure the jupyter notebook flow also works.