From 209aaa5addb98c3054f845204e64dc2c9d88aa34 Mon Sep 17 00:00:00 2001 From: konstin Date: Mon, 5 Jun 2023 11:38:08 +0200 Subject: [PATCH] Ensure type_ignores for Module are empty (#4861) 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 ``` --- crates/ruff_python_formatter/src/module/mod_module.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/ruff_python_formatter/src/module/mod_module.rs b/crates/ruff_python_formatter/src/module/mod_module.rs index fe21ed65fa..0d0fee7c00 100644 --- a/crates/ruff_python_formatter/src/module/mod_module.rs +++ b/crates/ruff_python_formatter/src/module/mod_module.rs @@ -9,10 +9,17 @@ pub struct FormatModModule; impl FormatNodeRule 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, [ - item.body.format().with_options(SuiteLevel::TopLevel), + body.format().with_options(SuiteLevel::TopLevel), // Trailing newline at the end of the file hard_line_break() ]