Skip to content

[flake8-bugbear] Add async-lru and aiocache decorators to the B019 rule checker #16450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LuckySting
Copy link

Summary

Resolves #16436.

This change adds support for checking the B019 rule against async cache decorators from the two most commonly used third-party libraries (aiocache and async-lru). It also updates the error message to clearly indicate which decorator was used.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Mar 1, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the breaking Breaking API change label Mar 3, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thank you.

I'm a bit hesitant to accept this change because it is for third party libraries. Most of our rules are limited to first-party modules or very popular first-party modules.

I'm not sure async-lru meets that bar

/// - [don't lru_cache methods!](https://www.youtube.com/watch?v=sVjtp6tGo0g)
#[derive(ViolationMetadata)]
pub(crate) struct CachedInstanceMethod;
pub(crate) struct CachedInstanceMethod {
decorator_name: String,
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I'd suggest using an enum here to avoid unnecessary String allocation

#[derive(Copy, Clone, Deubg)]
enum LruDecorator {
	FuncToolsLruCache,
	FunctoolsCache,
	AsyncLru,
	AiocacheCached,
	AiocacheCachedStampede,
	AiocacheMultiCached
}

impl LruDecorator {
	fn from_qualified_name(name: &[str]) -> Option<Self> {
		match qualified_name.segments() {
            ["functools", "lru_cache"] => Some(Self::FunctoolsLruCache),
						...
            _ => None,
	}
}

impl std::fmt::Display for LruDecorator {
	fn fmt(f: &mut std::fmt::Formatter) -> std::fmt::Result {
		match self {
			Self::FuncToolsLruCache => f.write_str("functools.lru_cache"),
			...
		}
	}
}

Copy link
Author

Choose a reason for hiding this comment

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

Oh, really, thanks!
I used to think about allocation

/// Returns `Some(<decorator_name>)` if the given expression is one of the known
/// cache decorators, otherwise `None`.
fn get_cache_decorator_name(expr: &Expr, semantic: &SemanticModel) -> Option<String> {
if let Some(qualified_name) = semantic.resolve_qualified_name(expr) {
Copy link
Member

Choose a reason for hiding this comment

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

We have to gate this new behavior behind preview mode because it's a considerable extension of the rule's intent (checker.settings().preview.is_enabled())

Copy link
Author

Choose a reason for hiding this comment

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

I don't understand how to write insta tests in this case, could you explain it to me or give an example?

@MichaReiser MichaReiser added the needs-decision Awaiting a decision from a maintainer label Mar 3, 2025
@LuckySting
Copy link
Author

LuckySting commented Mar 3, 2025

I'm not sure async-lru meets that bar

Hello! Here the download statistics for these two:
async-lru More then 20kk downloads last month
aiocache About 2kk downloads last month

It seems that async-lru just a young lib, which is getting popular, and aiocache is old-but-gold one. What do you think, could we include them both now?

@LuckySting LuckySting force-pushed the extend-flake8-bugbear-with-async-decorators branch 3 times, most recently from 88febe6 to 9a5d515 Compare March 3, 2025 11:52
@LuckySting LuckySting force-pushed the extend-flake8-bugbear-with-async-decorators branch from 9a5d515 to 218755f Compare March 3, 2025 11:55
@MichaReiser
Copy link
Member

MichaReiser commented Mar 4, 2025

@zanieb, do you know how popular/recommended these libraries are in the Python ecosystem? E.g. are they as popular as attrs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking API change needs-decision Awaiting a decision from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cached-instance-method (B019) for async_lru and aiocache
2 participants