Skip to content

Commit a0accce

Browse files
committed
fix lint errors
1 parent a349bde commit a0accce

File tree

8 files changed

+83
-60
lines changed

8 files changed

+83
-60
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repos:
2020
- id: sort-simple-yaml
2121
- id: trailing-whitespace
2222
- repo: https://github.com/astral-sh/ruff-pre-commit
23-
rev: 'v0.13.1'
23+
rev: 'v0.14.3'
2424
hooks:
2525
- id: ruff-check
2626
args: [--fix, --exit-non-zero-on-fix]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ tests = [
5353
"proxy.py",
5454
]
5555
dev = [
56-
"mypy==1.17.1",
56+
"mypy==1.18.2",
5757
"scmrepo[tests]",
5858
"types-certifi",
5959
"types-mock",

src/scmrepo/git/backend/dulwich/__init__.py

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def clone(
250250
url,
251251
target=to_path,
252252
errstream=(
253-
DulwichProgressReporter(progress) if progress else NoneStream()
253+
DulwichProgressReporter(progress) if progress else NoneStream() # type: ignore[arg-type]
254254
),
255255
bare=bare,
256256
)
@@ -278,8 +278,11 @@ def clone(
278278
def _set_default_tracking_branch(repo: "Repo"):
279279
from dulwich.refs import LOCAL_BRANCH_PREFIX, parse_symref_value
280280

281+
head_ref = repo.refs.read_ref(b"HEAD")
282+
if head_ref is None:
283+
return
281284
try:
282-
ref = parse_symref_value(repo.refs.read_ref(b"HEAD"))
285+
ref = parse_symref_value(head_ref)
283286
except ValueError:
284287
return
285288
if ref.startswith(LOCAL_BRANCH_PREFIX):
@@ -307,7 +310,7 @@ def _set_mirror(
307310
fetch(
308311
repo,
309312
remote_location=b"origin",
310-
errstream=(DulwichProgressReporter(progress) if progress else NoneStream()),
313+
errstream=(DulwichProgressReporter(progress) if progress else NoneStream()), # type: ignore[arg-type]
311314
)
312315

313316
@staticmethod
@@ -599,11 +602,11 @@ def iter_remote_refs(self, url: str, base: Optional[str] = None, **kwargs):
599602
if base:
600603
yield from (
601604
os.fsdecode(ref)
602-
for ref in client.get_refs(path)
605+
for ref in client.get_refs(path.encode())
603606
if ref.startswith(os.fsencode(base))
604607
)
605608
else:
606-
yield from (os.fsdecode(ref) for ref in client.get_refs(path))
609+
yield from (os.fsdecode(ref) for ref in client.get_refs(path.encode()))
607610
except NotGitRepository as exc:
608611
raise InvalidRemote(url) from exc
609612
except HTTPUnauthorized as exc:
@@ -634,7 +637,7 @@ def push_refspecs( # noqa: C901
634637
raise SCMError(f"'{url}' is not a valid Git remote or URL") from exc
635638

636639
change_result = {}
637-
selected_refs = []
640+
selected_refs: list[tuple[Optional[bytes], Optional[bytes], bool]] = []
638641

639642
def update_refs(refs):
640643
from dulwich.objects import ZERO_SHA
@@ -649,6 +652,7 @@ def update_refs(refs):
649652
)
650653
new_refs = {}
651654
for lh, rh, _ in selected_refs:
655+
assert rh is not None
652656
refname = os.fsdecode(rh)
653657
if rh in refs and lh is not None:
654658
if refs[rh] == self.repo.refs[lh]:
@@ -679,9 +683,9 @@ def update_refs(refs):
679683

680684
try:
681685
result = client.send_pack(
682-
path,
686+
path.encode(),
683687
update_refs,
684-
generate_pack_data=self.repo.generate_pack_data,
688+
generate_pack_data=self.repo.generate_pack_data, # type: ignore[arg-type]
685689
progress=(DulwichProgressReporter(progress) if progress else None),
686690
)
687691
except (NotGitRepository, SendPackError) as exc:
@@ -717,7 +721,7 @@ def fetch_refspecs(
717721
from dulwich.porcelain import DivergedBranches, check_diverged, get_remote_repo
718722
from dulwich.refs import DictRefsContainer
719723

720-
fetch_refs = []
724+
fetch_refs: list[tuple[Optional[bytes], Optional[bytes], bool]] = []
721725

722726
def determine_wants(
723727
remote_refs: dict[bytes, bytes],
@@ -736,7 +740,7 @@ def determine_wants(
736740
return [
737741
remote_refs[lh]
738742
for (lh, _, _) in fetch_refs
739-
if remote_refs[lh] not in self.repo.object_store
743+
if lh is not None and remote_refs[lh] not in self.repo.object_store
740744
]
741745

742746
with reraise(Exception, SCMError(f"'{url}' is not a valid Git remote or URL")):
@@ -749,41 +753,42 @@ def determine_wants(
749753
SCMError(f"Git failed to fetch ref from '{url}'"),
750754
):
751755
fetch_result = client.fetch(
752-
path,
756+
path.encode(),
753757
self.repo,
754758
progress=DulwichProgressReporter(progress) if progress else None,
755-
determine_wants=determine_wants,
759+
determine_wants=determine_wants, # type: ignore[arg-type]
756760
)
757761

758762
result = {}
759763

760764
for lh, rh, _ in fetch_refs:
765+
assert rh is not None
761766
refname = os.fsdecode(rh)
767+
assert lh is not None
768+
fetch_ref_lh = fetch_result.refs[lh]
769+
assert fetch_ref_lh is not None
762770
if rh in self.repo.refs:
763-
if self.repo.refs[rh] == fetch_result.refs[lh]:
771+
if self.repo.refs[rh] == fetch_ref_lh:
764772
result[refname] = SyncStatus.UP_TO_DATE
765773
continue
766774
try:
767775
check_diverged(
768776
self.repo,
769777
self.repo.refs[rh],
770-
fetch_result.refs[lh],
778+
fetch_ref_lh,
771779
)
772780
except DivergedBranches:
773781
if not force:
774782
overwrite = (
775-
on_diverged(
776-
os.fsdecode(rh),
777-
os.fsdecode(fetch_result.refs[lh]),
778-
)
783+
on_diverged(os.fsdecode(rh), os.fsdecode(fetch_ref_lh))
779784
if on_diverged
780785
else False
781786
)
782787
if not overwrite:
783788
result[refname] = SyncStatus.DIVERGED
784789
continue
785790

786-
self.repo.refs[rh] = fetch_result.refs[lh]
791+
self.repo.refs[rh] = fetch_ref_lh
787792
result[refname] = SyncStatus.SUCCESS
788793
return result
789794

@@ -865,6 +870,7 @@ def _describe(
865870
return results
866871

867872
def diff(self, rev_a: str, rev_b: str, binary=False) -> str:
873+
from dulwich.objects import Commit
868874
from dulwich.patch import write_tree_diff
869875

870876
try:
@@ -873,6 +879,9 @@ def diff(self, rev_a: str, rev_b: str, binary=False) -> str:
873879
except KeyError as exc:
874880
raise RevError("Invalid revision") from exc
875881

882+
assert isinstance(commit_a, Commit)
883+
assert isinstance(commit_b, Commit)
884+
876885
buf = BytesIO()
877886
write_tree_diff(buf, self.repo.object_store, commit_a.tree, commit_b.tree)
878887
return buf.getvalue().decode("utf-8")
@@ -958,20 +967,22 @@ def get_tag(self, name: str) -> Optional[Union[str, "GitTag"]]:
958967
ref = self.repo.refs[name_b]
959968
except KeyError:
960969
return None
961-
if ref in self.repo and isinstance(self.repo[ref], Tag):
962-
tag = self.repo[ref]
963-
_typ, target_sha = tag.object
964-
tagger_name, tagger_email = _parse_identity(tag.tagger.decode("utf-8"))
965-
return GitTag(
966-
os.fsdecode(tag.name),
967-
tag.id,
968-
target_sha.decode("ascii"),
969-
tagger_name,
970-
tagger_email,
971-
tag.tag_time,
972-
tag.tag_timezone,
973-
tag.message.decode("utf-8"),
974-
)
970+
if ref in self.repo:
971+
shafile = self.repo[ref]
972+
if isinstance(shafile, Tag):
973+
tag = shafile
974+
_typ, target_sha = tag.object
975+
tagger_name, tagger_email = _parse_identity(tag.tagger.decode("utf-8"))
976+
return GitTag(
977+
os.fsdecode(tag.name),
978+
tag.id.decode("ascii"),
979+
target_sha.decode("ascii"),
980+
tagger_name,
981+
tagger_email,
982+
tag.tag_time,
983+
tag.tag_timezone,
984+
tag.message.decode("utf-8"),
985+
)
975986
return os.fsdecode(ref)
976987

977988
def get_config(self, path: Optional[str] = None) -> "Config":
@@ -1000,13 +1011,16 @@ def _parse_identity(identity: str) -> tuple[str, str]:
10001011
return m.group("name"), m.group("email")
10011012

10021013

1003-
def ls_remote(url: str) -> dict[str, str]:
1014+
def ls_remote(url: str) -> dict[str, Optional[str]]:
10041015
from dulwich import porcelain
10051016
from dulwich.client import HTTPUnauthorized
10061017

10071018
try:
10081019
refs = porcelain.ls_remote(url).refs
1009-
return {os.fsdecode(ref): sha.decode("ascii") for ref, sha in refs.items()}
1020+
return {
1021+
os.fsdecode(ref): sha.decode("ascii") if sha is not None else None
1022+
for ref, sha in refs.items()
1023+
}
10101024
except HTTPUnauthorized as exc:
10111025
raise AuthError(url) from exc
10121026
except Exception as exc:

src/scmrepo/git/backend/dulwich/asyncssh_vendor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async def public_key_auth_requested( # noqa: C901
168168
pubkey = read_public_key(pubkey_to_load)
169169
except (OSError, KeyImportError):
170170
pubkey = None
171-
return SSHLocalKeyPair(key, pubkey)
171+
return SSHLocalKeyPair(key, pubkey, cert=None, enc_key=None)
172172
return None
173173

174174
async def _read_private_key_interactive(self, path: "FilePath") -> "SSHKey":

src/scmrepo/git/backend/pygit2/__init__.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
if TYPE_CHECKING:
35-
from pygit2 import Commit, Oid, Signature
35+
from pygit2 import Commit, Oid, Reference, Signature, Tag
3636
from pygit2.config import Config as _Pygit2Config
3737
from pygit2.enums import CheckoutStrategy
3838
from pygit2.remotes import Remote
@@ -67,15 +67,11 @@ def open(
6767
else:
6868
pass
6969
if raw:
70-
blob_kwargs = {}
70+
blobio = BlobIO(self.obj)
7171
else:
7272
assert key is not None
7373
path = "/".join(key)
74-
blob_kwargs = {
75-
"as_path": path,
76-
"commit_id": commit.id,
77-
}
78-
blobio = BlobIO(self.obj, **blob_kwargs)
74+
blobio = BlobIO(self.obj, as_path=path, commit_id=commit.id)
7975
if mode == "rb":
8076
return blobio
8177
return TextIOWrapper(blobio, encoding=encoding)
@@ -190,14 +186,17 @@ def _refdb(self):
190186

191187
return RefdbFsBackend(self.repo)
192188

193-
def _resolve_refish(self, refish: str):
189+
def _resolve_refish(
190+
self, refish: str
191+
) -> tuple["Commit", Union["Reference", "Tag"]]:
194192
from pygit2 import Tag
195193
from pygit2.enums import ObjectType
196194

197-
commit, ref = self.repo.resolve_refish(refish) # type: ignore[attr-defined]
195+
ref: Union[Reference, Tag]
196+
commit, ref = self.repo.resolve_refish(refish)
198197
if isinstance(commit, Tag):
199198
ref = commit
200-
commit = commit.peel(ObjectType.COMMIT) # type: ignore[call-overload]
199+
commit = commit.peel(ObjectType.COMMIT)
201200
return commit, ref
202201

203202
@property
@@ -313,6 +312,7 @@ def _set_mirror(
313312
# duplicate config section for each remote config entry. We just edit
314313
# the config directly so that it creates a single section to be
315314
# consistent with CLI Git
315+
assert url is not None
316316
repo.config["remote.origin.url"] = url
317317
repo.config["remote.origin.fetch"] = "+refs/*:refs/*"
318318
repo.config["remote.origin.mirror"] = True
@@ -347,7 +347,7 @@ def checkout(
347347
force: bool = False,
348348
**kwargs,
349349
):
350-
from pygit2 import GitError
350+
from pygit2 import Commit, GitError
351351
from pygit2.enums import CheckoutStrategy
352352

353353
strategy = self._get_checkout_strategy(
@@ -356,9 +356,10 @@ def checkout(
356356

357357
with self.release_odb_handles():
358358
if create_new:
359-
commit = self.repo.revparse_single("HEAD")
360-
new_branch = self.repo.branches.local.create(branch, commit) # type: ignore[arg-type]
361-
self.repo.checkout(new_branch, strategy=strategy) # type: ignore[attr-defined]
359+
_commit = self.repo.revparse_single("HEAD")
360+
assert isinstance(_commit, Commit)
361+
new_branch = self.repo.branches.local.create(branch, _commit)
362+
self.repo.checkout(new_branch, strategy=strategy)
362363
else:
363364
if branch == "-":
364365
branch = "@{-1}"
@@ -582,7 +583,7 @@ def get_ref(self, name, follow: bool = True) -> Optional[str]:
582583

583584
def remove_ref(self, name: str, old_ref: Optional[str] = None):
584585
ref = self.repo.references.get(name)
585-
if not ref and not old_ref:
586+
if not ref:
586587
return
587588
if old_ref and old_ref != str(ref.target):
588589
raise SCMError(f"Failed to remove '{name}'")
@@ -676,6 +677,7 @@ def _get_remote(self, url: str) -> Iterator["Remote"]:
676677
"""Return a pygit2.Remote suitable for the specified Git URL or remote name."""
677678
try:
678679
remote = self.repo.remotes[url]
680+
assert remote.url is not None
679681
url = remote.url
680682
except ValueError:
681683
pass
@@ -685,6 +687,7 @@ def _get_remote(self, url: str) -> Iterator["Remote"]:
685687
if os.name == "nt":
686688
url = url.removeprefix("file://")
687689
remote = self.repo.remotes.create_anonymous(url)
690+
assert remote.url is not None
688691
parsed = urlparse(remote.url)
689692
if parsed.scheme in ("git", "git+ssh", "ssh") or is_scp_style_url(remote.url):
690693
raise NotImplementedError
@@ -838,7 +841,7 @@ def _apply(index):
838841
_apply(i)
839842
return
840843

841-
self.set_ref(Stash.DEFAULT_STASH, commit.id, message=commit.message)
844+
self.set_ref(Stash.DEFAULT_STASH, commit.id, message=commit.message) # type: ignore[arg-type]
842845
try:
843846
_apply(0)
844847
finally:
@@ -936,7 +939,9 @@ def checkout_index(
936939
assert self.root_dir
937940
path = os.path.join(self.root_dir, entry.path)
938941
with open(path, "wb") as fobj:
939-
fobj.write(self.repo.get(entry.id).read_raw()) # type: ignore[attr-defined]
942+
obj = self.repo.get(entry.id)
943+
assert obj is not None
944+
fobj.write(obj.read_raw())
940945
index.add(entry.path)
941946
index.write()
942947

@@ -1074,7 +1079,9 @@ def validate_git_remote(self, url: str, **kwargs):
10741079

10751080
def get_remote_url(self, remote: str) -> str:
10761081
try:
1077-
return self.repo.remotes[remote].url
1082+
url = self.repo.remotes[remote].url
1083+
assert url is not None
1084+
return url
10781085
except KeyError as exc:
10791086
raise InvalidRemote(remote) from exc
10801087

src/scmrepo/git/lfs/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def _git_lfs_authenticate(
267267
action = "upload" if upload else "download"
268268
return json.loads(
269269
self._ssh.run_command(
270-
command=f"git-lfs-authenticate {path} {action}",
270+
command=f"git-lfs-authenticate {path} {action}".encode(),
271271
host=host,
272272
port=port,
273273
username=username,

0 commit comments

Comments
 (0)