Skip to content
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

cached-instance-method (B019) gives false positive when decorated method belongs to enum.Enum #9912

Closed
jakob-keller opened this issue Feb 9, 2024 · 2 comments · Fixed by #11312
Assignees
Labels
rule Implementing or modifying a lint rule

Comments

@jakob-keller
Copy link

jakob-keller commented Feb 9, 2024

What do you think about the following edge case? I believe this should not be reported, since Enum members are typically low cardinality and are not garbage collected anyway.

# test.py

import enum
import functools


class Foo(enum.Enum):
    ONE = enum.auto()
    TWO = enum.auto()

    @functools.cache
    def bar(self, arg: str) -> str:
        return f"{self} - {arg}"
$ ruff --version
ruff 0.2.1

$ ruff check --isolated --select B test.py
test.py:9:5: B019 Use of `functools.lru_cache` or `functools.cache` on methods can lead to memory leaks
Found 1 error.
@AlexWaygood AlexWaygood added the rule Implementing or modifying a lint rule label Feb 9, 2024
@charliermarsh
Copy link
Member

I think I agree with this, though I'd love other opinions / confirmations that this is safe.

@charliermarsh
Copy link
Member

Confirmed with Alex and Carl that this should be fine.

@charliermarsh charliermarsh self-assigned this May 6, 2024
charliermarsh added a commit that referenced this issue May 6, 2024
…B019`) (#11312)

## Summary

While I was here, I also updated the rule to use
`function_type::classify` rather than hard-coding `staticmethod` and
friends.

Per Carl:

> Enum instances are already referred to by the class, forming a cycle
that won't get collected until the class itself does. At which point the
`lru_cache` itself would be collected, too.

Closes #9912.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants