Add bugbear immutable functions as allowed in dataclasses (#4122)

This commit is contained in:
Moritz Sauter 2023-04-28 03:23:06 +02:00 committed by GitHub
parent 089b64e9c1
commit ee6d8f7467
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 132 additions and 63 deletions

View file

@ -122,3 +122,33 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
_ => false,
}
}
const IMMUTABLE_FUNCS: &[&[&str]] = &[
&["", "tuple"],
&["", "frozenset"],
&["datetime", "date"],
&["datetime", "datetime"],
&["datetime", "timedelta"],
&["decimal", "Decimal"],
&["operator", "attrgetter"],
&["operator", "itemgetter"],
&["operator", "methodcaller"],
&["pathlib", "Path"],
&["types", "MappingProxyType"],
&["re", "compile"],
];
pub fn is_immutable_func(
context: &Context,
func: &Expr,
extend_immutable_calls: &[CallPath],
) -> bool {
context.resolve_call_path(func).map_or(false, |call_path| {
IMMUTABLE_FUNCS
.iter()
.any(|target| call_path.as_slice() == *target)
|| extend_immutable_calls
.iter()
.any(|target| call_path == *target)
})
}