Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
@dvanderveer

Description

@dvanderveer

Since GitPython 3.1.45 was released, we've hadmypy 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 failingmypy 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 newmypy errors appears to bethis commit, which added a new type hint for the return value ofSymbolicReference.reference:

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

In manual testing I've found that thereference method doesn't actually return aSymbolicReference object when the the symbolic reference isHEAD. Instead, aHead 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">

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp