Skip to content

Commit 3c5ac56

Browse files
committed
Remove deprecated oid.hex, use str(oid)
1 parent 95bbd1f commit 3c5ac56

16 files changed

+83
-105
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
Breaking changes:
1818

19+
- Remove deprecated `oid.hex`, use `str(oid)`
20+
1921
- Remove deprecated `Repository.add_submodule(...)`, use `Repository.submodules.add(...)`
2022
- Remove deprecated `Repository.lookup_submodule(...)`, use `Repository.submodules[...]`
2123
- Remove deprecated `Repository.init_submodules(...)`, use `Repository.submodules.init(...)`

pygit2/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ def oid(self):
366366
@property
367367
def hex(self):
368368
"""The id of the referenced object as a hex string"""
369-
return self.id.hex
369+
return str(self.id)
370370

371371
def __str__(self):
372372
return f'<path={self.path} id={self.hex} mode={self.mode}>'

src/oid.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,8 @@ Oid_raw__get__(Oid *self)
254254
}
255255

256256

257-
PyDoc_STRVAR(Oid_hex__doc__, "Hex oid, a 40 chars long string (type str).\n"
258-
"This attribute is deprecated, please use the built-in str()\n");
259-
260-
PyObject *
261-
Oid_hex__get__(Oid *self)
262-
{
263-
return Oid__str__(self);
264-
}
265-
266257
PyGetSetDef Oid_getseters[] = {
267258
GETTER(Oid, raw),
268-
GETTER(Oid, hex),
269259
{NULL},
270260
};
271261

test/test_blob.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@
8383
def test_read_blob(testrepo):
8484
blob = testrepo[BLOB_SHA]
8585
assert blob.hex == BLOB_SHA
86-
sha = blob.id.hex
87-
assert sha == BLOB_SHA
86+
assert str(blob.id) == BLOB_SHA
8887
assert isinstance(blob, pygit2.Blob)
8988
assert not blob.is_binary
9089
assert ObjectType.BLOB == blob.type
@@ -101,7 +100,7 @@ def test_create_blob(testrepo):
101100
assert ObjectType.BLOB == blob.type
102101

103102
assert blob_oid == blob.id
104-
assert utils.gen_blob_sha1(BLOB_NEW_CONTENT) == blob_oid.hex
103+
assert utils.gen_blob_sha1(BLOB_NEW_CONTENT) == str(blob_oid)
105104

106105
assert BLOB_NEW_CONTENT == blob.data
107106
assert len(BLOB_NEW_CONTENT) == blob.size
@@ -125,7 +124,7 @@ def test_create_blob_fromworkdir(testrepo):
125124
assert ObjectType.BLOB == blob.type
126125

127126
assert blob_oid == blob.id
128-
assert utils.gen_blob_sha1(BLOB_FILE_CONTENT) == blob_oid.hex
127+
assert utils.gen_blob_sha1(BLOB_FILE_CONTENT) == str(blob_oid)
129128

130129
assert BLOB_FILE_CONTENT == blob.data
131130
assert len(BLOB_FILE_CONTENT) == blob.size
@@ -164,7 +163,7 @@ def test_create_blob_fromiobase(testrepo):
164163
assert ObjectType.BLOB == blob.type
165164

166165
assert blob_oid == blob.id
167-
assert BLOB_SHA == blob_oid.hex
166+
assert BLOB_SHA == str(blob_oid)
168167

169168

170169
def test_diff_blob(testrepo):

test/test_branch.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040

4141
def test_branches_getitem(testrepo):
4242
branch = testrepo.branches['master']
43-
assert branch.target.hex == LAST_COMMIT
43+
assert str(branch.target) == LAST_COMMIT
4444

4545
branch = testrepo.branches.local['i18n']
46-
assert branch.target.hex == I18N_LAST_COMMIT
46+
assert str(branch.target) == I18N_LAST_COMMIT
4747
assert testrepo.branches.get('not-exists') is None
4848
with pytest.raises(KeyError):
4949
testrepo.branches['not-exists']
@@ -59,15 +59,15 @@ def test_branches_create(testrepo):
5959
reference = testrepo.branches.create('version1', commit)
6060
assert 'version1' in testrepo.branches
6161
reference = testrepo.branches['version1']
62-
assert reference.target.hex == LAST_COMMIT
62+
assert str(reference.target) == LAST_COMMIT
6363

