From c1811ded3f634be827aef68623f72d823b175a28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EB=B0=94=EB=A6=84?= <48576227+rumbarum@users.noreply.github.com> Date: Mon, 15 Aug 2022 18:36:56 +0900 Subject: [PATCH] fix method pop for stack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pyright version 1.1.266, mypy 0.971 에서 Type Check시 에러가 발생하네요 pyright error - error: "item" is not a known member of "None" (reportOptionalMemberAccess) - error: "pointer" is not a known member of "None" (reportOptionalMemberAccess) - error: Function with declared type of "T@Stack" must return value   Type "None" cannot be assigned to type "T@Stack" (reportGeneralTypeIssues) mypy error - error: Item "None" of "Optional[Node[Any]]" has no attribute "item" 해당 에러들 안나는 방향으로 수정 했습니다. 수정 해보니 pyright랑 mypy 랑 잡는 부분이 다르긴 하네요. --- .../03-linked-list.py" | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git "a/03 \353\263\264\353\204\210\354\212\244 - \353\260\260\354\232\264 \352\262\203 \354\235\221\354\232\251\355\225\230\352\270\260/03-linked-list.py" "b/03 \353\263\264\353\204\210\354\212\244 - \353\260\260\354\232\264 \352\262\203 \354\235\221\354\232\251\355\225\230\352\270\260/03-linked-list.py" index 4d928f7..1fefd76 100644 --- "a/03 \353\263\264\353\204\210\354\212\244 - \353\260\260\354\232\264 \352\262\203 \354\235\221\354\232\251\355\225\230\352\270\260/03-linked-list.py" +++ "b/03 \353\263\264\353\204\210\354\212\244 - \353\260\260\354\232\264 \352\262\203 \354\235\221\354\232\251\355\225\230\352\270\260/03-linked-list.py" @@ -47,18 +47,20 @@ def push(self, item: T) -> None: cur_node = cur_node.pointer cur_node.pointer = new_node - def pop(self) -> T: + def pop(self) -> Optional[T]: if self.head is None: - raise ValueError("stack is empty") - cur_node = self.head + raise ValueError("Stack is empty") + cur_node: Node[T] = self.head if cur_node.pointer is None: self.head = None return cur_node.item - while cur_node.pointer.pointer is not None: - cur_node = cur_node.pointer - result = cur_node.pointer - cur_node.pointer = None - return result.item + while cur_node.pointer is not None: + if cur_node.pointer.pointer is not None: + cur_node = cur_node.pointer + continue + result = cur_node.pointer + cur_node.pointer = None + return result.item if result is not None else None class Queue(Generic[T], LinkedList[T]):