[flake8-pytest-style] Implement pytest.warns diagnostics (PT029, PT030, PT031) (#15444)

## Summary

Implements upstream diagnostics `PT029`, `PT030`, `PT031` that function
as pytest.warns corollaries of `PT010`, `PT011`, `PT012` respectively.
Most of the implementation and documentation is designed to mirror those
existing diagnostics.

Closes #14239

## Test Plan

Tests for `PT029`, `PT030`, `PT031` largely copied from `PT010`,
`PT011`, `PT012` respectively.

`cargo nextest run`

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
Tom Kuson 2025-01-13 01:46:59 +00:00 committed by GitHub
parent fa11b08766
commit 347ab5b47a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 823 additions and 0 deletions

View file

@ -1564,6 +1564,38 @@ pub struct Flake8PytestStyleOptions {
example = "mark-parentheses = true"
)]
pub mark_parentheses: Option<bool>,
/// List of warning names that require a match= parameter in a
/// `pytest.warns()` call.
///
/// Supports glob patterns. For more information on the glob syntax, refer
/// to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax).
#[option(
default = r#"["Warning", "UserWarning", "DeprecationWarning"]"#,
value_type = "list[str]",
example = "warns-require-match-for = [\"requests.RequestsWarning\"]"
)]
pub warns_require_match_for: Option<Vec<String>>,
/// List of additional warning names that require a match= parameter in a
/// `pytest.warns()` call. This extends the default list of warnings that
/// require a match= parameter.
///
/// This option is useful if you want to extend the default list of warnings
/// that require a match= parameter without having to specify the entire
/// list.
///
/// Note that this option does not remove any warnings from the default
/// list.
///
/// Supports glob patterns. For more information on the glob syntax, refer
/// to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax).
#[option(
default = "[]",
value_type = "list[str]",
example = "warns-extend-require-match-for = [\"requests.RequestsWarning\"]"
)]
pub warns_extend_require_match_for: Option<Vec<String>>,
}
impl Flake8PytestStyleOptions {
@ -1596,6 +1628,28 @@ impl Flake8PytestStyleOptions {
.map_err(SettingsError::InvalidRaisesExtendRequireMatchFor)?
.unwrap_or_default(),
mark_parentheses: self.mark_parentheses.unwrap_or_default(),
warns_require_match_for: self
.warns_require_match_for
.map(|patterns| {
patterns
.into_iter()
.map(|pattern| IdentifierPattern::new(&pattern))
.collect()
})
.transpose()
.map_err(SettingsError::InvalidWarnsRequireMatchFor)?
.unwrap_or_else(flake8_pytest_style::settings::default_broad_warnings),
warns_extend_require_match_for: self
.warns_extend_require_match_for
.map(|patterns| {
patterns
.into_iter()
.map(|pattern| IdentifierPattern::new(&pattern))
.collect()
})
.transpose()
.map_err(SettingsError::InvalidWarnsExtendRequireMatchFor)?
.unwrap_or_default(),
})
}
}