Skip to content

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

@jakob-keller

Description

@jakob-keller

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.

Metadata

Metadata

Assignees

Labels

ruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions