python system_error ai_generated true

RecursionError: maximum recursion depth exceeded while calling a Python object

ID: python/recursionerror-maximum-recursion-depth

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-11-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active

Root Cause

A test function or fixture calls itself recursively without a base case, or a mock setup causes infinite recursion.

generic

中文

测试函数或 fixture 在没有基本情况的情况下递归调用自身,或模拟设置导致无限递归。

Workarounds

  1. 95% success Add a base case to recursive function
    def recursive_func(n):
        if n <= 0: return
        recursive_func(n-1)
  2. 90% success Use iterative approach instead of recursion
    def iterative_func(n):
        while n > 0:
            n -= 1

Dead Ends

Common approaches that don't work:

  1. Increasing recursion limit with sys.setrecursionlimit 70% fail

    Postpones the problem; may cause stack overflow or crash.

  2. Ignoring the error 90% fail

    The test will always fail until recursion is fixed.