Use source type to determine parser mode for formatting (#8205)

## Summary

This PR fixes the bug where if a Notebook contained IPython syntax, then
the format command would fail. This was because the correct mode was not
being used while parsing through the formatter code path.

## Test Plan

This PR isn't the only requirement for Notebook formatting to start
working with IPython escape commands. The following PR in the stack is
required as well.
This commit is contained in:
Dhruv Manilawala 2023-10-25 19:20:02 +05:30 committed by GitHub
parent 31032f4f70
commit c2ec5f0bc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 17 deletions

View file

@ -548,10 +548,10 @@ mod tests {
use insta::assert_debug_snapshot;
use ruff_formatter::SourceCode;
use ruff_python_ast::Mod;
use ruff_python_ast::{Mod, PySourceType};
use ruff_python_index::tokens_and_ranges;
use ruff_python_parser::{parse_ok_tokens, Mode};
use ruff_python_parser::{parse_ok_tokens, AsMode};
use ruff_python_trivia::CommentRanges;
use crate::comments::Comments;
@ -565,9 +565,10 @@ mod tests {
impl<'a> CommentsTestCase<'a> {
fn from_code(source: &'a str) -> Self {
let source_code = SourceCode::new(source);
let source_type = PySourceType::Python;
let (tokens, comment_ranges) =
tokens_and_ranges(source).expect("Expect source to be valid Python");
let parsed = parse_ok_tokens(tokens, source, Mode::Module, "test.py")
tokens_and_ranges(source, source_type).expect("Expect source to be valid Python");
let parsed = parse_ok_tokens(tokens, source, source_type.as_mode(), "test.py")
.expect("Expect source to be valid Python");
CommentsTestCase {