Specialize ConversionFlag (#4450)

This commit is contained in:
Jeong, YunWon 2023-05-17 01:00:13 +09:00 committed by GitHub
parent f0465bf106
commit 4b05ca1198
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 27 deletions

15
Cargo.lock generated
View file

@ -2001,7 +2001,7 @@ dependencies = [
[[package]]
name = "ruff_text_size"
version = "0.0.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee"
source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [
"schemars",
"serde",
@ -2072,7 +2072,7 @@ dependencies = [
[[package]]
name = "rustpython-ast"
version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee"
source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [
"is-macro",
"num-bigint",
@ -2083,7 +2083,7 @@ dependencies = [
[[package]]
name = "rustpython-format"
version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee"
source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [
"bitflags 2.2.1",
"itertools",
@ -2095,9 +2095,10 @@ dependencies = [
[[package]]
name = "rustpython-literal"
version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee"
source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [
"hexf-parse",
"is-macro",
"lexical-parse-float",
"num-traits",
"unic-ucd-category",
@ -2106,9 +2107,10 @@ dependencies = [
[[package]]
name = "rustpython-parser"
version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee"
source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [
"anyhow",
"is-macro",
"itertools",
"lalrpop-util",
"log",
@ -2128,8 +2130,9 @@ dependencies = [
[[package]]
name = "rustpython-parser-core"
version = "0.2.0"
source = "git+https://github.com/RustPython/Parser.git?rev=735c06d5f43da05d191f32442224f082f1d202ee#735c06d5f43da05d191f32442224f082f1d202ee"
source = "git+https://github.com/RustPython/Parser.git?rev=e820928f11a2453314ad4d5ce23f066d1d3faf73#e820928f11a2453314ad4d5ce23f066d1d3faf73"
dependencies = [
"is-macro",
"ruff_text_size",
]

View file

@ -31,10 +31,10 @@ proc-macro2 = { version = "1.0.51" }
quote = { version = "1.0.23" }
regex = { version = "1.7.1" }
rustc-hash = { version = "1.1.0" }
ruff_text_size = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee" }
rustpython-format = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee" }
rustpython-literal = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee" }
rustpython-parser = { git = "https://github.com/RustPython/Parser.git", rev = "735c06d5f43da05d191f32442224f082f1d202ee", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] }
ruff_text_size = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73" }
rustpython-format = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73" }
rustpython-literal = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73" }
rustpython-parser = { git = "https://github.com/RustPython/Parser.git", rev = "e820928f11a2453314ad4d5ce23f066d1d3faf73", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] }
schemars = { version = "0.8.12" }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93", features = ["preserve_order"] }

View file

@ -1,11 +1,11 @@
use ruff_text_size::TextRange;
use rustpython_parser::ast::{self, Constant, Expr, Int};
use rustpython_parser::ast::{self, Constant, ConversionFlag, Expr};
/// Wrap an expression in a `FormattedValue` with no special formatting.
fn to_formatted_value_expr(inner: &Expr) -> Expr {
let node = ast::ExprFormattedValue {
value: Box::new(inner.clone()),
conversion: Int::new(0),
conversion: ConversionFlag::None,
format_spec: None,
range: TextRange::default(),
};

View file

@ -1,4 +1,4 @@
use rustpython_parser::ast::{self, Expr, Ranged};
use rustpython_parser::ast::{self, ConversionFlag, Expr, Ranged};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
@ -43,10 +43,10 @@ impl AlwaysAutofixableViolation for ExplicitFStringTypeConversion {
pub(crate) fn explicit_f_string_type_conversion(
checker: &mut Checker,
formatted_value: &Expr,
conversion: ast::Int,
conversion: ConversionFlag,
) {
// Skip if there's already a conversion flag.
if conversion != ast::ConversionFlag::None as u32 {
if !conversion.is_none() {
return;
}

View file

@ -3,8 +3,9 @@
use num_bigint::BigInt;
use rustpython_parser::ast::{
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler, Expr,
ExprContext, Identifier, Int, Keyword, MatchCase, Operator, Pattern, Stmt, Unaryop, Withitem,
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag,
Excepthandler, Expr, ExprContext, Identifier, Int, Keyword, MatchCase, Operator, Pattern, Stmt,
Unaryop, Withitem,
};
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
@ -481,7 +482,7 @@ pub enum ComparableExpr<'a> {
},
FormattedValue {
value: Box<ComparableExpr<'a>>,
conversion: Int,
conversion: ConversionFlag,
format_spec: Option<Box<ComparableExpr<'a>>>,
},
JoinedStr {

View file

@ -5,7 +5,7 @@ use std::ops::Deref;
use rustpython_literal::escape::{AsciiEscape, Escape, UnicodeEscape};
use rustpython_parser::ast::{
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag,
Excepthandler, Expr, Identifier, Int, MatchCase, Operator, Pattern, Stmt, Suite, Withitem,
Excepthandler, Expr, Identifier, MatchCase, Operator, Pattern, Stmt, Suite, Withitem,
};
use crate::newlines::LineEnding;
@ -1340,7 +1340,12 @@ impl<'a> Generator<'a> {
}
}
fn unparse_formatted<U>(&mut self, val: &Expr<U>, conversion: Int, spec: Option<&Expr<U>>) {
fn unparse_formatted<U>(
&mut self,
val: &Expr<U>,
conversion: ConversionFlag,
spec: Option<&Expr<U>>,
) {
let mut generator = Generator::new(self.indent, self.quote, self.line_ending);
generator.unparse_expr(val, precedence::FORMATTED_VALUE);
let brace = if generator.buffer.starts_with('{') {
@ -1352,10 +1357,10 @@ impl<'a> Generator<'a> {
self.p(brace);
self.buffer += &generator.buffer;
if conversion.to_u32() != ConversionFlag::None as u32 {
if !conversion.is_none() {
self.p("!");
#[allow(clippy::cast_possible_truncation)]
self.p(&format!("{}", conversion.to_u32() as u8 as char));
self.p(&format!("{}", conversion as u8 as char));
}
if let Some(spec) = spec {

View file

@ -5,7 +5,7 @@ use std::ops::Deref;
use itertools::Itertools;
use ruff_text_size::{TextRange, TextSize};
use rustpython_parser::ast::{Constant, Ranged};
use rustpython_parser::ast::{Constant, ConversionFlag, Ranged};
use rustpython_parser::{ast, Mode};
use ruff_python_ast::source_code::Locator;
@ -410,7 +410,7 @@ pub enum ExprKind {
},
FormattedValue {
value: Box<Expr>,
conversion: usize,
conversion: ConversionFlag,
format_spec: Option<Box<Expr>>,
},
JoinedStr {
@ -2038,7 +2038,7 @@ impl From<(rustpython_parser::ast::Expr, &Locator<'_>)> for Expr {
range,
node: ExprKind::FormattedValue {
value: Box::new((*value, locator).into()),
conversion: conversion.to_usize(),
conversion,
format_spec: format_spec.map(|f| Box::new((*f, locator).into())),
},
trivia: vec![],

View file

@ -1,6 +1,6 @@
#![allow(unused_variables, clippy::too_many_arguments)]
use rustpython_parser::ast::Constant;
use rustpython_parser::ast::{Constant, ConversionFlag};
use ruff_formatter::prelude::*;
use ruff_formatter::{format_args, write};
@ -246,7 +246,7 @@ fn format_formatted_value(
f: &mut Formatter<ASTFormatContext>,
expr: &Expr,
value: &Expr,
_conversion: usize,
_conversion: ConversionFlag,
format_spec: Option<&Expr>,
) -> FormatResult<()> {
write!(f, [text("!")])?;