python.lang.correctness.cannot-cache-generators.cannot-cache-generators

profile photo of semgrepsemgrep
Author
unknown
Download Count*

Generators can only be consumed once, so in most cases, caching them will cause an error when the already-consumed generator is retrieved from cache.

Run Locally

Run in CI

Defintion

rules:
  - id: cannot-cache-generators
    patterns:
      - pattern-inside: |
          @functools.lru_cache(...)
          def $FUNC(...):
              ...
              yield ...
      - pattern: functools.lru_cache(...)
    message: Generators can only be consumed once, so in most cases, caching them
      will cause an error when the already-consumed generator is retrieved from
      cache.
    languages:
      - python
    severity: WARNING
    metadata:
      category: correctness
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

cannot-cache-generators.py

import functools
from functools import lru_cache


# ok: cannot-cache-generators
@functools.lru_cache(maxsize=10)
def not_a_generator():
    return 1

# ok: cannot-cache-generators
@lru_cache(maxsize=10)
def not_a_generator():
    return 1


# ok: cannot-cache-generators
@lru_cache
def not_a_generator():
    return 1


# ruleid: cannot-cache-generators
@functools.lru_cache(maxsize=10)
def generator():
    yield 1

# ruleid: cannot-cache-generators
@lru_cache(maxsize=10)
def generator():
    yield 1


# ruleid: cannot-cache-generators
@lru_cache
def generator():
    yield 1