Skip to content

Commit edcccfc

Browse files
committed
improve types according to test/test_refs.py
1 parent f8d9a05 commit edcccfc

File tree

2 files changed

+51
-39
lines changed

2 files changed

+51
-39
lines changed

pygit2/_pygit2.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,11 @@ class Repository:
845845
force: bool = False,
846846
) -> Oid: ...
847847
def create_reference(
848-
self, name: str, target: _OidArg, force: bool = False
848+
self,
849+
name: str,
850+
target: _OidArg,
851+
force: bool = False,
852+
message: str | None = None,
849853
) -> Reference: ...
850854
def create_reference_direct(
851855
self, name: str, target: _OidArg, force: bool, message: Optional[str] = None
@@ -893,6 +897,7 @@ class Repository:
893897
def listall_branches(self, flag: BranchType = BranchType.LOCAL) -> list[str]: ...
894898
def listall_mergeheads(self) -> list[Oid]: ...
895899
def listall_references(self) -> list[str]: ...
900+
def listall_reference_objects(self) -> list[Reference]: ...
896901
def listall_stashes(self) -> list[Stash]: ...
897902
def listall_submodules(self) -> list[str]: ...
898903
def lookup_branch(
@@ -962,6 +967,7 @@ class Repository:
962967
references_return_type: ReferenceFilter = ReferenceFilter.ALL,
963968
) -> Reference: ...
964969
def reset(self, oid: _OidArg, reset_type: ResetMode) -> None: ...
970+
def resolve_refish(self, refresh: str) -> tuple[Commit, Reference]: ...
965971
def revparse(self, revspec: str) -> RevSpec: ...
966972
def revparse_ext(self, revision: str) -> tuple[Object, Reference]: ...
967973
def revparse_single(self, revision: str) -> Object: ...

test/test_refs.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@
3434
Commit,
3535
GitError,
3636
InvalidSpecError,
37+
Oid,
3738
Repository,
3839
Signature,
3940
Tree,
4041
reference_is_valid_name,
4142
)
42-
from pygit2.enums import ReferenceType
43+
from pygit2.enums import ReferenceFilter, ReferenceType
4344

4445
LAST_COMMIT = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
4546

4647

47-
def test_refs_list_objects(testrepo):
48+
def test_refs_list_objects(testrepo: Repository) -> None:
4849
refs = [(ref.name, ref.target) for ref in testrepo.references.objects]
4950
assert sorted(refs) == [
5051
('refs/heads/i18n', '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
@@ -292,7 +293,7 @@ def test_refs_compress(testrepo: Repository) -> None:
292293
#
293294

294295

295-
def test_list_all_reference_objects(testrepo):
296+
def test_list_all_reference_objects(testrepo: Repository) -> None:
296297
repo = testrepo
297298
refs = [(ref.name, ref.target) for ref in repo.listall_reference_objects()]
298299

@@ -302,7 +303,7 @@ def test_list_all_reference_objects(testrepo):
302303
]
303304

304305

305-
def test_list_all_references(testrepo):
306+
def test_list_all_references(testrepo: Repository) -> None:
306307
repo = testrepo
307308

308309
# Without argument
@@ -326,14 +327,14 @@ def test_list_all_references(testrepo):
326327
]
327328

328329

329-
def test_references_iterator_init(testrepo):
330+
def test_references_iterator_init(testrepo: Repository) -> None:
330331
repo = testrepo
331332
iter = repo.references_iterator_init()
332333

333334
assert iter.__class__.__name__ == 'RefsIterator'
334335

335336

336-
def test_references_iterator_next(testrepo):
337+
def test_references_iterator_next(testrepo: Repository) -> None:
337338
repo = testrepo
338339
repo.create_reference(
339340
'refs/tags/version1', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
@@ -359,7 +360,9 @@ def test_references_iterator_next(testrepo):
359360
iter_branches = repo.references_iterator_init()
360361
all_branches = []
361362
for _ in range(4):
362-
curr_ref = repo.references_iterator_next(iter_branches, 1)
363+
curr_ref = repo.references_iterator_next(
364+
iter_branches, ReferenceFilter.BRANCHES
365+
)
363366
if curr_ref:
364367
all_branches.append((curr_ref.name, curr_ref.target))
365368

@@ -371,7 +374,7 @@ def test_references_iterator_next(testrepo):
371374
iter_tags = repo.references_iterator_init()
372375
all_tags = []
373376
for _ in range(4):
374-
curr_ref = repo.references_iterator_next(iter_tags, 2)
377+
curr_ref = repo.references_iterator_next(iter_tags, ReferenceFilter.TAGS)
375378
if curr_ref:
376379
all_tags.append((curr_ref.name, curr_ref.target))
377380

@@ -381,7 +384,7 @@ def test_references_iterator_next(testrepo):
381384
]
382385

383386

