# RecursionError: 调用 Python 对象时超出最大递归深度

- **ID:** `python/pytest-recursion-error-in-fixture`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

夹具直接或间接依赖自身，导致夹具解析期间无限递归。这通常发生在夹具名称与其请求的参数名称相同时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   @pytest.fixture
def db_connection():
    return create_connection()

@pytest.fixture
def db_session(db_connection):  # Different name
    return Session(db_connection)
   ```
2. **** (85% 成功率)
   ```
   @pytest.fixture
def my_fixture(request):
    other = request.getfixturevalue('other_fixture')
    return process(other)
   ```

## 无效尝试

- **** — This delays the inevitable crash and may cause a stack overflow, potentially crashing the Python interpreter. (90% 失败率)
- **** — RecursionError occurs during fixture resolution, before the fixture body executes. The try/except inside the fixture never catches it. (95% 失败率)
