Skip to content

Commit 375f238

Browse files
committed
Improve typing: add Branches to Repository
The following command mypy test reports Found 280 errors in 8 files (checked 50 source files) after this change.
1 parent 4b3f85d commit 375f238

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

pygit2/_pygit2.pyi

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class Branch(Reference):
126126
def delete(self) -> None: ...
127127
def is_checked_out(self) -> bool: ...
128128
def is_head(self) -> bool: ...
129-
def rename(self, name: str, force: bool = False) -> None: ...
129+
def rename(self, name: str, force: bool = False) -> 'Branch': ... # type: ignore[override]
130130

131131
class Commit(Object):
132132
author: Signature
@@ -390,6 +390,23 @@ class RemoteCollection:
390390
def add_fetch(self, name: str, refspec: str) -> None: ...
391391
def add_push(self, name: str, refspec: str) -> None: ...
392392

393+
class Branches:
394+
local: 'Branches'
395+
remote: 'Branches'
396+
def __init__(
397+
self,
398+
repository: BaseRepository,
399+
flag: BranchType = ...,
400+
commit: Commit | _OidArg | None = None,
401+
) -> None: ...
402+
def __getitem__(self, name: str) -> Branch: ...
403+
def get(self, key: str) -> Branch: ...
404+
def __iter__(self) -> Iterator[str]: ...
405+
def create(self, name: str, commit: Commit, force: bool = False) -> Branch: ...
406+
def delete(self, name: str) -> None: ...
407+
def with_commit(self, commit: Commit | _OidArg | None) -> 'Branches': ...
408+
def __contains__(self, name: _OidArg) -> bool: ...
409+
393410
class Repository:
394411
_pointer: bytes
395412
default_signature: Signature
@@ -405,6 +422,7 @@ class Repository:
405422
workdir: str
406423
references: References
407424
remotes: RemoteCollection
425+
branches: Branches
408426
def __init__(self, *args, **kwargs) -> None: ...
409427
def TreeBuilder(self, src: Tree | _OidArg = ...) -> TreeBuilder: ...
410428
def _disown(self, *args, **kwargs) -> None: ...

test/test_branch.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import pytest
3030
import os
3131
from pygit2.enums import BranchType
32+
from pygit2 import Repository
3233

3334

3435
LAST_COMMIT = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
@@ -38,7 +39,7 @@
3839
SHARED_COMMIT = '4ec4389a8068641da2d6578db0419484972284c8'
3940

4041

41-
def test_branches_getitem(testrepo):
42+
def test_branches_getitem(testrepo: Repository) -> None:
4243
branch = testrepo.branches['master']
4344
assert branch.target == LAST_COMMIT
4445

@@ -49,12 +50,12 @@ def test_branches_getitem(testrepo):
4950
testrepo.branches['not-exists']
5051

5152

52-
def test_branches(testrepo):
53+
def test_branches(testrepo: Repository) -> None:
5354
branches = sorted(testrepo.branches)
5455
assert branches == ['i18n', 'master']
5556

5657

57-
def test_branches_create(testrepo):
58+
def test_branches_create(testrepo: Repository) -> None:
5859
commit = testrepo[LAST_COMMIT]
5960
reference = testrepo.branches.create('version1', commit)
6061
assert 'version1' in testrepo.branches
@@ -70,53 +71,53 @@ def test_branches_create(testrepo):
7071
assert reference.target == LAST_COMMIT
7172

7273

73-
def test_branches_delete(testrepo):
74+
def test_branches_delete(testrepo: Repository) -> None:
7475
testrepo.branches.delete('i18n')
7576
assert testrepo.branches.get('i18n') is None
7677

7778

78-
def test_branches_delete_error(testrepo):
79+
def test_branches_delete_error(testrepo: Repository) -> None:
7980
with pytest.raises(pygit2.GitError):
8081
testrepo.branches.delete('master')
8182

8283

83-
def test_branches_is_head(testrepo):
84+
def test_branches_is_head(testrepo: Repository) -> None:
8485
branch = testrepo.branches.get('master')
8586
assert branch.is_head()
8687

8788

88-
def test_branches_is_not_head(testrepo):
89+
def test_branches_is_not_head(testrepo: Repository) -> None:
8990
branch = testrepo.branches.get('i18n')
9091
assert not branch.is_head()
9192

9293

93-
def test_branches_rename(testrepo):
94+
def test_branches_rename(testrepo: Repository) -> None:
9495
new_branch = testrepo.branches['i18n'].rename('new-branch')
9596
assert new_branch.target == I18N_LAST_COMMIT
9697

9798
new_branch_2 = testrepo.branches.get('new-branch')
9899
assert new_branch_2.target == I18N_LAST_COMMIT
99100

100101

101-
def test_branches_rename_error(testrepo):
102+
def test_branches_rename_error(testrepo: Repository) -> None:
102103
original_branch = testrepo.branches.get('i18n')
103104
with pytest.raises(ValueError):
104105
original_branch.rename('master')
105106

106107

107-
def test_branches_rename_force(testrepo):
108+
def test_branches_rename_force(testrepo: Repository) -> None:
108109
original_branch = testrepo.branches.get('master')
109110
new_branch = original_branch.rename('i18n', True)
110111
assert new_branch.target == LAST_COMMIT
111112

112113

113-
def test_branches_rename_invalid(testrepo):
114+
def test_branches_rename_invalid(testrepo: Repository) -> None:
114115
original_branch = testrepo.branches.get('i18n')
115116
with pytest.raises(ValueError):
116117
original_branch.rename('abc@{123')
117118

118119

119-
def test_branches_name(testrepo):
120+
def test_branches_name(testrepo: Repository) -> None:
120121
branch = testrepo.branches.get('master')
121122
assert branch.branch_name == 'master'
122123
assert branch.name == 'refs/heads/master'
@@ -128,7 +129,7 @@ def test_branches_name(testrepo):
128129
assert branch.raw_branch_name == branch.branch_name.encode('utf-8')
129130

130131

131-
def test_branches_with_commit(testrepo):
132+
def test_branches_with_commit(testrepo: Repository) -> None:
132133
branches = testrepo.branches.with_commit(EXCLUSIVE_MASTER_COMMIT)
133134
assert sorted(branches) == ['master']
134135
assert branches.get('i18n') is None

0 commit comments

Comments
 (0)