Skip to content

Commit 4ad5903

Browse files
authored
Delete type-ignore node
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary This PR removes the type ignore node from the AST because our parser doesn't support it, and just having it around is confusing. <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan `cargo build` <!-- How was it tested? -->
1 parent c6986ac commit 4ad5903

File tree

12 files changed

+13
-391
lines changed

12 files changed

+13
-391
lines changed

crates/ruff_python_ast/src/node.rs

Lines changed: 2 additions & 259 deletions
Large diffs are not rendered by default.

crates/ruff_python_ast/src/nodes.rs

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub enum Mod {
2020
pub struct ModModule {
2121
pub range: TextRange,
2222
pub body: Vec<Stmt>,
23-
pub type_ignores: Vec<TypeIgnore>,
2423
}
2524

2625
impl From<ModModule> for Mod {
@@ -2037,26 +2036,6 @@ impl From<PatternMatchOr> for Pattern {
20372036
}
20382037
}
20392038

2040-
/// See also [type_ignore](https://docs.python.org/3/library/ast.html#ast.type_ignore)
2041-
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
2042-
pub enum TypeIgnore {
2043-
TypeIgnore(TypeIgnoreTypeIgnore),
2044-
}
2045-
2046-
/// See also [TypeIgnore](https://docs.python.org/3/library/ast.html#ast.TypeIgnore)
2047-
#[derive(Clone, Debug, PartialEq)]
2048-
pub struct TypeIgnoreTypeIgnore {
2049-
pub range: TextRange,
2050-
pub lineno: Int,
2051-
pub tag: String,
2052-
}
2053-
2054-
impl From<TypeIgnoreTypeIgnore> for TypeIgnore {
2055-
fn from(payload: TypeIgnoreTypeIgnore) -> Self {
2056-
TypeIgnore::TypeIgnore(payload)
2057-
}
2058-
}
2059-
20602039
/// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)
20612040
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
20622041
pub enum TypeParam {
@@ -2993,18 +2972,6 @@ impl Ranged for crate::Pattern {
29932972
}
29942973
}
29952974

2996-
impl Ranged for crate::nodes::TypeIgnoreTypeIgnore {
2997-
fn range(&self) -> TextRange {
2998-
self.range
2999-
}
3000-
}
3001-
impl Ranged for crate::TypeIgnore {
3002-
fn range(&self) -> TextRange {
3003-
match self {
3004-
Self::TypeIgnore(node) => node.range(),
3005-
}
3006-
}
3007-
}
30082975
impl Ranged for crate::nodes::TypeParamTypeVar {
30092976
fn range(&self) -> TextRange {
30102977
self.range
@@ -3055,5 +3022,5 @@ mod size_assertions {
30553022
assert_eq_size!(Expr, [u8; 80]);
30563023
assert_eq_size!(Constant, [u8; 32]);
30573024
assert_eq_size!(Pattern, [u8; 96]);
3058-
assert_eq_size!(Mod, [u8; 64]);
3025+
assert_eq_size!(Mod, [u8; 48]);
30593026
}

crates/ruff_python_ast/src/visitor/preorder.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
self as ast, Alias, Arg, ArgWithDefault, Arguments, BoolOp, CmpOp, Comprehension, Constant,
33
Decorator, ElifElseClause, ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Pattern,
4-
Stmt, TypeIgnore, TypeParam, TypeParamTypeVar, UnaryOp, WithItem,
4+
Stmt, TypeParam, TypeParamTypeVar, UnaryOp, WithItem,
55
};
66

77
/// Visitor that traverses all nodes recursively in pre-order.
@@ -96,10 +96,6 @@ pub trait PreorderVisitor<'a> {
9696
walk_body(self, body);
9797
}
9898

99-
fn visit_type_ignore(&mut self, type_ignore: &'a TypeIgnore) {
100-
walk_type_ignore(self, type_ignore);
101-
}
102-
10399
fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause) {
104100
walk_elif_else_clause(self, elif_else_clause);
105101
}
@@ -110,15 +106,8 @@ where
110106
V: PreorderVisitor<'a> + ?Sized,
111107
{
112108
match module {
113-
Mod::Module(ast::ModModule {
114-
body,
115-
range: _,
116-
type_ignores,
117-
}) => {
109+
Mod::Module(ast::ModModule { body, range: _ }) => {
118110
visitor.visit_body(body);
119-
for ignore in type_ignores {
120-
visitor.visit_type_ignore(ignore);
121-
}
122111
}
123112
Mod::Interactive(ast::ModInteractive { body, range: _ }) => visitor.visit_body(body),
124113
Mod::Expression(ast::ModExpression { body, range: _ }) => visitor.visit_expr(body),
@@ -941,13 +930,6 @@ where
941930
}
942931
}
943932

944-
#[inline]
945-
pub fn walk_type_ignore<'a, V>(_visitor: &mut V, _type_ignore: &'a TypeIgnore)
946-
where
947-
V: PreorderVisitor<'a> + ?Sized,
948-
{
949-
}
950-
951933
pub fn walk_bool_op<'a, V>(_visitor: &mut V, _bool_op: &'a BoolOp)
952934
where
953935
V: PreorderVisitor<'a> + ?Sized,

