Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add B019 check to find cache decorators on class methods (#218)
* Add B019 check to find cache decorators on class methods * B019: Change decorator resolution approach to retain lineno Starting in Python 3.8, the function node definition's `lineno` is changed to index its `def ...` line rather than the first line where its decorators start. This causes inconsistent line numbers across Python versions for the line reported by Flake8. We can use the decorator node location instead, which provides a consistent location, and makes sense because this hits on decorators. * Update README verbiage * Prefer `extend-select` and `extend-ignore` for configuring opinionated warnings (`B9`) * Add deprecation note for Bugbear's internal handling of whether or not to emit `B9` codes * Add an example for `extend-immutable-call` specification * B9: Make Bugbear aware of flake8's `extend-select` * The code for Bugbear's built-in filtering for opinionated warnings predates the addition of `extend-select` to flake8 (`v4.0`) so it's not part of the check for explicit specification of `B9` codes. * Switch from `Mock` to `argparse.Namespace` for specifying options to tests to match the incoming type from `flake8` and avoid mocking side-effects. * Style fixes from review Co-authored-by: Cooper Ry Lees <me@cooperlees.com> Co-authored-by: Cooper Ry Lees <me@cooperlees.com>
- Loading branch information
1 parent
83a8227
commit 4a92ce5
Showing
4 changed files
with
271 additions
and
37 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
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,103 @@ | ||
""" | ||
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(): | ||
... | ||
|
||
|
||
class Foo: | ||
def __init__(self, x): | ||
self.x = x | ||
|
||
def compute_method(self, y): | ||
... | ||
|
||
@some_other_cache | ||
def user_cached_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_method(self, y): | ||
... | ||
|
||
@cache | ||
def another_cached_method(self, y): | ||
... | ||
|
||
@functools.cache() | ||
def called_cached_method(self, y): | ||
... | ||
|
||
@cache() | ||
def another_called_cached_method(self, y): | ||
... | ||
|
||
@functools.lru_cache | ||
def lru_cached_method(self, y): | ||
... | ||
|
||
@lru_cache | ||
def another_lru_cached_method(self, y): | ||
... | ||
|
||
@functools.lru_cache() | ||
def called_lru_cached_method(self, y): | ||
... | ||
|
||
@lru_cache() | ||
def another_called_lru_cached_method(self, y): | ||
... |
Oops, something went wrong.