Skip to content

Commit

Permalink
Remove unsupported type_comment field
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_comment` field which our parser doesn't support.

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

`cargo test`

<!-- How was it tested? -->
  • Loading branch information
MichaReiser committed Aug 1, 2023
1 parent 4ad5903 commit 7c7231d
Show file tree
Hide file tree
Showing 54 changed files with 31 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fn assignment(obj: &Expr, name: &str, value: &Expr, generator: Generator) -> Str
range: TextRange::default(),
})],
value: Box::new(value.clone()),
type_comment: None,
range: TextRange::default(),
});
generator.stmt(&stmt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ fn generate_fix(
range: TextRange::default(),
})],
value: Box::new(exc_arg.clone()),
type_comment: None,
range: TextRange::default(),
});

Expand Down
2 changes: 0 additions & 2 deletions crates/ruff/src/rules/flake8_simplify/rules/ast_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,6 @@ fn ternary(target_var: &Expr, body_value: &Expr, test: &Expr, orelse_value: &Exp
let node1 = ast::StmtAssign {
targets: vec![target_var.clone()],
value: Box::new(node.into()),
type_comment: None,
range: TextRange::default(),
};
node1.into()
Expand Down Expand Up @@ -957,7 +956,6 @@ pub(crate) fn use_dict_get_with_default(checker: &mut Checker, stmt_if: &StmtIf)
let node5 = ast::StmtAssign {
targets: vec![node4],
value: Box::new(node3.into()),
type_comment: None,
range: TextRange::default(),
};
let contents = checker.generator().stmt(&node5.into());
Expand Down
2 changes: 0 additions & 2 deletions crates/ruff/src/rules/pycodestyle/rules/lambda_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ fn function(
decorator_list: vec![],
returns: Some(Box::new(return_type)),
type_params: vec![],
type_comment: None,
range: TextRange::default(),
});
return generator.stmt(&func);
Expand All @@ -242,7 +241,6 @@ fn function(
decorator_list: vec![],
returns: None,
type_params: vec![],
type_comment: None,
range: TextRange::default(),
});
generator.stmt(&func)
Expand Down
16 changes: 3 additions & 13 deletions crates/ruff/src/rules/ruff/rules/unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,24 +566,14 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
let _ = (body, handlers, orelse, finalbody); // Silence unused code warnings.
self.unconditional_next_block(after)
}
Stmt::With(StmtWith {
items,
body,
type_comment,
..
})
| Stmt::AsyncWith(StmtAsyncWith {
items,
body,
type_comment,
..
}) => {
Stmt::With(StmtWith { items, body, .. })
| Stmt::AsyncWith(StmtAsyncWith { items, body, .. }) => {
// TODO: handle `with` statements, see
// <https://docs.python.org/3/reference/compound_stmts.html#the-with-statement>.
// I recommend to `try` statements first as `with` can desugar
// to a `try` statement.
// For now we'll skip over it.
let _ = (items, body, type_comment); // Silence unused code warnings.
let _ = (items, body); // Silence unused code warnings.
self.unconditional_next_block(after)
}
Stmt::Match(StmtMatch { subject, cases, .. }) => {
Expand Down
23 changes: 0 additions & 23 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,13 @@ impl<'a> From<&'a Box<ast::Arg>> for ComparableArg<'a> {
pub struct ComparableArg<'a> {
arg: &'a str,
annotation: Option<Box<ComparableExpr<'a>>>,
type_comment: Option<&'a str>,
}

impl<'a> From<&'a ast::Arg> for ComparableArg<'a> {
fn from(arg: &'a ast::Arg) -> Self {
Self {
arg: arg.arg.as_str(),
annotation: arg.annotation.as_ref().map(Into::into),
type_comment: arg.type_comment.as_deref(),
}
}
}
Expand Down Expand Up @@ -955,7 +953,6 @@ pub struct StmtFunctionDef<'a> {
decorator_list: Vec<ComparableDecorator<'a>>,
type_params: Vec<ComparableTypeParam<'a>>,
returns: Option<ComparableExpr<'a>>,
type_comment: Option<&'a str>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand All @@ -966,7 +963,6 @@ pub struct StmtAsyncFunctionDef<'a> {
decorator_list: Vec<ComparableDecorator<'a>>,
type_params: Vec<ComparableTypeParam<'a>>,
returns: Option<ComparableExpr<'a>>,
type_comment: Option<&'a str>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -1048,7 +1044,6 @@ pub struct TypeParamTypeVarTuple<'a> {
pub struct StmtAssign<'a> {
targets: Vec<ComparableExpr<'a>>,
value: ComparableExpr<'a>,
type_comment: Option<&'a str>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand All @@ -1072,7 +1067,6 @@ pub struct StmtFor<'a> {
iter: ComparableExpr<'a>,
body: Vec<ComparableStmt<'a>>,
orelse: Vec<ComparableStmt<'a>>,
type_comment: Option<&'a str>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand All @@ -1081,7 +1075,6 @@ pub struct StmtAsyncFor<'a> {
iter: ComparableExpr<'a>,
body: Vec<ComparableStmt<'a>>,
orelse: Vec<ComparableStmt<'a>>,
type_comment: Option<&'a str>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand All @@ -1102,14 +1095,12 @@ pub struct StmtIf<'a> {
pub struct StmtWith<'a> {
items: Vec<ComparableWithItem<'a>>,
body: Vec<ComparableStmt<'a>>,
type_comment: Option<&'a str>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct StmtAsyncWith<'a> {
items: Vec<ComparableWithItem<'a>>,
body: Vec<ComparableStmt<'a>>,
type_comment: Option<&'a str>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -1221,7 +1212,6 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
body,
decorator_list,
returns,
type_comment,
type_params,
range: _range,
}) => Self::FunctionDef(StmtFunctionDef {
Expand All @@ -1230,7 +1220,6 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
body: body.iter().map(Into::into).collect(),
decorator_list: decorator_list.iter().map(Into::into).collect(),
returns: returns.as_ref().map(Into::into),
type_comment: type_comment.as_ref().map(String::as_str),
type_params: type_params.iter().map(Into::into).collect(),
}),
ast::Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
Expand All @@ -1239,7 +1228,6 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
body,
decorator_list,
returns,
type_comment,
type_params,
range: _range,
}) => Self::AsyncFunctionDef(StmtAsyncFunctionDef {
Expand All @@ -1248,7 +1236,6 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
body: body.iter().map(Into::into).collect(),
decorator_list: decorator_list.iter().map(Into::into).collect(),
returns: returns.as_ref().map(Into::into),
type_comment: type_comment.as_ref().map(String::as_str),
type_params: type_params.iter().map(Into::into).collect(),
}),
ast::Stmt::ClassDef(ast::StmtClassDef {
Expand Down Expand Up @@ -1292,12 +1279,10 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
ast::Stmt::Assign(ast::StmtAssign {
targets,
value,
type_comment,
range: _range,
}) => Self::Assign(StmtAssign {
targets: targets.iter().map(Into::into).collect(),
value: value.into(),
type_comment: type_comment.as_ref().map(String::as_str),
}),
ast::Stmt::AugAssign(ast::StmtAugAssign {
target,
Expand Down Expand Up @@ -1326,28 +1311,24 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
iter,
body,
orelse,
type_comment,
range: _range,
}) => Self::For(StmtFor {
target: target.into(),
iter: iter.into(),
body: body.iter().map(Into::into).collect(),
orelse: orelse.iter().map(Into::into).collect(),
type_comment: type_comment.as_ref().map(String::as_str),
}),
ast::Stmt::AsyncFor(ast::StmtAsyncFor {
target,
iter,
body,
orelse,
type_comment,
range: _range,
}) => Self::AsyncFor(StmtAsyncFor {
target: target.into(),
iter: iter.into(),
body: body.iter().map(Into::into).collect(),
orelse: orelse.iter().map(Into::into).collect(),
type_comment: type_comment.as_ref().map(String::as_str),
}),
ast::Stmt::While(ast::StmtWhile {
test,
Expand All @@ -1372,22 +1353,18 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
ast::Stmt::With(ast::StmtWith {
items,
body,
type_comment,
range: _range,
}) => Self::With(StmtWith {
items: items.iter().map(Into::into).collect(),
body: body.iter().map(Into::into).collect(),
type_comment: type_comment.as_ref().map(String::as_str),
}),
ast::Stmt::AsyncWith(ast::StmtAsyncWith {
items,
body,
type_comment,
range: _range,
}) => Self::AsyncWith(StmtAsyncWith {
items: items.iter().map(Into::into).collect(),
body: body.iter().map(Into::into).collect(),
type_comment: type_comment.as_ref().map(String::as_str),
}),
ast::Stmt::Match(ast::StmtMatch {
subject,
Expand Down
7 changes: 0 additions & 7 deletions crates/ruff_python_ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ impl<'a> AnyFunctionDefinition<'a> {
}
}

pub fn type_comments(self) -> Option<&'a str> {
match self {
Self::FunctionDefinition(definition) => definition.type_comment.as_deref(),
Self::AsyncFunctionDefinition(definition) => definition.type_comment.as_deref(),
}
}

/// Returns `true` if this is [`Self::AsyncFunctionDefinition`]
pub const fn is_async(self) -> bool {
matches!(self, Self::AsyncFunctionDefinition(_))
Expand Down
11 changes: 3 additions & 8 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ pub struct StmtFunctionDef {
pub decorator_list: Vec<Decorator>,
pub returns: Option<Box<Expr>>,
pub type_params: Vec<TypeParam>,
pub type_comment: Option<String>,
}

impl From<StmtFunctionDef> for Stmt {
Expand All @@ -175,7 +174,6 @@ pub struct StmtAsyncFunctionDef {
pub decorator_list: Vec<Decorator>,
pub returns: Option<Box<Expr>>,
pub type_params: Vec<TypeParam>,
pub type_comment: Option<String>,
}

impl From<StmtAsyncFunctionDef> for Stmt {
Expand Down Expand Up @@ -249,7 +247,6 @@ pub struct StmtAssign {
pub range: TextRange,
pub targets: Vec<Expr>,
pub value: Box<Expr>,
pub type_comment: Option<String>,
}

impl From<StmtAssign> for Stmt {
Expand Down Expand Up @@ -297,7 +294,6 @@ pub struct StmtFor {
pub iter: Box<Expr>,
pub body: Vec<Stmt>,
pub orelse: Vec<Stmt>,
pub type_comment: Option<String>,
}

impl From<StmtFor> for Stmt {
Expand All @@ -314,7 +310,6 @@ pub struct StmtAsyncFor {
pub iter: Box<Expr>,
pub body: Vec<Stmt>,
pub orelse: Vec<Stmt>,
pub type_comment: Option<String>,
}

impl From<StmtAsyncFor> for Stmt {
Expand Down Expand Up @@ -366,7 +361,6 @@ pub struct StmtWith {
pub range: TextRange,
pub items: Vec<WithItem>,
pub body: Vec<Stmt>,
pub type_comment: Option<String>,
}

impl From<StmtWith> for Stmt {
Expand All @@ -381,7 +375,6 @@ pub struct StmtAsyncWith {
pub range: TextRange,
pub items: Vec<WithItem>,
pub body: Vec<Stmt>,
pub type_comment: Option<String>,
}

impl From<StmtAsyncWith> for Stmt {
Expand Down Expand Up @@ -1877,7 +1870,6 @@ pub struct Arg {
pub range: TextRange,
pub arg: Identifier,
pub annotation: Option<Box<Expr>>,
pub type_comment: Option<String>,
}

/// See also [keyword](https://docs.python.org/3/library/ast.html#ast.keyword)
Expand Down Expand Up @@ -3019,6 +3011,9 @@ mod size_assertions {
use static_assertions::assert_eq_size;

assert_eq_size!(Stmt, [u8; 168]);
assert_eq_size!(StmtFunctionDef, [u8; 128]);
assert_eq_size!(StmtClassDef, [u8; 160]);
assert_eq_size!(StmtTry, [u8; 104]);
assert_eq_size!(Expr, [u8; 80]);
assert_eq_size!(Constant, [u8; 32]);
assert_eq_size!(Pattern, [u8; 96]);
Expand Down
3 changes: 0 additions & 3 deletions crates/ruff_python_ast/src/visitor/preorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ where
targets,
value,
range: _,
type_comment: _,
}) => {
for expr in targets {
visitor.visit_expr(expr);
Expand Down Expand Up @@ -320,13 +319,11 @@ where
Stmt::With(ast::StmtWith {
items,
body,
type_comment: _,
range: _,
})
| Stmt::AsyncWith(ast::StmtAsyncWith {
items,
body,
type_comment: _,
range: _,
}) => {
for with_item in items {
Expand Down
1 change: 0 additions & 1 deletion crates/ruff_python_formatter/src/other/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ impl FormatNodeRule<Arg> for FormatArg {
range: _,
arg,
annotation,
type_comment: _,
} = item;

arg.format().fmt(f)?;
Expand Down
1 change: 0 additions & 1 deletion crates/ruff_python_formatter/src/statement/stmt_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
range: _,
targets,
value,
type_comment: _,
} = item;

let (first, rest) = targets.split_first().ok_or(FormatError::syntax_error(
Expand Down
Loading

0 comments on commit 7c7231d

Please sign in to comment.