Skip to content

Commit

Permalink
Remove Result from SourceCodeGenerator signature (astral-sh#1677)
Browse files Browse the repository at this point in the history
We populate this buffer ourselves, so I believe it's fine for us to use
an unchecked UTF-8 cast here. It _dramatically_ simplifies so much
downstream code.
  • Loading branch information
charliermarsh authored Jan 6, 2023
1 parent ee4cae9 commit 8caa73d
Show file tree
Hide file tree
Showing 17 changed files with 274 additions and 473 deletions.
2 changes: 1 addition & 1 deletion ruff_dev/src/round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ pub fn main(cli: &Cli) -> Result<()> {
stylist.line_ending(),
);
generator.unparse_suite(&python_ast);
println!("{}", generator.generate()?);
println!("{}", generator.generate());
Ok(())
}
16 changes: 5 additions & 11 deletions src/flake8_bugbear/plugins/assert_false.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind};

use crate::ast::types::Range;
Expand Down Expand Up @@ -54,16 +53,11 @@ pub fn assert_false(checker: &mut Checker, stmt: &Stmt, test: &Expr, msg: Option
checker.style.line_ending(),
);
generator.unparse_stmt(&assertion_error(msg));
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
stmt.location,
stmt.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite `assert False`: {e}"),
};
check.amend(Fix::replacement(
generator.generate(),
stmt.location,
stmt.end_location.unwrap(),
));
}
checker.add_check(check);
}
16 changes: 5 additions & 11 deletions src/flake8_bugbear/plugins/duplicate_exceptions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use itertools::Itertools;
use log::error;
use rustc_hash::{FxHashMap, FxHashSet};
use rustpython_ast::{Excepthandler, ExcepthandlerKind, Expr, ExprContext, ExprKind, Location};