384-
def test_references_iterator_next_python(testrepo):
387+
def test_references_iterator_next_python(testrepo: Repository) -> None:
385388
repo = testrepo
386389
repo.create_reference(
387390
'refs/tags/version1', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
@@ -398,41 +401,43 @@ def test_references_iterator_next_python(testrepo):
398401
('refs/tags/version2', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'),
399402
]
400403

401-
branches = [(x.name, x.target) for x in repo.references.iterator(1)]
404+
branches = [
405+
(x.name, x.target) for x in repo.references.iterator(ReferenceFilter.BRANCHES)
406+
]
402407
assert sorted(branches) == [
403408
('refs/heads/i18n', '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
404409
('refs/heads/master', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'),
405410
]
406411

407-
tags = [(x.name, x.target) for x in repo.references.iterator(2)]
412+
tags = [(x.name, x.target) for x in repo.references.iterator(ReferenceFilter.TAGS)]
408413
assert sorted(tags) == [
409414
('refs/tags/version1', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'),
410415
('refs/tags/version2', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'),
411416
]
412417

413418

414-
def test_references_iterator_invalid_filter(testrepo):
419+
def test_references_iterator_invalid_filter(testrepo: Repository) -> None:
415420
repo = testrepo
416421
iter_all = repo.references_iterator_init()
417422

418423
all_refs = []
419424
for _ in range(4):
420-
curr_ref = repo.references_iterator_next(iter_all, 5)
425+
curr_ref = repo.references_iterator_next(iter_all, 5) # type: ignore
421426
if curr_ref:
422427
all_refs.append((curr_ref.name, curr_ref.target))
423428

424429
assert all_refs == []
425430

426431

427-
def test_references_iterator_invalid_filter_python(testrepo):
432+
def test_references_iterator_invalid_filter_python(testrepo: Repository) -> None:
428433
repo = testrepo
429434
refs = []
430435
with pytest.raises(ValueError):
431-
for ref in repo.references.iterator(5):
436+
for ref in repo.references.iterator(5): # type: ignore
432437
refs.append((ref.name, ref.target))
433438

434439

435-
def test_lookup_reference(testrepo):
440+
def test_lookup_reference(testrepo: Repository) -> None:
436441
repo = testrepo
437442

438443
# Raise KeyError ?
@@ -444,7 +449,7 @@ def test_lookup_reference(testrepo):
444449
assert reference.name == 'refs/heads/master'
445450

446451

447-
def test_lookup_reference_dwim(testrepo):
452+
def test_lookup_reference_dwim(testrepo: Repository) -> None:
448453
repo = testrepo
449454

450455
# remote ref
@@ -474,7 +479,7 @@ def test_lookup_reference_dwim(testrepo):
474479
assert reference.name == 'refs/tags/version1'
475480

476481

477-
def test_resolve_refish(testrepo):
482+
def test_resolve_refish(testrepo: Repository) -> None:
478483
repo = testrepo
479484

480485
# remote ref
@@ -516,37 +521,37 @@ def test_resolve_refish(testrepo):
516521
assert commit.id == '5ebeeebb320790caf276b9fc8b24546d63316533'
517522

518523

519-
def test_reference_get_sha(testrepo):
524+
def test_reference_get_sha(testrepo: Repository) -> None:
520525
reference = testrepo.lookup_reference('refs/heads/master')
521526
assert reference.target == LAST_COMMIT
522527

523528

524-
def test_reference_set_sha(testrepo):
529+
def test_reference_set_sha(testrepo: Repository) -> None:
525530
NEW_COMMIT = '5ebeeebb320790caf276b9fc8b24546d63316533'
526531
reference = testrepo.lookup_reference('refs/heads/master')
527532
reference.set_target(NEW_COMMIT)
528533
assert reference.target == NEW_COMMIT
529534

530535

531-
def test_reference_set_sha_prefix(testrepo):
536+
def test_reference_set_sha_prefix(testrepo: Repository) -> None:
532537
NEW_COMMIT = '5ebeeebb320790caf276b9fc8b24546d63316533'
533538
reference = testrepo.lookup_reference('refs/heads/master')
534539
reference.set_target(NEW_COMMIT[0:6])
535540
assert reference.target == NEW_COMMIT
536541

537542

538-
def test_reference_get_type(testrepo):
543+
def test_reference_get_type(testrepo: Repository) -> None:
539544
reference = testrepo.lookup_reference('refs/heads/master')
540545
assert reference.type == ReferenceType.DIRECT
541546

542547

543-
def test_get_target(testrepo):
548+
def test_get_target(testrepo: Repository) -> None:
544549
reference = testrepo.lookup_reference('HEAD')
545550
assert reference.target == 'refs/heads/master'
546551
assert reference.raw_target == b'refs/heads/master'
547552

548553

549-
def test_set_target(testrepo):
554+
def test_set_target(testrepo: Repository) -> None:
550555
reference = testrepo.lookup_reference('HEAD')
551556
assert reference.target == 'refs/heads/master'
552557
assert reference.raw_target == b'refs/heads/master'
@@ -555,14 +560,14 @@ def test_set_target(testrepo):
555560
assert reference.raw_target == b'refs/heads/i18n'
556561

557562

558-
def test_get_shorthand(testrepo):
563+
def test_get_shorthand(testrepo: Repository) -> None:
559564
reference = testrepo.lookup_reference('refs/heads/master')
560565
assert reference.shorthand == 'master'
561566
reference = testrepo.create_reference('refs/remotes/origin/master', LAST_COMMIT)
562567
assert reference.shorthand == 'origin/master'
563568

564569

565-
def test_set_target_with_message(testrepo):
570+
def test_set_target_with_message(testrepo: Repository) -> None:
566571
reference = testrepo.lookup_reference('HEAD')
567572
assert reference.target == 'refs/heads/master'
568573
assert reference.raw_target == b'refs/heads/master'
@@ -577,7 +582,7 @@ def test_set_target_with_message(testrepo):
577582
assert first.committer == sig
578583

579584

580-
def test_delete(testrepo):
585+
def test_delete(testrepo: Repository) -> None:
581586
repo = testrepo
582587

583588
# We add a tag as a new reference that points to "origin/master"
@@ -605,15 +610,15 @@ def test_delete(testrepo):
605610
reference.rename('refs/tags/version2')
606611

607612

608-
def test_rename(testrepo):
613+
def test_rename(testrepo: Repository) -> None:
609614
# We add a tag as a new reference that points to "origin/master"
610615
reference = testrepo.create_reference('refs/tags/version1', LAST_COMMIT)
611616
assert reference.name == 'refs/tags/version1'
612617
reference.rename('refs/tags/version2')
613618
assert reference.name == 'refs/tags/version2'
614619

615620

616-
# def test_reload(testrepo):
621+
# def test_reload(testrepo: Repository) -> None:
617622
# name = 'refs/tags/version1'
618623

619624
# repo = testrepo
@@ -625,21 +630,21 @@ def test_rename(testrepo):
625630
# with pytest.raises(GitError): getattr(ref2, 'name')
626631

627632

628-
def test_reference_resolve(testrepo):
633+
def test_reference_resolve(testrepo: Repository) -> None:
629634
reference = testrepo.lookup_reference('HEAD')
630635
assert reference.type == ReferenceType.SYMBOLIC
631636
reference = reference.resolve()
632637
assert reference.type == ReferenceType.DIRECT
633638
assert reference.target == LAST_COMMIT
634639

635640

636-
def test_reference_resolve_identity(testrepo):
641+
def test_reference_resolve_identity(testrepo: Repository) -> None:
637642
head = testrepo.lookup_reference('HEAD')
638643
ref = head.resolve()
639644
assert ref.resolve() is ref
640645

641646

642-
def test_create_reference(testrepo):
647+
def test_create_reference(testrepo: Repository) -> None:
643648
# We add a tag as a new reference that points to "origin/master"
644649
reference = testrepo.create_reference('refs/tags/version1', LAST_COMMIT)
645650
assert 'refs/tags/version1' in testrepo.listall_references()
@@ -660,7 +665,7 @@ def test_create_reference(testrepo):
660665
assert reference.target == LAST_COMMIT
661666

662667

663-
def test_create_reference_with_message(testrepo):
668+
def test_create_reference_with_message(testrepo: Repository) -> None:
664669
sig = Signature('foo', 'bar')
665670
testrepo.set_ident('foo', 'bar')
666671
msg = 'Hello log'
@@ -672,7 +677,7 @@ def test_create_reference_with_message(testrepo):
672677
assert first.committer == sig
673678

674679

675-
def test_create_symbolic_reference(testrepo):
680+
def test_create_symbolic_reference(testrepo: Repository) -> None:
676681
repo = testrepo
677682
# We add a tag as a new symbolic reference that always points to
678683
# "refs/heads/master"
@@ -693,7 +698,7 @@ def test_create_symbolic_reference(testrepo):
693698
assert reference.raw_target == b'refs/heads/master'
694699

695700

696-
def test_create_symbolic_reference_with_message(testrepo):
701+
def test_create_symbolic_reference_with_message(testrepo: Repository) -> None:
697702
sig = Signature('foo', 'bar')
698703
testrepo.set_ident('foo', 'bar')
699704
msg = 'Hello log'
@@ -705,7 +710,7 @@ def test_create_symbolic_reference_with_message(testrepo):
705710
assert first.committer == sig
706711

707712

708-
def test_create_invalid_reference(testrepo):
713+
def test_create_invalid_reference(testrepo: Repository) -> None:
709714
repo = testrepo
710715

711716
# try to create a reference with an invalid name
@@ -714,14 +719,15 @@ def test_create_invalid_reference(testrepo):
714719
assert isinstance(error.value, ValueError)
715720

716721

717-
# def test_packall_references(testrepo):
722+
# def test_packall_references(testrepo: Repository) -> None:
718723
# testrepo.packall_references()
719724

720725

721-
def test_peel(testrepo):
726+
def test_peel(testrepo: Repository) -> None:
722727
repo = testrepo
723728
ref = repo.lookup_reference('refs/heads/master')
724729
assert repo[ref.target].id == ref.peel().id
730+
assert isinstance(ref.raw_target, Oid)
725731
assert repo[ref.raw_target].id == ref.peel().id
726732

727733
commit = ref.peel(Commit)

0 commit comments

Comments
 (0)