astral-sh/ruff · error

invalid default warning pattern

Error message

invalid default warning pattern

What it means

This is a compile-time-fixed default-value construction in flake8_pytest_style's settings. `default_broad_warnings` builds IdentifierPattern values from a hard-coded list ("Warning", "UserWarning", "DeprecationWarning") and calls `.expect(...)`. IdentifierPattern::new only fails for patterns that cannot be parsed/compiled; since these literals are valid, the panic is an internal invariant check and only fires if a refactor makes the defaults invalid.

Source

Thrown at crates/ruff_linter/src/rules/flake8_pytest_style/settings.rs:29

use super::types;

pub fn default_broad_exceptions() -> Vec<IdentifierPattern> {
    [
        "BaseException",
        "Exception",
        "ValueError",
        "OSError",
        "IOError",
        "EnvironmentError",
        "socket.error",
    ]
    .map(|pattern| IdentifierPattern::new(pattern).expect("invalid default exception pattern"))
    .to_vec()
}

pub fn default_broad_warnings() -> Vec<IdentifierPattern> {
    ["Warning", "UserWarning", "DeprecationWarning"]
        .map(|pattern| IdentifierPattern::new(pattern).expect("invalid default warning pattern"))
        .to_vec()
}

#[derive(Debug, Clone, CacheKey)]
pub struct Settings {
    pub fixture_parentheses: bool,
    pub parametrize_names_type: types::ParametrizeNameType,
    pub parametrize_values_type: types::ParametrizeValuesType,
    pub parametrize_values_row_type: types::ParametrizeValuesRowType,
    pub raises_require_match_for: Vec<IdentifierPattern>,
    pub raises_extend_require_match_for: Vec<IdentifierPattern>,
    pub mark_parentheses: bool,
    pub warns_require_match_for: Vec<IdentifierPattern>,
    pub warns_extend_require_match_for: Vec<IdentifierPattern>,
}

impl Default for Settings {
    fn default() -> Self {

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Fix the hard-coded default list so every entry is a valid IdentifierPattern (valid identifier, correct case)
  2. Re-run the settings/default tests after changing IdentifierPattern::new validation
  3. If defaults must support arbitrary patterns, use lazy_static/OnceLock with proper error handling instead of expect

Example fix

// before
["Warning", "UserWarnig", "DeprecationWarning"]
    .map(|p| IdentifierPattern::new(p).expect("invalid default warning pattern"))
// after
["Warning", "UserWarning", "DeprecationWarning"]
    .map(|p| IdentifierPattern::new(p).expect("invalid default warning pattern"))
Defensive patterns

Strategy: validation

Validate before calling

// In ruff's repo: verify defaults parse before shipping
for pattern in ["Warning", "UserWarning", "DeprecationWarning"] {
    IdentifierPattern::new(pattern).unwrap_or_else(|e| panic!("bad default {pattern}: {e}"));
}

Type guard

fn valid_pattern(p: &str) -> bool { IdentifierPattern::new(p).is_ok() }

Prevention

When it happens

Trigger: It cannot be triggered by user configuration: the inputs are hard-coded. It would only panic if a code change introduces an unparseable default pattern or changes IdentifierPattern::new's validation to reject these strings (e.g. disallowing bare names).

Common situations: Developers hit this while modifying ruff's flake8_pytest_style settings — e.g. adding a new default warning pattern with a typo, or tightening IdentifierPattern parsing so plain identifiers are rejected.

Related errors


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/51c2c7daec1c076d. Report an issue: GitHub.