According to https://docs.python.org/3/library/ast.html#ast-helpers, we expect type_ignores to be always be empty, so this adds a debug assert. Test plan: I confirmed that the assertion holdes for the file below and for all the black tests which include a number of `type: ignore` comments. ```python # type: ignore if 1: print("1") # type: ignore # elsebranch # type: ignore else: # type: ignore print("2") # type: ignore while 1: print() # type: ignore ```
29 lines
875 B
Rust
29 lines
875 B
Rust
use crate::statement::suite::SuiteLevel;
|
|
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
|
use ruff_formatter::prelude::hard_line_break;
|
|
use ruff_formatter::{write, Buffer, FormatResult};
|
|
use rustpython_parser::ast::ModModule;
|
|
|
|
#[derive(Default)]
|
|
pub struct FormatModModule;
|
|
|
|
impl FormatNodeRule<ModModule> for FormatModModule {
|
|
fn fmt_fields(&self, item: &ModModule, f: &mut PyFormatter) -> FormatResult<()> {
|
|
let ModModule {
|
|
range: _,
|
|
body,
|
|
type_ignores,
|
|
} = item;
|
|
// https://docs.python.org/3/library/ast.html#ast-helpers
|
|
debug_assert!(type_ignores.is_empty());
|
|
write!(
|
|
f,
|
|
[
|
|
body.format().with_options(SuiteLevel::TopLevel),
|
|
// Trailing newline at the end of the file
|
|
hard_line_break()
|
|
]
|
|
)
|
|
}
|
|
}
|