GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Incorrect return type hint in `SymbolicReference.reference` leads to mypy errors · Issue #2066 · gitpython-developers/GitPython

Incorrect return type hint in SymbolicReference.reference leads to mypy errors #2066

New issue
Copy link
New issue
Copy link
Closed
#2074
Closed
Incorrect return type hint in SymbolicReference.reference leads to mypy errors#2066
#2074
Copy link
Labels
acknowledged

Description

@dvanderveer
dvanderveer
opened on Aug 8, 2025
Issue body actions

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 no attribute "tracking_branch"  [attr-defined]
error: "SymbolicReference" has no attribute "tracking_branch"  [attr-defined]

The code failing mypy checks is doing things like getting the active branch, so we can switch back to it after doing other stuff:

    @contextmanager
    def checkout(self, branch: Branch) -> typing.Generator[None]:
        new_branch = self.api.create_head(branch.name)
        current = self.api.head.ref
        try:
            new_branch.checkout()
            yield
        finally:
            current.checkout()

... or ensuring that the active branch has a remote tracking branch set:

    tracking_branch = repo.head.reference.tracking_branch()
    if tracking_branch is None:
        raise GitConfigException(
            'Error: Upstream branch not set; use "git branch '
            '--set-upstream-to=<remote/branch>" to fix.'
        )
    if tracking_branch.remote_name == ".":
        raise GitConfigException(
            'Error: Upstream branch is local rather than remote; use "git '
            'branch --set-upstream-to=<remote/branch>" to fix.'
        )

The immediate cause of these new mypy errors appears to be this commit, which added a new type hint for the return value of SymbolicReference.reference:

    @property
    def reference(self) -> "SymbolicReference":
        return self._get_reference()

In 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:

>>> import git
>>> git.__version__
'3.1.45'
>>> testrepo = git.repo.Repo.init(".")
>>> type(testrepo.head)
<class 'git.refs.head.HEAD'>
>>> type(testrepo.head.ref)
<class 'git.refs.head.Head'>
>>> testrepo.head.ref.tracking_branch()
<git.RemoteReference "refs/remotes/./master">
>>> testrepo.head.ref.checkout()
<git.Head "refs/heads/bar">

Activity

  1. Byron
    added
    acknowledged
    on Aug 9, 2025
  2. Byron commented on Aug 9, 2025

    @Byron
    Byron
    on Aug 9, 2025
    Member
    More actions

    Thanks a lot for reporting!

    Is this something you know how to fix, or should the commit be reverted?

    In any case, a PR is welcome.

  3. dvanderveer commented on Aug 11, 2025

    @dvanderveer
    dvanderveer
    on Aug 11, 2025
    Author
    More actions

    @Byron I can take a crack at it. I think it might be as simple as changing the type hint to parent class Reference, or maybe typing.Union[Head, SymbolicReference].

    Reacted by Sebastian Thiel
  4. Byron
    closed this as completedin #2074on Sep 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.