Skip to content

Commit

Permalink
Delete type-ignore node
Browse files Browse the repository at this point in the history
<!--
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? -->
  • Loading branch information
MichaReiser committed Aug 1, 2023
1 parent c6986ac commit 4ad5903
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 391 deletions.
261 changes: 2 additions & 259 deletions crates/ruff_python_ast/src/node.rs

Large diffs are not rendered by default.

35 changes: 1 addition & 34 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum Mod {
pub struct ModModule {
pub range: TextRange,
pub body: Vec<Stmt>,
pub type_ignores: Vec<TypeIgnore>,
}

impl From<ModModule> for Mod {
Expand Down Expand Up @@ -2037,26 +2036,6 @@ impl From<PatternMatchOr> for Pattern {
}
}

/// See also [type_ignore](https://docs.python.org/3/library/ast.html#ast.type_ignore)
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum TypeIgnore {
TypeIgnore(TypeIgnoreTypeIgnore),
}

/// See also [TypeIgnore](https://docs.python.org/3/library/ast.html#ast.TypeIgnore)
#[derive(Clone, Debug, PartialEq)]
pub struct TypeIgnoreTypeIgnore {
pub range: TextRange,
pub lineno: Int,
pub tag: String,
}

impl From<TypeIgnoreTypeIgnore> for TypeIgnore {
fn from(payload: TypeIgnoreTypeIgnore) -> Self {
TypeIgnore::TypeIgnore(payload)
}
}

/// See also [type_param](https://docs.python.org/3/library/ast.html#ast.type_param)
#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum TypeParam {
Expand Down Expand Up @@ -2993,18 +2972,6 @@ impl Ranged for crate::Pattern {
}
}

impl Ranged for crate::nodes::TypeIgnoreTypeIgnore {
fn range(&self) -> TextRange {
self.range
}
}
impl Ranged for crate::TypeIgnore {
fn range(&self) -> TextRange {
match self {
Self::TypeIgnore(node) => node.range(),
}
}
}
impl Ranged for crate::nodes::TypeParamTypeVar {
fn range(&self) -> TextRange {
self.range
Expand Down Expand Up @@ -3055,5 +3022,5 @@ mod size_assertions {
assert_eq_size!(Expr, [u8; 80]);
assert_eq_size!(Constant, [u8; 32]);
assert_eq_size!(Pattern, [u8; 96]);
assert_eq_size!(Mod, [u8; 64]);
assert_eq_size!(Mod, [u8; 48]);
}
22 changes: 2 additions & 20 deletions crates/ruff_python_ast/src/visitor/preorder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
self as ast, Alias, Arg, ArgWithDefault, Arguments, BoolOp, CmpOp, Comprehension, Constant,
Decorator, ElifElseClause, ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator, Pattern,
Stmt, TypeIgnore, TypeParam, TypeParamTypeVar, UnaryOp, WithItem,
Stmt, TypeParam, TypeParamTypeVar, UnaryOp, WithItem,
};

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

fn visit_type_ignore(&mut self, type_ignore: &'a TypeIgnore) {
walk_type_ignore(self, type_ignore);
}

fn visit_elif_else_clause(&mut self, elif_else_clause: &'a ElifElseClause) {
walk_elif_else_clause(self, elif_else_clause);
}
Expand All @@ -110,15 +106,8 @@ where
V: PreorderVisitor<'a> + ?Sized,
{
match module {
Mod::Module(ast::ModModule {
body,
range: _,
type_ignores,
}) => {
Mod::Module(ast::ModModule { body, range: _ }) => {
visitor.visit_body(body);
for ignore in type_ignores {
visitor.visit_type_ignore(ignore);
}
}
Mod::Interactive(ast::ModInteractive { body, range: _ }) => visitor.visit_body(body),
Mod::Expression(ast::ModExpression { body, range: _ }) => visitor.visit_expr(body),
Expand Down Expand Up @@ -941,13 +930,6 @@ where
}
}

#[inline]
pub fn walk_type_ignore<'a, V>(_visitor: &mut V, _type_ignore: &'a TypeIgnore)
where
V: PreorderVisitor<'a> + ?Sized,
{
}

pub fn walk_bool_op<'a, V>(_visitor: &mut V, _bool_op: &'a BoolOp)
where
V: PreorderVisitor<'a> + ?Sized,
Expand Down
12 changes: 3 additions & 9 deletions crates/ruff_python_ast/tests/preorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use insta::assert_snapshot;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::visitor::preorder::{
walk_alias, walk_arg, walk_arguments, walk_comprehension, walk_except_handler, walk_expr,
walk_keyword, walk_match_case, walk_module, walk_pattern, walk_stmt, walk_type_ignore,
walk_type_param, walk_with_item, PreorderVisitor,
walk_keyword, walk_match_case, walk_module, walk_pattern, walk_stmt, walk_type_param,
walk_with_item, PreorderVisitor,
};
use ruff_python_ast::{
Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Constant, ExceptHandler, Expr, Keyword,
MatchCase, Mod, Operator, Pattern, Stmt, TypeIgnore, TypeParam, UnaryOp, WithItem,
MatchCase, Mod, Operator, Pattern, Stmt, TypeParam, UnaryOp, WithItem,
};
use ruff_python_parser::lexer::lex;
use ruff_python_parser::{parse_tokens, Mode};
Expand Down Expand Up @@ -273,12 +273,6 @@ impl PreorderVisitor<'_> for RecordVisitor {
self.exit_node();
}

