{
  "id": "python/pytest-unittest-mock-patch-decorator-order",
  "signature": "TypeError: test_x() missing 1 required positional argument: 'mock_b'\nDuring handling of the above exception, another exception occurred:\nTypeError: test_x() takes 1 positional argument but 3 were given",
  "signature_zh": "TypeError: test_x() 缺少 1 个必需的位置参数: 'mock_b'\n处理上述异常时又出现另一个异常:\nTypeError: test_x() 接受 1 个位置参数但传入了 3 个",
  "regex": "TypeError:\\ test_x\\(\\)\\ missing\\ 1\\ required\\ positional\\ argument:\\ 'mock_b'\\\nDuring\\ handling\\ of\\ the\\ above\\ exception,\\ another\\ exception\\ occurred:\\\nTypeError:\\ test_x\\(\\)\\ takes\\ 1\\ positional\\ argument\\ but\\ 3\\ were\\ given",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "Stacked @patch decorators inject mocks in bottom-up order, but the test function parameters were written top-down (or vice versa). This mismatches mock-to-parameter assignment.",
  "root_cause_type": "generic",
  "root_cause_zh": "堆叠的 @patch 装饰器按自下而上的顺序注入 mock，但测试函数参数按自上而下（或相反）编写。这导致 mock 与参数对应关系错位。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "unittest passes mocks positionally; *args accepts them but the names inside the body no longer refer to the intended mocks.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Non-deterministic across refactors; masks the real order rule.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Remember: decorators apply bottom-up, so the LAST decorator is the FIRST parameter:\n  @patch('a.foo')\n  @patch('a.bar')\n  def test_x(mock_bar, mock_foo): ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.93,
      "how": "Use context managers instead of decorators to avoid ordering confusion:\n  def test_x():\n      with patch('a.foo') as mf, patch('a.bar') as mb:\n          ...",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-02-14",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}