Title: Incorrect return type hint in `SymbolicReference.reference` leads to mypy errors · Issue #2066 · gitpython-developers/GitPython · GitHub
Open Graph Title: Incorrect return type hint in `SymbolicReference.reference` leads to mypy errors · Issue #2066 · gitpython-developers/GitPython
X Title: Incorrect return type hint in `SymbolicReference.reference` leads to mypy errors · Issue #2066 · gitpython-developers/GitPython
Description: Since GitPython 3.1.45 was released, we've had mypy reporting pre-checkin errors for code that has been stable and functional for years: error: "SymbolicReference" has no attribute "checkout" [attr-defined] error: "SymbolicReference" has...
Open Graph Description: Since GitPython 3.1.45 was released, we've had mypy reporting pre-checkin errors for code that has been stable and functional for years: error: "SymbolicReference" has no attribute "checkout" [attr...
X Description: Since GitPython 3.1.45 was released, we've had mypy reporting pre-checkin errors for code that has been stable and functional for years: error: "SymbolicReference" has no attribute &q...
Opengraph URL: https://github.com/gitpython-developers/GitPython/issues/2066
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect return type hint in `SymbolicReference.reference` leads to mypy errors","articleBody":"Since GitPython 3.1.45 was released, we've had `mypy` reporting pre-checkin errors for code that has been stable and functional for years:\n```\nerror: \"SymbolicReference\" has no attribute \"checkout\" [attr-defined]\nerror: \"SymbolicReference\" has no attribute \"tracking_branch\" [attr-defined]\nerror: \"SymbolicReference\" has no attribute \"tracking_branch\" [attr-defined]\n```\n\nThe code failing `mypy` checks is doing things like getting the active branch, so we can switch back to it after doing other stuff:\n```\n @contextmanager\n def checkout(self, branch: Branch) -\u003e typing.Generator[None]:\n new_branch = self.api.create_head(branch.name)\n current = self.api.head.ref\n try:\n new_branch.checkout()\n yield\n finally:\n current.checkout()\n```\n\n... or ensuring that the active branch has a remote tracking branch set:\n```\n tracking_branch = repo.head.reference.tracking_branch()\n if tracking_branch is None:\n raise GitConfigException(\n 'Error: Upstream branch not set; use \"git branch '\n '--set-upstream-to=\u003cremote/branch\u003e\" to fix.'\n )\n if tracking_branch.remote_name == \".\":\n raise GitConfigException(\n 'Error: Upstream branch is local rather than remote; use \"git '\n 'branch --set-upstream-to=\u003cremote/branch\u003e\" to fix.'\n )\n``` \n\nThe immediate cause of these new `mypy` errors appears to be [this commit](https://github.com/gitpython-developers/GitPython/commit/94151aa2ca9f16491a0cf2344b4daa8bf7b41d70#diff-72d42177f0e8535634711b5de0390d5bc81dbedab1fbac6547e5b9ab3b03eb9aL52), which added a new type hint for the return value of `SymbolicReference.reference`:\n```\n @property\n def reference(self) -\u003e \"SymbolicReference\":\n return self._get_reference()\n```\n\nIn manual testing I've found that the `reference` method doesn't actually return a `SymbolicReference` object when the the symbolic reference is `HEAD`. Instead, a `Head` object is returned:\n```\n\u003e\u003e\u003e import git\n\u003e\u003e\u003e git.__version__\n'3.1.45'\n\u003e\u003e\u003e testrepo = git.repo.Repo.init(\".\")\n\u003e\u003e\u003e type(testrepo.head)\n\u003cclass 'git.refs.head.HEAD'\u003e\n\u003e\u003e\u003e type(testrepo.head.ref)\n\u003cclass 'git.refs.head.Head'\u003e\n\u003e\u003e\u003e testrepo.head.ref.tracking_branch()\n\u003cgit.RemoteReference \"refs/remotes/./master\"\u003e\n\u003e\u003e\u003e testrepo.head.ref.checkout()\n\u003cgit.Head \"refs/heads/bar\"\u003e\n```","author":{"url":"https://github.com/dvanderveer","@type":"Person","name":"dvanderveer"},"datePublished":"2025-08-08T20:57:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/2066/GitPython/issues/2066"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:dd71e4fc-32fa-6abd-7b8b-f706e0cdb563 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B380:1C762F:5E7126:84B530:6968DEE0 |
| html-safe-nonce | 0f1738dc2b0a1902f9b2748e8592d7c623a731fb2d26b98e0647622552b1b670 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzgwOjFDNzYyRjo1RTcxMjY6ODRCNTMwOjY5NjhERUUwIiwidmlzaXRvcl9pZCI6IjY3NzI5NzQ2MzY3MTQ0NzI2NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 407d7502e863ddc6d5efa26520509b56497089a8761c09b9b5a4d3750ba2f856 |
| hovercard-subject-tag | issue:3305196079 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/gitpython-developers/GitPython/2066/issue_layout |
| twitter:image | https://opengraph.githubassets.com/72e3e975f60d75270ed9d6e64438f9388e439f5db4b10d007eabf607ed49e83f/gitpython-developers/GitPython/issues/2066 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/72e3e975f60d75270ed9d6e64438f9388e439f5db4b10d007eabf607ed49e83f/gitpython-developers/GitPython/issues/2066 |
| og:image:alt | Since GitPython 3.1.45 was released, we've had mypy reporting pre-checkin errors for code that has been stable and functional for years: error: "SymbolicReference" has no attribute "checkout" [attr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dvanderveer |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3c4e9f2fc1db52c609205138be6a397cfde4574cb54d53015014de30dd49f27e |
| turbo-cache-control | no-preview |
| go-import | github.com/gitpython-developers/GitPython git https://github.com/gitpython-developers/GitPython.git |
| octolytics-dimension-user_id | 503709 |
| octolytics-dimension-user_login | gitpython-developers |
| octolytics-dimension-repository_id | 1126087 |
| octolytics-dimension-repository_nwo | gitpython-developers/GitPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1126087 |
| octolytics-dimension-repository_network_root_nwo | gitpython-developers/GitPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 1fb1ea11f8c37dd4dd50347cca70d89824e93788 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width