fn visit_type_ignore(&mut self, type_ignore: &TypeIgnore) {
self.enter_node(type_ignore);
walk_type_ignore(self, type_ignore);
self.exit_node();
}

fn visit_type_param(&mut self, type_param: &TypeParam) {
self.enter_node(type_param);
walk_type_param(self, type_param);
Expand Down
6 changes: 1 addition & 5 deletions crates/ruff_python_ast/tests/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ where
V: Visitor<'a> + ?Sized,
{
match module {
ast::Mod::Module(ast::ModModule {
body,
range: _,
type_ignores: _,
}) => {
ast::Mod::Module(ast::ModModule { body, range: _ }) => {
visitor.visit_body(body);
}
ast::Mod::Interactive(ast::ModInteractive { body, range: _ }) => {
Expand Down
40 changes: 0 additions & 40 deletions crates/ruff_python_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2657,46 +2657,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::PatternMatchOr {
}
}

impl FormatRule<ast::TypeIgnoreTypeIgnore, PyFormatContext<'_>>
for crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore
{
#[inline]
fn fmt(
&self,
node: &ast::TypeIgnoreTypeIgnore,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::TypeIgnoreTypeIgnore>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::TypeIgnoreTypeIgnore {
type Format<'a> = FormatRefWithRule<
'a,
ast::TypeIgnoreTypeIgnore,
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::TypeIgnoreTypeIgnore {
type Format = FormatOwnedWithRule<
ast::TypeIgnoreTypeIgnore,
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::other::type_ignore_type_ignore::FormatTypeIgnoreTypeIgnore::default(),
)
}
}

impl FormatRule<ast::Comprehension, PyFormatContext<'_>>
for crate::other::comprehension::FormatComprehension
{
Expand Down
8 changes: 1 addition & 7 deletions crates/ruff_python_formatter/src/module/mod_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ 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());
let ModModule { range: _, body } = item;
write!(
f,
[
Expand Down
1 change: 0 additions & 1 deletion crates/ruff_python_formatter/src/other/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ pub(crate) mod except_handler_except_handler;
pub(crate) mod identifier;
pub(crate) mod keyword;
pub(crate) mod match_case;
pub(crate) mod type_ignore_type_ignore;
pub(crate) mod with_item;
12 changes: 0 additions & 12 deletions crates/ruff_python_formatter/src/other/type_ignore_type_ignore.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/ruff_python_parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ grammar(mode: Mode);
// For each public entry point, a full parse table is generated.
// By having only a single pub function, we reduce this to one.
pub(crate) Top: ast::Mod = {
<start:@L> StartModule <body:Program> <end:@R> => ast::ModModule { body, type_ignores: vec![], range: (start..end).into() }.into(),
<start:@L> StartModule <body:Program> <end:@R> => ast::ModModule { body, range: (start..end).into() }.into(),
<start:@L> StartInteractive <body:Program> <end:@R> => ast::ModInteractive { body, range: (start..end).into() }.into(),
<start:@L> StartExpression <body:TestList> ("\n")* <end:@R> => ast::ModExpression { body: Box::new(body), range: (start..end).into() }.into()
};
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_parser/src/python.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: bf0ea34f78939474a89bc0d4b6e7c14f370a2d2cd2ca8b98bd5aefdae0e1d5f1
// sha3: 76f8cd8ac95bef60488dc5962346273abca535cd4aa194edd11cda998a4b211e
use num_bigint::BigInt;
use ruff_text_size::TextSize;
use ruff_python_ast::{self as ast, Ranged, MagicKind};
Expand Down Expand Up @@ -30770,7 +30770,7 @@ fn __action1<
(_, end, _): (TextSize, TextSize, TextSize),
) -> ast::Mod
{
ast::ModModule { body, type_ignores: vec![], range: (start..end).into() }.into()
ast::ModModule { body, range: (start..end).into() }.into()
}

#[allow(unused_variables)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,5 @@ Module(
},
),
],
type_ignores: [],
},
)

0 comments on commit 4ad5903

Please sign in to comment.