-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
465 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
""" | ||
Should emit: | ||
B019 - on lines 73, 77, 81, 85, 89, 93, 97, 101 | ||
""" | ||
import functools | ||
from functools import cache, cached_property, lru_cache | ||
|
||
|
||
def some_other_cache(): | ||
... | ||
|
||
|
||
@functools.cache | ||
def compute_func(self, y): | ||
... | ||
|
||
|
||
class Foo: | ||
def __init__(self, x): | ||
self.x = x | ||
|
||
def compute_method(self, y): | ||
... | ||
|
||
@some_other_cache | ||
def user_cached_instance_method(self, y): | ||
... | ||
|
||
@classmethod | ||
@functools.cache | ||
def cached_classmethod(cls, y): | ||
... | ||
|
||
@classmethod | ||
@cache | ||
def other_cached_classmethod(cls, y): | ||
... | ||
|
||
@classmethod | ||
@functools.lru_cache | ||
def lru_cached_classmethod(cls, y): | ||
... | ||
|
||
@classmethod | ||
@lru_cache | ||
def other_lru_cached_classmethod(cls, y): | ||
... | ||
|
||
@staticmethod | ||
@functools.cache | ||
def cached_staticmethod(y): | ||
... | ||
|
||
@staticmethod | ||
@cache | ||
def other_cached_staticmethod(y): | ||
... | ||
|
||
@staticmethod | ||
@functools.lru_cache | ||
def lru_cached_staticmethod(y): | ||
... | ||
|
||
@staticmethod | ||
@lru_cache | ||
def other_lru_cached_staticmethod(y): | ||
... | ||
|
||
@functools.cached_property | ||
def some_cached_property(self): | ||
... | ||
|
||
@cached_property | ||
def some_other_cached_property(self): | ||
... | ||
|
||
# Remaining methods should emit B019 | ||
@functools.cache | ||
def cached_instance_method(self, y): | ||
... | ||
|
||
@cache | ||
def another_cached_instance_method(self, y): | ||
... | ||
|
||
@functools.cache() | ||
def called_cached_instance_method(self, y): | ||
... | ||
|
||
@cache() | ||
def another_called_cached_instance_method(self, y): | ||
... | ||
|
||
@functools.lru_cache | ||
def lru_cached_instance_method(self, y): | ||
... | ||
|
||
@lru_cache | ||
def another_lru_cached_instance_method(self, y): | ||
... | ||
|
||
@functools.lru_cache() | ||
def called_lru_cached_instance_method(self, y): | ||
... | ||
|
||
@lru_cache() | ||
def another_called_lru_cached_instance_method(self, y): | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use rustpython_ast::Expr; | ||
|
||
use crate::ast::helpers::compose_call_path; | ||
use crate::ast::types::Range; | ||
use crate::ast::types::ScopeKind; | ||
use crate::check_ast::Checker; | ||
use crate::checks::{Check, CheckKind}; | ||
|
||
const CACHE_FUNCTIONS: [&str; 4] = [ | ||
"functools.lru_cache", | ||
"functools.cache", | ||
"lru_cache", | ||
"cache", | ||
]; | ||
|
||
pub fn cached_instance_method(checker: &mut Checker, decorator_list: &[Expr]) { | ||
if matches!(checker.current_scope().kind, ScopeKind::Class(_)) { | ||
for decorator in decorator_list { | ||
if let Some(decorator_path) = compose_call_path(decorator) { | ||
if decorator_path == "classmethod" || decorator_path == "staticmethod" { | ||
return; | ||
} | ||
if CACHE_FUNCTIONS.contains(&decorator_path.as_str()) { | ||
checker.add_check(Check::new( | ||
CheckKind::CachedInstanceMethod, | ||
Range::from_located(decorator), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
source: src/linter.rs | ||
expression: checks | ||
--- | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 78 | ||
column: 5 | ||
end_location: | ||
row: 78 | ||
column: 20 | ||
fix: ~ | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 82 | ||
column: 5 | ||
end_location: | ||
row: 82 | ||
column: 10 | ||
fix: ~ | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 86 | ||
column: 5 | ||
end_location: | ||
row: 86 | ||
column: 22 | ||
fix: ~ | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 90 | ||
column: 5 | ||
end_location: | ||
row: 90 | ||
column: 12 | ||
fix: ~ | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 94 | ||
column: 5 | ||
end_location: | ||
row: 94 | ||
column: 24 | ||
fix: ~ | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 98 | ||
column: 5 | ||
end_location: | ||
row: 98 | ||
column: 14 | ||
fix: ~ | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 102 | ||
column: 5 | ||
end_location: | ||
row: 102 | ||
column: 26 | ||
fix: ~ | ||
- kind: CachedInstanceMethod | ||
location: | ||
row: 106 | ||
column: 5 | ||
end_location: | ||
row: 106 | ||
column: 16 | ||
fix: ~ | ||
|