[flake8-use-pathlib] Dotless suffix passed to Path.with_suffix() (PTH901) (#14779)

## Summary

Resolves #14441.

## Test Plan

`cargo nextest run` and `cargo insta test`.
This commit is contained in:
InSync 2024-12-06 19:08:20 +07:00 committed by GitHub
parent 1559c73fcd
commit 89368a62a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 1371 additions and 0 deletions

View file

@ -741,6 +741,43 @@ impl TypeChecker for IoBaseChecker {
}
}
pub struct PathlibPathChecker;
impl PathlibPathChecker {
fn is_pathlib_path_constructor(semantic: &SemanticModel, expr: &Expr) -> bool {
let Some(qualified_name) = semantic.resolve_qualified_name(expr) else {
return false;
};
matches!(
qualified_name.segments(),
[
"pathlib",
"Path"
| "PosixPath"
| "PurePath"
| "PurePosixPath"
| "PureWindowsPath"
| "WindowsPath"
]
)
}
}
impl TypeChecker for PathlibPathChecker {
fn match_annotation(annotation: &Expr, semantic: &SemanticModel) -> bool {
Self::is_pathlib_path_constructor(semantic, annotation)
}
fn match_initializer(initializer: &Expr, semantic: &SemanticModel) -> bool {
let Expr::Call(ast::ExprCall { func, .. }) = initializer else {
return false;
};
Self::is_pathlib_path_constructor(semantic, func)
}
}
/// Test whether the given binding can be considered a list.
///
/// For this, we check what value might be associated with it through it's initialization and
@ -824,6 +861,12 @@ pub fn is_io_base_expr(expr: &Expr, semantic: &SemanticModel) -> bool {
IoBaseChecker::match_initializer(expr, semantic)
}
/// Test whether the given binding can be considered a `pathlib.PurePath`
/// or an instance of a subclass thereof.
pub fn is_pathlib_path(binding: &Binding, semantic: &SemanticModel) -> bool {
check_type::<PathlibPathChecker>(binding, semantic)
}
/// Find the [`ParameterWithDefault`] corresponding to the given [`Binding`].
#[inline]
fn find_parameter<'a>(