6464
# try to create existing reference
6565
with pytest.raises(ValueError):
6666
testrepo.branches.create('version1', commit)
6767

6868
# try to create existing reference with force
6969
reference = testrepo.branches.create('version1', commit, True)
70-
assert reference.target.hex == LAST_COMMIT
70+
assert str(reference.target) == LAST_COMMIT
7171

7272

7373
def test_branches_delete(testrepo):
@@ -92,10 +92,10 @@ def test_branches_is_not_head(testrepo):
9292

9393
def test_branches_rename(testrepo):
9494
new_branch = testrepo.branches['i18n'].rename('new-branch')
95-
assert new_branch.target.hex == I18N_LAST_COMMIT
95+
assert str(new_branch.target) == I18N_LAST_COMMIT
9696

9797
new_branch_2 = testrepo.branches.get('new-branch')
98-
assert new_branch_2.target.hex == I18N_LAST_COMMIT
98+
assert str(new_branch_2.target) == I18N_LAST_COMMIT
9999

100100

101101
def test_branches_rename_error(testrepo):
@@ -107,7 +107,7 @@ def test_branches_rename_error(testrepo):
107107
def test_branches_rename_force(testrepo):
108108
original_branch = testrepo.branches.get('master')
109109
new_branch = original_branch.rename('i18n', True)
110-
assert new_branch.target.hex == LAST_COMMIT
110+
assert str(new_branch.target) == LAST_COMMIT
111111

112112

113113
def test_branches_rename_invalid(testrepo):
@@ -154,14 +154,14 @@ def test_branches_with_commit(testrepo):
154154

155155
def test_lookup_branch_local(testrepo):
156156
branch = testrepo.lookup_branch('master')
157-
assert branch.target.hex == LAST_COMMIT
157+
assert str(branch.target) == LAST_COMMIT
158158
branch = testrepo.lookup_branch(b'master')
159-
assert branch.target.hex == LAST_COMMIT
159+
assert str(branch.target) == LAST_COMMIT
160160

161161
branch = testrepo.lookup_branch('i18n', BranchType.LOCAL)
162-
assert branch.target.hex == I18N_LAST_COMMIT
162+
assert str(branch.target) == I18N_LAST_COMMIT
163163
branch = testrepo.lookup_branch(b'i18n', BranchType.LOCAL)
164-
assert branch.target.hex == I18N_LAST_COMMIT
164+
assert str(branch.target) == I18N_LAST_COMMIT
165165

166166
assert testrepo.lookup_branch('not-exists') is None
167167
assert testrepo.lookup_branch(b'not-exists') is None
@@ -183,17 +183,17 @@ def test_create_branch(testrepo):
183183
refs = testrepo.listall_branches()
184184
assert 'version1' in refs
185185
reference = testrepo.lookup_branch('version1')
186-
assert reference.target.hex == LAST_COMMIT
186+
assert str(reference.target) == LAST_COMMIT
187187
reference = testrepo.lookup_branch(b'version1')
188-
assert reference.target.hex == LAST_COMMIT
188+
assert str(reference.target) == LAST_COMMIT
189189

190190
# try to create existing reference
191191
with pytest.raises(ValueError):
192192
testrepo.create_branch('version1', commit)
193193

194194
# try to create existing reference with force
195195
reference = testrepo.create_branch('version1', commit, True)
196-
assert reference.target.hex == LAST_COMMIT
196+
assert str(reference.target) == LAST_COMMIT
197197

198198

199199
def test_delete(testrepo):
@@ -233,10 +233,10 @@ def test_branch_is_checked_out_returns_false_if_branch_is_not_checked_out(testre
233233
def test_branch_rename_succeeds(testrepo):
234234
original_branch = testrepo.lookup_branch('i18n')
235235
new_branch = original_branch.rename('new-branch')
236-
assert new_branch.target.hex == I18N_LAST_COMMIT
236+
assert str(new_branch.target) == I18N_LAST_COMMIT
237237

238238
new_branch_2 = testrepo.lookup_branch('new-branch')
239-
assert new_branch_2.target.hex == I18N_LAST_COMMIT
239+
assert str(new_branch_2.target) == I18N_LAST_COMMIT
240240

241241

242242
def test_branch_rename_fails_if_destination_already_exists(testrepo):
@@ -248,7 +248,7 @@ def test_branch_rename_fails_if_destination_already_exists(testrepo):
248248
def test_branch_rename_not_fails_if_force_is_true(testrepo):
249249
original_branch = testrepo.lookup_branch('master')
250250
new_branch = original_branch.rename('i18n', True)
251-
assert new_branch.target.hex == LAST_COMMIT
251+
assert str(new_branch.target) == LAST_COMMIT
252252

253253

254254
def test_branch_rename_fails_with_invalid_names(testrepo):

test/test_branch_empty.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26-
import pygit2
2726
import pytest
2827
from pygit2.enums import BranchType
2928

@@ -40,7 +39,7 @@ def repo(emptyrepo):
4039

4140
def test_branches_remote_get(repo):
4241
branch = repo.branches.remote.get('origin/master')
43-
assert branch.target.hex == ORIGIN_MASTER_COMMIT
42+
assert str(branch.target) == ORIGIN_MASTER_COMMIT
4443

4544
assert repo.branches.remote.get('origin/not-exists') is None
4645

@@ -57,7 +56,7 @@ def test_branches_remote_getitem(repo):
5756

5857
def test_branches_upstream(repo):
5958
remote_master = repo.branches.remote['origin/master']
60-
master = repo.branches.create('master', repo[remote_master.target.hex])
59+
master = repo.branches.create('master', repo[remote_master.target])
6160

6261
assert master.upstream is None
6362
master.upstream = remote_master
@@ -75,7 +74,7 @@ def set_bad_upstream():
7574

7675
def test_branches_upstream_name(repo):
7776
remote_master = repo.branches.remote['origin/master']
78-
master = repo.branches.create('master', repo[remote_master.target.hex])
77+
master = repo.branches.create('master', repo[remote_master.target])
7978

8079
master.upstream = remote_master
8180
assert master.upstream_name == 'refs/remotes/origin/master'
@@ -85,10 +84,9 @@ def test_branches_upstream_name(repo):
8584
# Low level API written in C, repo.remotes call these.
8685
#
8786

88-
8987
def test_lookup_branch_remote(repo):
9088
branch = repo.lookup_branch('origin/master', BranchType.REMOTE)
91-
assert branch.target.hex == ORIGIN_MASTER_COMMIT
89+
assert str(branch.target) == ORIGIN_MASTER_COMMIT
9290

9391
assert repo.lookup_branch('origin/not-exists', BranchType.REMOTE) is None
9492

@@ -108,7 +106,7 @@ def test_branch_remote_name(repo):
108106

109107
def test_branch_upstream(repo):
110108
remote_master = repo.lookup_branch('origin/master', BranchType.REMOTE)
111-
master = repo.create_branch('master', repo[remote_master.target.hex])
109+
master = repo.create_branch('master', repo[remote_master.target])
112110

113111
assert master.upstream is None
114112
master.upstream = remote_master
@@ -126,7 +124,7 @@ def set_bad_upstream():
126124

127125
def test_branch_upstream_name(repo):
128126
remote_master = repo.lookup_branch('origin/master', BranchType.REMOTE)
129-
master = repo.create_branch('master', repo[remote_master.target.hex])
127+
master = repo.create_branch('master', repo[remote_master.target])
130128

131129
master.upstream = remote_master
132130
assert master.upstream_name == 'refs/remotes/origin/master'

test/test_diff.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,16 @@ def test_diff_ids(barerepo):
278278
commit_b = barerepo[COMMIT_SHA1_2]
279279
patch = commit_a.tree.diff_to_tree(commit_b.tree)[0]
280280
delta = patch.delta
281-
assert delta.old_file.id.hex == '7f129fd57e31e935c6d60a0c794efe4e6927664b'
282-
assert delta.new_file.id.hex == 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
281+
assert str(delta.old_file.id) == '7f129fd57e31e935c6d60a0c794efe4e6927664b'
282+
assert str(delta.new_file.id) == 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
283283

284284

285285
def test_diff_patchid(barerepo):
286286
commit_a = barerepo[COMMIT_SHA1_1]
287287
commit_b = barerepo[COMMIT_SHA1_2]
288288
diff = commit_a.tree.diff_to_tree(commit_b.tree)
289289
assert diff.patch == PATCH
290-
assert diff.patchid.hex == PATCHID
290+
assert str(diff.patchid) == PATCHID
291291

292292

293293
def test_hunk_content(barerepo):

test/test_index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ def test_read_tree(testrepo):
148148
assert len(index) == 1
149149
# Test read-write returns the same oid
150150
oid = index.write_tree()
151-
assert oid.hex == tree_oid
151+
assert str(oid) == tree_oid
152152
# Test the index is only modified in memory
153153
index.read()
154154
assert len(index) == 2
155155

156156

157157
def test_write_tree(testrepo):
158158
oid = testrepo.index.write_tree()
159-
assert oid.hex == 'fd937514cb799514d4b81bb24c5fcfeb6472b245'
159+
assert str(oid) == 'fd937514cb799514d4b81bb24c5fcfeb6472b245'
160160

161161

162162
def test_iter(testrepo):

test/test_note.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,31 @@ def test_create_note(barerepo):
4949
annotated_id = barerepo.revparse_single('HEAD~3').hex
5050
author = committer = Signature('Foo bar', '[email protected]', 12346, 0)
5151
note_id = barerepo.create_note(NOTE[1], author, committer, annotated_id)
52-
assert NOTE[0] == note_id.hex
52+
assert NOTE[0] == str(note_id)
5353

5454
# check the note blob
5555
assert NOTE[1].encode() == barerepo[note_id].data
5656

5757

5858
def test_lookup_note(barerepo):
59-
annotated_id = barerepo.head.target.hex
59+
annotated_id = str(barerepo.head.target)
6060
note = barerepo.lookup_note(annotated_id)
61-
assert NOTES[0][0] == note.id.hex
61+
assert NOTES[0][0] == str(note.id)
6262
assert NOTES[0][1] == note.message
6363

6464

6565
def test_remove_note(barerepo):
6666
head = barerepo.head
67-
note = barerepo.lookup_note(head.target.hex)
67+
note = barerepo.lookup_note(str(head.target))
6868
author = committer = Signature('Foo bar', '[email protected]', 12346, 0)
6969
note.remove(author, committer)
7070
with pytest.raises(KeyError):
71-
barerepo.lookup_note(head.target.hex)
71+
barerepo.lookup_note(str(head.target))
7272

7373

7474
def test_iterate_notes(barerepo):
7575
for i, note in enumerate(barerepo.notes()):
76-
entry = (note.id.hex, note.message, note.annotated_id.hex)
77-
assert NOTES[i] == entry
76+
assert NOTES[i] == (str(note.id), note.message, str(note.annotated_id))
7877

7978

8079
def test_iterate_non_existing_ref(barerepo):

test/test_object.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ def test_short_id(testrepo):
123123

124124
def test_obj(obj, msg):
125125
short_id = obj.short_id
126-
msg = msg + ' short_id=' + short_id
126+
msg = msg + f' short_id={short_id}'
127127
already = seen.get(short_id)
128128
if already:
129-
assert already == obj.id.hex
129+
assert already == str(obj.id)
130130
else:
131-
seen[short_id] = obj.id.hex
131+
seen[short_id] = str(obj.id)
132132
lookup = testrepo[short_id]
133133
assert obj.id == lookup.id
134134

135135
for commit in testrepo.walk(testrepo.head.target):
136-
test_obj(commit, 'commit#' + commit.id.hex)
136+
test_obj(commit, f'commit#{commit.id}')
137137
tree = commit.tree
138-
test_obj(tree, 'tree#' + tree.id.hex)
138+
test_obj(tree, f'tree#{tree.id}')
139139
for entry in tree:
140-
test_obj(testrepo[entry.hex], 'entry=' + entry.name + '#' + entry.hex)
140+
test_obj(testrepo[entry.hex], f'entry={entry.name}#{entry.hex}')
141141

142142

143143
def test_repr(testrepo):

0 commit comments

Comments
 (0)