{
  "id": "python/unittest-mock-patch-target-not-found",
  "signature": "AttributeError: <module 'myapp.services' from '/app/services.py'> does not have the attribute 'requests'",
  "signature_zh": "AttributeError: <module 'myapp.services' from '/app/services.py'> 没有属性 'requests'",
  "regex": "AttributeError:\\ <module\\ 'myapp\\.services'\\ from\\ '/app/services\\.py'>\\ does\\ not\\ have\\ the\\ attribute\\ 'requests'",
  "domain": "python",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "unittest.mock.patch targets the name where it is looked up, not where it is defined. If services.py does `import requests` and calls `requests.get`, patch 'myapp.services.requests.get'. If it does `from requests import get`, patch 'myapp.services.get'.",
  "root_cause_type": "generic",
  "root_cause_zh": "unittest.mock.patch 针对的是名称被查找的位置，而非定义位置。若 services.py 使用 `import requests` 并调用 `requests.get`，应 patch 'myapp.services.requests.get'；若使用 `from requests import get`，则应 patch 'myapp.services.get'。",
  "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": "Patches the global requests module, not the reference the SUT uses; the real network call still fires.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Creates a separate binding; the SUT's binding remains untouched.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Identify the binding style in the SUT and patch accordingly:\n  # if SUT: import requests\n  @patch('myapp.services.requests')\n  def test_get(mock_req): ...\n  # if SUT: from requests import get\n  @patch('myapp.services.get')\n  def test_get(mock_get): ...",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use pytest-mock's mocker fixture which resolves the same lookup semantics:\n  def test_get(mocker):\n      mocker.patch('myapp.services.requests.get')",
      "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-01-19",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}