PullRequest.delete_branch fails with AttributeError when branch is already deleted
Author: mschoettleCreated Mar 10, 2025Updated Sep 1, 2026
I am cleaning up merged branches and two PRs used the same source branch name. When calling delete_branch on the second PR the following error is raised due to a GitRef(ref=None):
Traceback (most recent call last):
File "path/to/project/gh_branch_deleter.py", line 31, in <module>
pull.delete_branch()
File "path/to/cache/uv/environments-v2/gh-branch-deleter-55d50c1f1548d2bb/lib/python3.12/site-packages/github/PullRequest.py", line 851, in delete_branch
return self.head.repo.get_git_ref(f"heads/{self.head.ref}").delete()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path/to/cache/uv/environments-v2/gh-branch-deleter-55d50c1f1548d2bb/lib/python3.12/site-packages/github/GitRef.py", line 99, in delete
headers, data = self._requester.requestJsonAndCheck("DELETE", self.url)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path/to/cache/uv/environments-v2/gh-branch-deleter-55d50c1f1548d2bb/lib/python3.12/site-packages/github/Requester.py", line 631, in requestJsonAndCheck
self.__customConnection(url),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path/to/cache/uv/environments-v2/gh-branch-deleter-55d50c1f1548d2bb/lib/python3.12/site-packages/github/Requester.py", line 799, in __customConnection
if not url.startswith("/"):
^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'startswith'Tested this with pygithub==2.6.1
For some reason self.head.repo.get_git_ref(f"heads/{self.head.ref}") returns GitRef(ref=None) instead of raising a 404 with Not found which would be caught by __check.
Source: PyGithub/PyGithub