crates/ruff_python_ast/tests/preorder.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use insta::assert_snapshot;
55
use ruff_python_ast::node::AnyNodeRef;
66
use ruff_python_ast::visitor::preorder::{
77
walk_alias, walk_arg, walk_arguments, walk_comprehension, walk_except_handler, walk_expr,
8-
walk_keyword, walk_match_case, walk_module, walk_pattern, walk_stmt, walk_type_ignore,
9-
walk_type_param, walk_with_item, PreorderVisitor,
8+
walk_keyword, walk_match_case, walk_module, walk_pattern, walk_stmt, walk_type_param,
9+
walk_with_item, PreorderVisitor,
1010
};
1111
use ruff_python_ast::{
1212
Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant, ExceptHandler, Expr, Keyword,
13-
MatchCase, Mod, Operator, Pattern, Stmt, TypeIgnore, TypeParam, UnaryOp, WithItem,
13+
MatchCase, Mod, Operator, Pattern, Stmt, TypeParam, UnaryOp, WithItem,
1414
};
1515
use ruff_python_parser::lexer::lex;
1616
use ruff_python_parser::{parse_tokens, Mode};
@@ -273,12 +273,6 @@ impl PreorderVisitor<'_> for RecordVisitor {
273273
self.exit_node();
274274
}
275275

276-
fn visit_type_ignore(&mut self, type_ignore: &TypeIgnore) {
277-
self.enter_node(type_ignore);
278-
walk_type_ignore(self, type_ignore);
279-
self.exit_node();
280-
}
281-
282276
fn visit_type_param(&mut self, type_param: &TypeParam) {
283277
self.enter_node(type_param);
284278
walk_type_param(self, type_param);

crates/ruff_python_ast/tests/visitor.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ where
144144
V: Visitor<'a> + ?Sized,
145145
{
146146
match module {
147-
ast::Mod::Module(ast::ModModule {
148-
body,
149-
range: _,
150-
type_ignores: _,
151-
}) => {
147+
ast::Mod::Module(ast::ModModule { body, range: _ }) => {
152148
visitor.visit_body(body);
153149
}
154150
ast::Mod::Interactive(ast::ModInteractive { body, range: _ }) => {

crates/ruff_python_formatter/src/generated.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,46 +2657,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::PatternMatchOr {
26572657
}
26582658
}
26592659

2660-
impl FormatRule<ast::TypeIgnoreTypeIgnore, PyFormatContext<'_>>
2661-
for crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore
2662-
{
2663-
#[inline]
2664-
fn fmt(
2665-
&self,
2666-
node: &ast::TypeIgnoreTypeIgnore,
2667-
f: &mut Formatter<PyFormatContext<'_>>,
2668-
) -> FormatResult<()> {
2669-
FormatNodeRule::<ast::TypeIgnoreTypeIgnore>::fmt(self, node, f)
2670-
}
2671-
}
2672-
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeIgnoreTypeIgnore {
2673-
type Format<'a> = FormatRefWithRule<
2674-
'a,
2675-
ast::TypeIgnoreTypeIgnore,
2676-
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore,
2677-
PyFormatContext<'ast>,
2678-
>;
2679-
fn format(&self) -> Self::Format<'_> {
2680-
FormatRefWithRule::new(
2681-
self,
2682-
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore::default(),
2683-
)
2684-
}
2685-
}
2686-
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeIgnoreTypeIgnore {
2687-
type Format = FormatOwnedWithRule<
2688-
ast::TypeIgnoreTypeIgnore,
2689-
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore,
2690-
PyFormatContext<'ast>,
2691-
>;
2692-
fn into_format(self) -> Self::Format {
2693-
FormatOwnedWithRule::new(
2694-
self,
2695-
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore::default(),
2696-
)
2697-
}
2698-
}
2699-
27002660
impl FormatRule<ast::Comprehension, PyFormatContext<'_>>
27012661
for crate::other::comprehension::FormatComprehension
27022662
{

crates/ruff_python_formatter/src/module/mod_module.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ pub struct FormatModModule;
99

1010
impl FormatNodeRule<ModModule> for FormatModModule {
1111
fn fmt_fields(&self, item: &ModModule, f: &mut PyFormatter) -> FormatResult<()> {
12-
let ModModule {
13-
range: _,
14-
body,
15-
type_ignores,
16-
} = item;
17-
// https://docs.python.org/3/library/ast.html#ast-helpers
18-
debug_assert!(type_ignores.is_empty());
12+
let ModModule { range: _, body } = item;
1913
write!(
2014
f,
2115
[

crates/ruff_python_formatter/src/other/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ pub(crate) mod except_handler_except_handler;
99
pub(crate) mod identifier;
1010
pub(crate) mod keyword;
1111
pub(crate) mod match_case;
12-
pub(crate) mod type_ignore_type_ignore;
1312
pub(crate) mod with_item;

crates/ruff_python_formatter/src/other/type_ignore_type_ignore.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

crates/ruff_python_parser/src/python.lalrpop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ grammar(mode: Mode);
2121
// For each public entry point, a full parse table is generated.
2222
// By having only a single pub function, we reduce this to one.
2323
pub(crate) Top: ast::Mod = {
24-
<start:@L> StartModule <body:Program> <end:@R> => ast::ModModule { body, type_ignores: vec![], range: (start..end).into() }.into(),
24+
<start:@L> StartModule <body:Program> <end:@R> => ast::ModModule { body, range: (start..end).into() }.into(),
2525
<start:@L> StartInteractive <body:Program> <end:@R> => ast::ModInteractive { body, range: (start..end).into() }.into(),
2626
<start:@L> StartExpression <body:TestList> ("\n")* <end:@R> => ast::ModExpression { body: Box::new(body), range: (start..end).into() }.into()
2727
};

crates/ruff_python_parser/src/python.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// auto-generated: "lalrpop 0.20.0"
2-
// sha3: bf0ea34f78939474a89bc0d4b6e7c14f370a2d2cd2ca8b98bd5aefdae0e1d5f1
2+
// sha3: 76f8cd8ac95bef60488dc5962346273abca535cd4aa194edd11cda998a4b211e
33
use num_bigint::BigInt;
44
use ruff_text_size::TextSize;
55
use ruff_python_ast::{self as ast, Ranged, MagicKind};
@@ -30770,7 +30770,7 @@ fn __action1<
3077030770
(_, end, _): (TextSize, TextSize, TextSize),
3077130771
) -> ast::Mod
3077230772
{
30773-
ast::ModModule { body, type_ignores: vec![], range: (start..end).into() }.into()
30773+
ast::ModModule { body, range: (start..end).into() }.into()
3077430774
}
3077530775

3077630776
#[allow(unused_variables)]

crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__jupyter_magic.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,5 @@ Module(
347347
},
348348
),
349349
],
350-
type_ignores: [],
351350
},
352351
)

0 commit comments

Comments
 (0)