[flake8-errmsg] Stabilize extending raw-string-in-exception (EM101) to support byte strings (#20273)

Introduced in #18867. Removed gating, updated tests, updated docs.
This commit is contained in:
Dylan 2025-09-08 09:07:35 -05:00 committed by Brent Westbrook
parent beeeb8d5c5
commit 64fe7d30a3
4 changed files with 3 additions and 18 deletions

View file

@ -200,11 +200,6 @@ pub(crate) const fn is_optional_as_none_in_union_enabled(settings: &LinterSettin
settings.preview.is_enabled() settings.preview.is_enabled()
} }
// https://github.com/astral-sh/ruff/pull/18867
pub(crate) const fn is_raise_exception_byte_string_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}
// https://github.com/astral-sh/ruff/pull/18683 // https://github.com/astral-sh/ruff/pull/18683
pub(crate) const fn is_safe_super_call_with_parameters_fix_enabled( pub(crate) const fn is_safe_super_call_with_parameters_fix_enabled(
settings: &LinterSettings, settings: &LinterSettings,

View file

@ -9,7 +9,6 @@ mod tests {
use anyhow::Result; use anyhow::Result;
use crate::registry::Rule; use crate::registry::Rule;
use crate::settings::types::PreviewMode;
use crate::test::test_path; use crate::test::test_path;
use crate::{assert_diagnostics, settings}; use crate::{assert_diagnostics, settings};
@ -47,15 +46,14 @@ mod tests {
} }
#[test] #[test]
fn preview_string_exception() -> Result<()> { fn string_exception() -> Result<()> {
let diagnostics = test_path( let diagnostics = test_path(
Path::new("flake8_errmsg/EM101_byte_string.py"), Path::new("flake8_errmsg/EM101_byte_string.py"),
&settings::LinterSettings { &settings::LinterSettings {
preview: PreviewMode::Enabled,
..settings::LinterSettings::for_rule(Rule::RawStringInException) ..settings::LinterSettings::for_rule(Rule::RawStringInException)
}, },
)?; )?;
assert_diagnostics!("preview", diagnostics); assert_diagnostics!(diagnostics);
Ok(()) Ok(())
} }
} }

View file

@ -7,16 +7,12 @@ use ruff_text_size::Ranged;
use crate::Locator; use crate::Locator;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::preview::is_raise_exception_byte_string_enabled;
use crate::registry::Rule; use crate::registry::Rule;
use crate::{Edit, Fix, FixAvailability, Violation}; use crate::{Edit, Fix, FixAvailability, Violation};
/// ## What it does /// ## What it does
/// Checks for the use of string literals in exception constructors. /// Checks for the use of string literals in exception constructors.
/// ///
/// In [preview], this rule checks for byte string literals in
/// exception constructors.
///
/// ## Why is this bad? /// ## Why is this bad?
/// Python includes the `raise` in the default traceback (and formatters /// Python includes the `raise` in the default traceback (and formatters
/// like Rich and IPython do too). /// like Rich and IPython do too).
@ -51,8 +47,6 @@ use crate::{Edit, Fix, FixAvailability, Violation};
/// raise RuntimeError(msg) /// raise RuntimeError(msg)
/// RuntimeError: 'Some value' is incorrect /// RuntimeError: 'Some value' is incorrect
/// ``` /// ```
///
/// [preview]: https://docs.astral.sh/ruff/preview/
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
pub(crate) struct RawStringInException; pub(crate) struct RawStringInException;
@ -218,9 +212,7 @@ pub(crate) fn string_in_exception(checker: &Checker, stmt: &Stmt, exc: &Expr) {
// Check for byte string literals. // Check for byte string literals.
Expr::BytesLiteral(ast::ExprBytesLiteral { value: bytes, .. }) => { Expr::BytesLiteral(ast::ExprBytesLiteral { value: bytes, .. }) => {
if checker.settings().rules.enabled(Rule::RawStringInException) { if checker.settings().rules.enabled(Rule::RawStringInException) {
if bytes.len() >= checker.settings().flake8_errmsg.max_string_length if bytes.len() >= checker.settings().flake8_errmsg.max_string_length {
&& is_raise_exception_byte_string_enabled(checker.settings())
{
let mut diagnostic = let mut diagnostic =
checker.report_diagnostic(RawStringInException, first.range()); checker.report_diagnostic(RawStringInException, first.range());
if let Some(indentation) = whitespace::indentation(checker.source(), stmt) { if let Some(indentation) = whitespace::indentation(checker.source(), stmt) {