astral-sh/ruff · critical

invalid default exception pattern

Error message

invalid default exception pattern

What it means

flake8-pytest-style builds its default lists of broad-exception patterns for PT001/PT011-style settings from hard-coded names like `ValueError` and `socket.error`. `IdentifierPattern::new` validates each name; the built-in defaults are expected to always be valid, so failure panics — meaning the pattern grammar or defaults were changed inconsistently.

Source

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

use crate::display_settings;
use ruff_macros::CacheKey;

use crate::settings::types::IdentifierPattern;

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,

View on GitHub (pinned to 26f38c119c)

Solutions

  1. If developing: verify which default failed validation and fix the pattern grammar or default list to agree
  2. Update Ruff to a version with consistent defaults/validator
  3. For user-facing config errors, pass valid dotted names (e.g. `socket.error`) in `broad-exceptions`/`broad-exceptions-warn` settings
  4. Temporarily override the settings with your own explicit pattern list in pyproject.toml

Example fix

// before
.map(|pattern| IdentifierPattern::new(pattern).expect("invalid default exception pattern"))
// after
.map(|pattern| IdentifierPattern::new(pattern))
.collect::<Result<Vec<_>, _>>()
.expect("built-in default exception patterns must be valid — report this bug")
Defensive patterns

Strategy: validation

Validate before calling

# validate user-configured broad exception patterns early
# ruff config (pyproject.toml): [tool.ruff.lint.flake8-pytest-style]
# broad-exceptions = ["BaseException"]  — use valid dotted names, no wildcards
import tomllib
cfg = tomllib.load(open('pyproject.toml','rb'))['tool']['ruff']['lint']['flake8-pytest-style']
for p in cfg.get('broad-exceptions', []):
    assert all(part.isidentifier() for part in p.split('.')), f'invalid pattern: {p}'

Type guard

def is_valid_exception_pattern(p: str) -> bool:
    return bool(p) and all(part.isidentifier() for part in p.split('.'))

Prevention

When it happens

Trigger: Constructing `Flake8PytestStyleSettings::default` (via `default_broad_exceptions`) when `IdentifierPattern::new` rejects one of the hard-coded defaults (e.g. `socket.error`) — only after a change to the pattern grammar/validator, or if user settings substitute an invalid pattern into the same construction path.

Common situations: Contributors editing the defaults or IdentifierPattern validation; users with custom `flake8-pytest-style.broad-exceptions` config passing invalid patterns may see analogous validation errors rather than this specific default panic.

Related errors


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