Skip to content

Extend B019 to alru_cache and async methods - #570

Merged
cooperlees merged 1 commit into
PyCQA:mainfrom
ChrisJr404:b019-alru-cache
Aug 19, 2026
Merged

Extend B019 to alru_cache and async methods#570
cooperlees merged 1 commit into
PyCQA:mainfrom
ChrisJr404:b019-alru-cache

Conversation

@ChrisJr404

Copy link
Copy Markdown
Contributor

This extends B019 to cover async_lru.alru_cache and cache decorators on async def methods, as suggested in #488.

alru_cache is the async port of functools.lru_cache (maintained by aio-libs), so when it decorates a method it keeps instance references alive in exactly the same way and leads to the same memory leak B019 is meant to catch. While looking into it I noticed B019 only ran on synchronous function definitions, so even a plain @lru_cache/@cache on an async def method slipped through. I added async_lru.alru_cache and alru_cache to the recognised decorators and wired the check into visit_AsyncFunctionDef, so all of these now report on both sync and async methods.

I extended tests/eval_files/b019.py with an async class covering alru_cache, async_lru.alru_cache, their called forms, and lru_cache/cache on async methods, plus the classmethod/staticmethod negatives to make sure those still don't fire. The README description and change log are updated to match.

Closes #488

B019 warns about functools.lru_cache/cache on methods because the cache
holds instance references and prevents garbage collection. The same
problem applies to async_lru.alru_cache, which is the async equivalent,
and to plain lru_cache/cache used on async methods (which the check
previously skipped because it only visited sync function defs).

Add async_lru.alru_cache and alru_cache to the recognised cache
decorators and run the check on async def methods too.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request expands flake8-bugbear’s B019 check to also detect cache decorators on async def instance methods, including async_lru.alru_cache, addressing the memory-leak risk described in #488.

Changes:

  • Run the B019 check for ast.AsyncFunctionDef nodes in addition to ast.FunctionDef.
  • Extend B019’s recognized cache decorators to include async_lru.alru_cache / alru_cache.
  • Update the B019 eval test file and README/changelog text to cover async-method scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
bugbear.py Runs B019 on async method definitions and recognizes async_lru.alru_cache / alru_cache decorators.
tests/eval_files/b019.py Adds async-method test cases for cache/lru_cache and alru_cache (including called forms) and preserves classmethod/staticmethod negatives.
README.rst Updates B019 documentation and changelog entry to reflect async and alru_cache coverage.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@cooperlees cooperlees left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests pass and it all looks good to me. So thanks!

Comment thread tests/eval_files/b019.py
Comment on lines +10 to +11
import async_lru
from async_lru import alru_cache

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a dep for the test running? How does this not cause an error or am I forgetting we have some import mocking magic?

@cooperlees
cooperlees merged commit 838c365 into PyCQA:main Aug 19, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider adding alru_cache to B019

3 participants