Expand Down Expand Up @@ -65,16 +64,11 @@ fn duplicate_handler_exceptions<'a>(
} else {
generator.unparse_expr(&type_pattern(unique_elts), 0);
}
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to remove duplicate exceptions: {e}"),
}
check.amend(Fix::replacement(
generator.generate(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down
16 changes: 5 additions & 11 deletions src/flake8_bugbear/plugins/getattr_with_constant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location};

use crate::ast::types::Range;
Expand Down Expand Up @@ -53,16 +52,11 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar
checker.style.line_ending(),
);
generator.unparse_expr(&attribute(obj, value), 0);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to rewrite `getattr`: {e}"),
}
check.amend(Fix::replacement(
generator.generate(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
16 changes: 5 additions & 11 deletions src/flake8_bugbear/plugins/redundant_tuple_in_exception_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::error;
use rustpython_ast::{Excepthandler, ExcepthandlerKind, ExprKind};

use crate::ast::types::Range;
Expand Down Expand Up @@ -30,16 +29,11 @@ pub fn redundant_tuple_in_exception_handler(checker: &mut Checker, handlers: &[E
checker.style.line_ending(),
);
generator.unparse_expr(elt, 0);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
type_.location,
type_.end_location.unwrap(),
));
}
Err(e) => error!("Failed to remove redundant tuple: {e}"),
}
check.amend(Fix::replacement(
generator.generate(),
type_.location,
type_.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down
26 changes: 7 additions & 19 deletions src/flake8_bugbear/plugins/setattr_with_constant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use anyhow::Result;
use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind, Location, Stmt, StmtKind};

use crate::ast::types::Range;
Expand All @@ -11,12 +9,7 @@ use crate::registry::{Check, CheckKind};
use crate::source_code_generator::SourceCodeGenerator;
use crate::source_code_style::SourceCodeStyleDetector;

fn assignment(
obj: &Expr,
name: &str,
value: &Expr,
stylist: &SourceCodeStyleDetector,
) -> Result<String> {
fn assignment(obj: &Expr, name: &str, value: &Expr, stylist: &SourceCodeStyleDetector) -> String {
let stmt = Stmt::new(
Location::default(),
Location::default(),
Expand All @@ -40,7 +33,7 @@ fn assignment(
stylist.line_ending(),
);
generator.unparse_stmt(&stmt);
generator.generate().map_err(std::convert::Into::into)
generator.generate()
}

/// B010
Expand Down Expand Up @@ -73,16 +66,11 @@ pub fn setattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar
if expr == child.as_ref() {
let mut check = Check::new(CheckKind::SetAttrWithConstant, Range::from_located(expr));
if checker.patch(check.kind.code()) {
match assignment(obj, name, value, checker.style) {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to fix invalid comparison: {e}"),
};
check.amend(Fix::replacement(
assignment(obj, name, value, checker.style),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down
101 changes: 26 additions & 75 deletions src/flake8_pytest_style/plugins/parametrize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use log::error;
use rustpython_ast::{Constant, Expr, ExprContext, ExprKind};

use super::helpers::is_pytest_parametrize;
Expand Down Expand Up @@ -36,7 +35,6 @@ fn elts_to_csv(elts: &[Expr], checker: &Checker) -> Option<String> {
checker.style.quote(),
checker.style.line_ending(),
);

generator.unparse_expr(
&create_expr(ExprKind::Constant {
value: Constant::Str(elts.iter().fold(String::new(), |mut acc, elt| {
Expand All @@ -56,17 +54,7 @@ fn elts_to_csv(elts: &[Expr], checker: &Checker) -> Option<String> {
}),
0,
);

match generator.generate() {
Ok(s) => Some(s),
Err(e) => {
error!(
"Failed to generate CSV string from sequence of names: {}",
e
);
None
}
}
Some(generator.generate())
}

/// PT006
Expand Down Expand Up @@ -120,19 +108,11 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
}),
1,
);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!(
"Failed to fix wrong name(s) type in \
`@pytest.mark.parametrize`: {e}"
),
};
check.amend(Fix::replacement(
generator.generate(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down Expand Up @@ -162,19 +142,11 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
}),
0,
);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!(
"Failed to fix wrong name(s) type in \
`@pytest.mark.parametrize`: {e}"
),
};
check.amend(Fix::replacement(
generator.generate(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down Expand Up @@ -208,19 +180,11 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
}),
0,
);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!(
"Failed to fix wrong name(s) type in \
`@pytest.mark.parametrize`: {e}"
),
};
check.amend(Fix::replacement(
generator.generate(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down Expand Up @@ -269,19 +233,11 @@ fn check_names(checker: &mut Checker, expr: &Expr) {
}),
1, // so tuple is generated with parentheses
);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!(
"Failed to fix wrong name(s) type in \
`@pytest.mark.parametrize`: {e}"
),
};
check.amend(Fix::replacement(
generator.generate(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down Expand Up @@ -353,16 +309,11 @@ fn handle_single_name(checker: &mut Checker, expr: &Expr, value: &Expr) {
checker.style.line_ending(),
);
generator.unparse_expr(&create_expr(value.node.clone()), 0);
match generator.generate() {
Ok(content) => {
check.amend(Fix::replacement(
content,
expr.location,
expr.end_location.unwrap(),
));
}
Err(e) => error!("Failed to fix wrong name(s) type in `@pytest.mark.parametrize`: {e}"),
};
check.amend(Fix::replacement(
generator.generate(),
expr.location,
expr.end_location.unwrap(),
));
}
checker.add_check(check);
}
Expand Down
2 changes: 1 addition & 1 deletion src/flake8_simplify/plugins/ast_bool_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn to_source(expr: &Expr, stylist: &SourceCodeStyleDetector) -> String {
stylist.line_ending(),
);
generator.unparse_expr(expr, 0);
generator.generate().unwrap()
generator.generate()
}

/// SIM101
Expand Down
Loading

0 comments on commit 8caa73d

Please sign in to comment.