Skip to content

Commit d7c75db

Browse files
committed
chore: mypy type corrections
1 parent 9fb350e commit d7c75db

File tree

10 files changed

+72
-50
lines changed

10 files changed

+72
-50
lines changed

docs/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
sys.path.insert(0, str(doc_path / "_ext"))
1616

1717
# package data
18-
about = {}
18+
about: dict = {}
1919
with open(project_root / "libvcs" / "__about__.py") as fp:
2020
exec(fp.read(), about)
2121

@@ -58,8 +58,8 @@
5858
html_extra_path = ["manifest.json"]
5959
html_favicon = "_static/favicon.ico"
6060
html_theme = "furo"
61-
html_theme_path = []
62-
html_theme_options = {
61+
html_theme_path: list = []
62+
html_theme_options: dict = {
6363
"light_logo": "img/libvcs.svg",
6464
"dark_logo": "img/libvcs.svg",
6565
"footer_icons": [

libvcs/_internal/run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def run(
178178
shell: bool = False,
179179
cwd: Optional[StrOrBytesPath] = None,
180180
env: Optional[_ENV] = None,
181-
universal_newlines: Optional[bool] = None,
181+
universal_newlines: bool = False,
182182
startupinfo: Optional[Any] = None,
183183
creationflags: int = 0,
184184
restore_signals: bool = True,
@@ -262,15 +262,15 @@ def progress_cb(output, timestamp):
262262
umask=umask,
263263
)
264264

265-
all_output = []
265+
all_output: list[str] = []
266266
code = None
267267
line = None
268268
while code is None:
269269
code = proc.poll()
270270

271271
# output = console_to_str(proc.stdout.readline())
272272
# all_output.append(output)
273-
if callback and callable(callback):
273+
if callback and callable(callback) and proc.stderr is not None:
274274
line = console_to_str(proc.stderr.read(128))
275275
if line:
276276
callback(output=line, timestamp=datetime.datetime.now())

libvcs/cmd/git.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def clone(
250250
if template is not None:
251251
local_flags.append(f"--template={template}")
252252
if separate_git_dir is not None:
253-
local_flags.append(f"--separate-git-dir={separate_git_dir}")
253+
local_flags.append(f"--separate-git-dir={separate_git_dir!r}")
254254
if (filter := kwargs.pop("filter", None)) is not None:
255255
local_flags.append(f"--filter={filter}")
256256
if depth is not None:
@@ -388,7 +388,7 @@ def fetch(
388388
local_flags: list[str] = []
389389

390390
if submodule_prefix is not None:
391-
local_flags.append(f"--submodule-prefix={submodule_prefix}")
391+
local_flags.append(f"--submodule-prefix={submodule_prefix!r}")
392392
if (filter := kwargs.pop("filter", None)) is not None:
393393
local_flags.append(f"--filter={filter}")
394394
if depth is not None:
@@ -561,7 +561,7 @@ def rebase(
561561
if onto:
562562
local_flags.extend(["--onto", onto])
563563
if context:
564-
local_flags.extend(["--C", context])
564+
local_flags.extend(["--C", str(context)])
565565

566566
if exec:
567567
local_flags.extend(["--exec", shlex.quote(exec)])
@@ -864,7 +864,7 @@ def pull(
864864
# Fetch-related arguments
865865
#
866866
if submodule_prefix is not None:
867-
local_flags.append(f"--submodule-prefix={submodule_prefix}")
867+
local_flags.append(f"--submodule-prefix={submodule_prefix!r}")
868868
if (filter := kwargs.pop("filter", None)) is not None:
869869
local_flags.append(f"--filter={filter}")
870870
if depth is not None:
@@ -1007,7 +1007,7 @@ def init(
10071007
if template is not None:
10081008
local_flags.append(f"--template={template}")
10091009
if separate_git_dir is not None:
1010-
local_flags.append(f"--separate-git-dir={separate_git_dir}")
1010+
local_flags.append(f"--separate-git-dir={separate_git_dir!r}")
10111011
if object_format is not None:
10121012
local_flags.append(f"--object-format={object_format}")
10131013
if branch is not None:
@@ -1164,7 +1164,7 @@ def reset(
11641164
if refresh is True:
11651165
local_flags.append("--refresh")
11661166
if pathspec_from_file is not None:
1167-
local_flags.append(f"--pathspec_from_file={pathspec_from_file}")
1167+
local_flags.append(f"--pathspec_from_file={pathspec_from_file!r}")
11681168

11691169
# HEAD to commit form
11701170
if soft is True:

libvcs/cmd/svn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def auth(
257257

258258
def blame(
259259
self,
260-
target: pathlib.Path,
260+
target: StrOrBytesPath,
261261
*,
262262
revision: Union[RevisionLiteral, str] = None,
263263
verbose: Optional[bool] = None,

libvcs/conftest.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from faker import Faker
1212

1313
from libvcs._internal.run import run, which
14-
from libvcs.projects.git import GitProject, GitRemoteDict
14+
from libvcs.projects.git import GitProject, GitRemote
1515

1616
skip_if_git_missing = pytest.mark.skipif(
1717
not which("git"), reason="git is not available"
@@ -322,11 +322,10 @@ def git_repo(projects_path: pathlib.Path, git_remote_repo: pathlib.Path):
322322
url=f"file://{git_remote_repo}",
323323
dir=str(projects_path / "git_repo"),
324324
remotes={
325-
"origin": GitRemoteDict(
326-
**{
327-
"push_url": f"file://{git_remote_repo}",
328-
"fetch_url": f"file://{git_remote_repo}",
329-
}
325+
"origin": GitRemote(
326+
name="origin",
327+
push_url=f"file://{git_remote_repo}",
328+
fetch_url=f"file://{git_remote_repo}",
330329
)
331330
},
332331
)

libvcs/projects/git.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def to_tuple(self):
5353

5454
GitProjectRemoteDict = Dict[str, GitRemote]
5555
GitFullRemoteDict = Dict[str, GitRemoteDict]
56-
GitRemotesArgs = Union[None, GitFullRemoteDict, Dict[str, str]]
56+
GitRemotesArgs = Union[None, GitFullRemoteDict, GitProjectRemoteDict, Dict[str, str]]
5757

5858

5959
@dataclasses.dataclass
@@ -123,6 +123,9 @@ def from_stdout(cls, value: str):
123123
re.VERBOSE | re.MULTILINE,
124124
)
125125
matches = pattern.search(value)
126+
127+
if matches is None:
128+
raise Exception("Could not find match")
126129
return cls(**matches.groupdict())
127130

128131

@@ -154,6 +157,7 @@ def convert_pip_url(pip_url: str) -> VCSLocation:
154157
class GitProject(BaseProject):
155158
bin_name = "git"
156159
schemes = ("git", "git+http", "git+https", "git+ssh", "git+git", "git+file")
160+
_remotes: GitProjectRemoteDict
157161

158162
def __init__(
159163
self, *, url: str, dir: StrPath, remotes: GitRemotesArgs = None, **kwargs
@@ -226,7 +230,11 @@ def __init__(
226230
)
227231
elif isinstance(remote_url, dict):
228232
self._remotes[remote_name] = GitRemote(
229-
**{**remote_url, "name": remote_name}
233+
**{
234+
"fetch_url": remote_url["fetch_url"],
235+
"push_url": remote_url["push_url"],
236+
"name": remote_name,
237+
}
230238
)
231239
elif isinstance(remote_url, GitRemote):
232240
self._remotes[remote_name] = remote_url
@@ -238,13 +246,15 @@ def __init__(
238246
push_url=url,
239247
)
240248
super().__init__(url=url, dir=dir, **kwargs)
241-
self.url = self.chomp_protocol(
242-
(
243-
self._remotes.get("origin")
244-
if "origin" in self._remotes
245-
else next(iter(self._remotes.items()))[1]
246-
).fetch_url
249+
250+
origin = (
251+
self._remotes.get("origin")
252+
if "origin" in self._remotes
253+
else next(iter(self._remotes.items()))[1]
247254
)
255+
if origin is None:
256+
raise Exception("Missing origin")
257+
self.url = self.chomp_protocol(origin.fetch_url)
248258

249259
@classmethod
250260
def from_pip_url(cls, pip_url, **kwargs):
@@ -376,6 +386,8 @@ def update_repo(self, set_remotes: bool = False, *args, **kwargs):
376386
show_ref_output,
377387
re.MULTILINE,
378388
)
389+
if m is None:
390+
raise exc.CommandError("Could not fetch remote names")
379391
git_remote_name = m.group("git_remote_name")
380392
git_tag = m.group("git_tag")
381393
self.log.debug("git_remote_name: %s" % git_remote_name)
@@ -497,15 +509,15 @@ def remotes(self, flat=False) -> Dict:
497509
remotes = {}
498510

499511
cmd = self.run(["remote"])
500-
ret = filter(None, cmd.split("\n"))
512+
ret: filter[str] = filter(None, cmd.split("\n"))
501513

502514
for remote_name in ret:
503-
remotes[remote_name] = (
504-
self.remote(remote_name) if flat else self.remote(remote_name).to_dict()
505-
)
515+
remote = self.remote(remote_name)
516+
if remote is not None:
517+
remotes[remote_name] = remote if flat else remote.to_dict()
506518
return remotes
507519

508-
def remote(self, name, **kwargs) -> GitRemote:
520+
def remote(self, name, **kwargs) -> Optional[GitRemote]:
509521
"""Get the fetch and push URL for a specified remote name.
510522
511523
Parameters

tests/_internal/subprocess/test_SubprocessCommand.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def test_init_and_check_output(args: list, kwargs: dict, expected_result: Any):
132132
ids=idfn,
133133
)
134134
def test_run(tmp_path: pathlib.Path, args: list, kwargs: dict, run_kwargs: dict):
135-
cmd = SubprocessCommand(*args, cwd=tmp_path, **kwargs)
135+
kwargs["cwd"] = tmp_path
136+
cmd = SubprocessCommand(*args, **kwargs)
136137
response = cmd.run(**run_kwargs)
137138

138139
assert response.returncode == 0

tests/projects/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_convert_pip_url():
4343

4444

4545
def test_progress_callback(
46-
capsys: pytest.LogCaptureFixture,
46+
capsys: pytest.CaptureFixture[str],
4747
tmp_path: pathlib.Path,
4848
git_remote_repo: pathlib.Path,
4949
):

tests/projects/test_git.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,15 @@ def test_remotes(
311311
expected = lazy_remote_expected(**locals())
312312
assert len(expected.keys()) > 0
313313
for expected_remote_name, expected_remote_url in expected.items():
314-
assert (
315-
expected_remote_name,
316-
expected_remote_url,
317-
expected_remote_url,
318-
) == git_repo.remote(expected_remote_name).to_tuple()
314+
remote = git_repo.remote(expected_remote_name)
315+
assert remote is not None
316+
317+
if remote is not None:
318+
assert (
319+
expected_remote_name,
320+
expected_remote_url,
321+
expected_remote_url,
322+
) == remote.to_tuple()
319323

320324

321325
@pytest.mark.parametrize(
@@ -424,7 +428,10 @@ def test_remotes_update_repo(
424428
git_repo: GitProject = constructor(**lazy_constructor_options(**locals()))
425429
git_repo.obtain()
426430

427-
git_repo._remotes |= lazy_remote_dict(**locals())
431+
git_repo._remotes |= {
432+
k: GitRemote(*v) if isinstance(v, dict) else v
433+
for k, v in lazy_remote_dict(**locals()).items()
434+
}
428435
git_repo.update_repo(set_remotes=True)
429436

430437
expected = lazy_remote_expected(**locals())
@@ -558,7 +565,10 @@ def test_set_remote(git_repo: GitProject, repo_name: str, new_repo_url: str):
558565
assert isinstance(
559566
git_repo.remote(name=repo_name), GitRemote
560567
), "remote() returns GitRemote"
561-
assert "file:///" in git_repo.remote(name=repo_name).fetch_url, "new value set"
568+
remote = git_repo.remote(name=repo_name)
569+
assert remote is not None, "Remote should exist"
570+
if remote is not None:
571+
assert "file:///" in remote.fetch_url, "new value set"
562572

563573
assert "myrepo" in git_repo.remotes(), ".remotes() returns new remote"
564574

@@ -570,9 +580,12 @@ def test_set_remote(git_repo: GitProject, repo_name: str, new_repo_url: str):
570580

571581
mynewremote = git_repo.set_remote(name="myrepo", url=new_repo_url, overwrite=True)
572582

573-
assert (
574-
new_repo_url in git_repo.remote(name="myrepo").fetch_url
575-
), "Running remove_set should overwrite previous remote"
583+
remote = git_repo.remote(name="myrepo")
584+
assert remote is not None
585+
if remote is not None:
586+
assert (
587+
new_repo_url in remote.fetch_url
588+
), "Running remove_set should overwrite previous remote"
576589

577590

578591
def test_get_git_version(git_repo: GitProject):

tests/projects/test_svn.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from libvcs._internal.run import which
88
from libvcs.conftest import CreateProjectCallbackFixtureProtocol
99
from libvcs.projects.svn import SubversionProject
10-
from libvcs.shortcuts import create_project_from_pip_url
1110

1211
if not which("svn"):
1312
pytestmark = pytest.mark.skip(reason="svn is not available")
@@ -16,11 +15,9 @@
1615
def test_repo_svn(tmp_path: pathlib.Path, svn_remote_repo):
1716
repo_name = "my_svn_project"
1817

19-
svn_repo = create_project_from_pip_url(
20-
**{
21-
"pip_url": f"svn+file://{svn_remote_repo}",
22-
"dir": tmp_path / repo_name,
23-
}
18+
svn_repo = SubversionProject(
19+
url=f"file://{svn_remote_repo}",
20+
dir=str(tmp_path / repo_name),
2421
)
2522

2623
svn_repo.obtain()

0 commit comments

Comments
 (0)