Extend assertEquals check to all deprecated unittest aliases (#380)

This commit is contained in:
Charlie Marsh 2022-10-09 22:13:51 -04:00 committed by GitHub
parent a2f78ba2c7
commit 71ebd39f35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 48 deletions

View file

@ -6,3 +6,5 @@ class Suite(unittest.TestCase):
self.assertEquals (1, 2) self.assertEquals (1, 2)
self.assertEquals(1, 2) self.assertEquals(1, 2)
self.assertEqual(3, 4) self.assertEqual(3, 4)
self.failUnlessAlmostEqual(1, 1.1)
self.assertNotRegexpMatches("a", "b")

View file

@ -361,23 +361,6 @@ pub fn check_duplicate_arguments(arguments: &Arguments) -> Vec<Check> {
checks checks
} }
/// Check AssertEquals compliance.
pub fn check_assert_equals(expr: &Expr) -> Option<Check> {
if let ExprKind::Attribute { value, attr, .. } = &expr.node {
if attr == "assertEquals" {
if let ExprKind::Name { id, .. } = &value.node {
if id == "self" {
return Some(Check::new(
CheckKind::NoAssertEquals,
Range::from_located(expr),
));
}
}
}
}
None
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
enum DictionaryKey<'a> { enum DictionaryKey<'a> {
Constant(&'a Constant), Constant(&'a Constant),

View file

@ -726,7 +726,7 @@ where
.. ..
} => { } => {
if self.settings.enabled.contains(&CheckCode::U005) { if self.settings.enabled.contains(&CheckCode::U005) {
plugins::assert_equals(self, func); plugins::deprecated_unittest_alias(self, func);
} }
// flake8-super // flake8-super

View file

@ -227,7 +227,7 @@ pub enum CheckKind {
TypeOfPrimitive(Primitive), TypeOfPrimitive(Primitive),
UnnecessaryAbspath, UnnecessaryAbspath,
UselessMetaclassType, UselessMetaclassType,
NoAssertEquals, DeprecatedUnittestAlias(String, String),
UselessObjectInheritance(String), UselessObjectInheritance(String),
UsePEP585Annotation(String), UsePEP585Annotation(String),
UsePEP604Annotation, UsePEP604Annotation,
@ -323,7 +323,10 @@ impl CheckCode {
CheckCode::U002 => CheckKind::UnnecessaryAbspath, CheckCode::U002 => CheckKind::UnnecessaryAbspath,
CheckCode::U003 => CheckKind::TypeOfPrimitive(Primitive::Str), CheckCode::U003 => CheckKind::TypeOfPrimitive(Primitive::Str),
CheckCode::U004 => CheckKind::UselessObjectInheritance("...".to_string()), CheckCode::U004 => CheckKind::UselessObjectInheritance("...".to_string()),
CheckCode::U005 => CheckKind::NoAssertEquals, CheckCode::U005 => CheckKind::DeprecatedUnittestAlias(
"assertEquals".to_string(),
"assertEqual".to_string(),
),
CheckCode::U006 => CheckKind::UsePEP585Annotation("List".to_string()), CheckCode::U006 => CheckKind::UsePEP585Annotation("List".to_string()),
CheckCode::U007 => CheckKind::UsePEP604Annotation, CheckCode::U007 => CheckKind::UsePEP604Annotation,
CheckCode::U008 => CheckKind::SuperCallWithParameters, CheckCode::U008 => CheckKind::SuperCallWithParameters,
@ -403,7 +406,7 @@ impl CheckKind {
CheckKind::TypeOfPrimitive(_) => &CheckCode::U003, CheckKind::TypeOfPrimitive(_) => &CheckCode::U003,
CheckKind::UnnecessaryAbspath => &CheckCode::U002, CheckKind::UnnecessaryAbspath => &CheckCode::U002,
CheckKind::UselessMetaclassType => &CheckCode::U001, CheckKind::UselessMetaclassType => &CheckCode::U001,
CheckKind::NoAssertEquals => &CheckCode::U005, CheckKind::DeprecatedUnittestAlias(_, _) => &CheckCode::U005,
CheckKind::UsePEP585Annotation(_) => &CheckCode::U006, CheckKind::UsePEP585Annotation(_) => &CheckCode::U006,
CheckKind::UsePEP604Annotation => &CheckCode::U007, CheckKind::UsePEP604Annotation => &CheckCode::U007,
CheckKind::UselessObjectInheritance(_) => &CheckCode::U004, CheckKind::UselessObjectInheritance(_) => &CheckCode::U004,
@ -595,8 +598,8 @@ impl CheckKind {
"`abspath(__file__)` is unnecessary in Python 3.9 and later".to_string() "`abspath(__file__)` is unnecessary in Python 3.9 and later".to_string()
} }
CheckKind::UselessMetaclassType => "`__metaclass__ = type` is implied".to_string(), CheckKind::UselessMetaclassType => "`__metaclass__ = type` is implied".to_string(),
CheckKind::NoAssertEquals => { CheckKind::DeprecatedUnittestAlias(alias, target) => {
"`assertEquals` is deprecated, use `assertEqual` instead".to_string() format!("`{}` is deprecated, use `{}` instead", alias, target)
} }
CheckKind::UselessObjectInheritance(name) => { CheckKind::UselessObjectInheritance(name) => {
format!("Class `{name}` inherits from object") format!("Class `{name}` inherits from object")
@ -624,7 +627,7 @@ impl CheckKind {
pub fn fixable(&self) -> bool { pub fn fixable(&self) -> bool {
matches!( matches!(
self, self,
CheckKind::NoAssertEquals CheckKind::DeprecatedUnittestAlias(_, _)
| CheckKind::PPrintFound | CheckKind::PPrintFound
| CheckKind::PrintFound | CheckKind::PrintFound
| CheckKind::SuperCallWithParameters | CheckKind::SuperCallWithParameters

View file

@ -1,5 +1,5 @@
pub use assert_equals::assert_equals;
pub use assert_tuple::assert_tuple; pub use assert_tuple::assert_tuple;
pub use deprecated_unittest_alias::deprecated_unittest_alias;
pub use if_tuple::if_tuple; pub use if_tuple::if_tuple;
pub use invalid_print_syntax::invalid_print_syntax; pub use invalid_print_syntax::invalid_print_syntax;
pub use print_call::print_call; pub use print_call::print_call;
@ -11,8 +11,8 @@ pub use use_pep604_annotation::use_pep604_annotation;
pub use useless_metaclass_type::useless_metaclass_type; pub use useless_metaclass_type::useless_metaclass_type;
pub use useless_object_inheritance::useless_object_inheritance; pub use useless_object_inheritance::useless_object_inheritance;
mod assert_equals;
mod assert_tuple; mod assert_tuple;
mod deprecated_unittest_alias;
mod if_tuple; mod if_tuple;
mod invalid_print_syntax; mod invalid_print_syntax;
mod print_call; mod print_call;

View file

@ -1,20 +0,0 @@
use rustpython_ast::{Expr, Location};
use crate::ast::checks;
use crate::autofix::fixer;
use crate::check_ast::Checker;
use crate::checks::Fix;
pub fn assert_equals(checker: &mut Checker, expr: &Expr) {
if let Some(mut check) = checks::check_assert_equals(expr) {
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
check.amend(Fix {
content: "self.assertEqual".to_string(),
location: Location::new(expr.location.row(), expr.location.column()),
end_location: Location::new(expr.end_location.row(), expr.end_location.column()),
applied: false,
});
}
checker.add_check(check);
}
}

View file

@ -0,0 +1,56 @@
use std::collections::BTreeMap;
use once_cell::sync::Lazy;
use rustpython_ast::{Expr, ExprKind, Location};
use crate::ast::types::Range;
use crate::autofix::fixer;
use crate::check_ast::Checker;
use crate::checks::{Check, CheckKind, Fix};
static DEPRECATED_ALIASES: Lazy<BTreeMap<&'static str, &'static str>> = Lazy::new(|| {
BTreeMap::from([
("failUnlessEqual", "assertEqual"),
("assertEquals", "assertEqual"),
("failIfEqual", "assertNotEqual"),
("assertNotEquals", "assertNotEqual"),
("failUnless", "assertTrue"),
("assert_", "assertTrue"),
("failIf", "assertFalse"),
("failUnlessRaises", "assertRaises"),
("failUnlessAlmostEqual", "assertAlmostEqual"),
("assertAlmostEquals", "assertAlmostEqual"),
("failIfAlmostEqual", "assertNotAlmostEqual"),
("assertNotAlmostEquals", "assertNotAlmostEqual"),
("assertRegexpMatches", "assertRegex"),
("assertNotRegexpMatches", "assertNotRegex"),
("assertRaisesRegexp", "assertRaisesRegex"),
])
});
pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) {
if let ExprKind::Attribute { value, attr, .. } = &expr.node {
if let Some(target) = DEPRECATED_ALIASES.get(attr.as_str()) {
if let ExprKind::Name { id, .. } = &value.node {
if id == "self" {
let mut check = Check::new(
CheckKind::DeprecatedUnittestAlias(attr.to_string(), target.to_string()),
Range::from_located(expr),
);
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
check.amend(Fix {
content: format!("self.{}", target),
location: Location::new(expr.location.row(), expr.location.column()),
end_location: Location::new(
expr.end_location.row(),
expr.end_location.column(),
),
applied: false,
});
}
checker.add_check(check);
}
}
}
}
}

View file

@ -2,7 +2,10 @@
source: src/linter.rs source: src/linter.rs
expression: checks expression: checks
--- ---
- kind: NoAssertEquals - kind:
DeprecatedUnittestAlias:
- assertEquals
- assertEqual
location: location:
row: 6 row: 6
column: 9 column: 9
@ -18,7 +21,10 @@ expression: checks
row: 6 row: 6
column: 26 column: 26
applied: false applied: false
- kind: NoAssertEquals - kind:
DeprecatedUnittestAlias:
- assertEquals
- assertEqual
location: location:
row: 7 row: 7
column: 9 column: 9
@ -34,4 +40,42 @@ expression: checks
row: 7 row: 7
column: 26 column: 26
applied: false applied: false
- kind:
DeprecatedUnittestAlias:
- failUnlessAlmostEqual
- assertAlmostEqual
location:
row: 9
column: 9
end_location:
row: 9
column: 35
fix:
content: self.assertAlmostEqual
location:
row: 9
column: 9
end_location:
row: 9
column: 35
applied: false
- kind:
DeprecatedUnittestAlias:
- assertNotRegexpMatches
- assertNotRegex
location:
row: 10
column: 9
end_location:
row: 10
column: 36
fix:
content: self.assertNotRegex
location:
row: 10
column: 9
end_location:
row: 10
column: 36
applied: false