{"record":{"id":"0c190a945dd30351","repo":"krahets/hello-algo","slug":"error-0c190a","errorCode":null,"errorMessage":"スタックが空です","messagePattern":"スタックが空です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/codes/python/chapter_stack_and_queue/array_stack.py","lineNumber":30,"sourceCode":"        \"\"\"コンストラクタ\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"スタックの長さを取得\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"スタックが空かどうかを判定\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"プッシュ\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"ポップ\"\"\"\n        if self.is_empty():\n            raise IndexError(\"スタックが空です\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"スタックトップの要素にアクセス\"\"\"\n        if self.is_empty():\n            raise IndexError(\"スタックが空です\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"表示用のリストを返す\"\"\"\n        return self._stack\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # スタックを初期化\n    stack = ArrayStack()\n","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/python/chapter_stack_and_queue/array_stack.py#L12-L48","documentation":"This IndexError (Japanese: 'スタックが空です' = 'stack is empty') is raised by ArrayStack.pop() (array_stack.py:30) when size() == 0. ArrayStack wraps a Python list; the guard prevents calling list.pop() on an empty backing list (which would itself raise IndexError with a different message). push() is unbounded (list.append grows dynamically), so 'full' is never an issue here.","triggerScenarios":"Calling pop() on a freshly constructed ArrayStack (empty list). Calling pop() more times than push(). Popping in a loop without an is_empty() check.","commonSituations":"Unbalanced push/pop in expression evaluation or backtracking. Draining the stack to empty then popping once more. Assuming a prior push succeeded when it actually was skipped by upstream logic.","solutions":["Check `if not stack.is_empty(): stack.pop()`.","Loop with `while not stack.is_empty():` for full drain.","Track expected depth separately and never pop below it.","Use try/except IndexError if pop-on-empty is a benign case in your algorithm."],"exampleFix":"// before\ntop = stack.pop()  # IndexError when empty\n\n// after\ntop = stack.pop() if not stack.is_empty() else None","handlingStrategy":"validation","validationCode":"if not stack.is_empty():\n    top = stack.pop()","typeGuard":"def stack_has_top(stack: ArrayStack) -> bool:\n    return not stack.is_empty()","tryCatchPattern":"try:\n    top = stack.pop()\nexcept IndexError:\n    top = None","preventionTips":["Guard every pop() with is_empty().","Drain with `while not stack.is_empty():`.","Track push/pop balance in expression evaluators and backtracking.","push() is unbounded (list.append), so only underflow, never overflow, can occur."],"tags":["stack","python","index-error","empty","array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}