Skip to content

Commit bede2d7

Browse files
committed
type: add type annotations to AssetRepo class
Add type annotations to all methods in AssetRepo. Changes: - Add types to __init__(), parse_shortcut_repo_url() - Add types to get_remote_url(), make_default_branch() - Add instance variable type annotations
1 parent aa118d6 commit bede2d7

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

md2zhihu/config/asset_reop.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hashlib
22
import os
33
import re
4+
from typing import Optional
45

56
import k3git
67
from k3handy import CMD_RAISE_STDOUT
@@ -11,12 +12,12 @@
1112

1213

1314
class AssetRepo(object):
14-
is_local = False
15+
is_local: bool = False
1516

16-
def __init__(self, repo_url, cdn=True):
17+
def __init__(self, repo_url: str, cdn: bool = True) -> None:
1718
# TODO: test rendering md rendering with pushed assets
1819

19-
self.cdn = cdn
20+
self.cdn: bool = cdn
2021

2122
repo_url = self.parse_shortcut_repo_url(repo_url)
2223

@@ -35,7 +36,7 @@ def __init__(self, repo_url, cdn=True):
3536
f.get("branch"),
3637
)
3738

38-
self.url = url
39+
self.url: str = url
3940

4041
url_patterns = {
4142
"github.com": "https://raw.githubusercontent.com/{user}/{repo}/{branch}/{path}",
@@ -52,18 +53,18 @@ def __init__(self, repo_url, cdn=True):
5253
# strip '@'
5354
branch = branch[1:]
5455

55-
self.host = host
56-
self.user = user
57-
self.repo = repo
58-
self.branch = branch
56+
self.host: Optional[str] = host
57+
self.user: Optional[str] = user
58+
self.repo: Optional[str] = repo
59+
self.branch: str = branch
5960

6061
ptn = url_patterns[host]
6162
if self.cdn and host == "github.com":
6263
ptn = cdn_patterns[host]
6364

64-
self.path_pattern = ptn.format(user=user, repo=repo, branch=branch, path="{path}")
65+
self.path_pattern: str = ptn.format(user=user, repo=repo, branch=branch, path="{path}")
6566

66-
def parse_shortcut_repo_url(self, repo_url):
67+
def parse_shortcut_repo_url(self, repo_url: str) -> str:
6768
"""
6869
If repo_url is a shortcut specifying to use local git repo remote url,
6970
convert repo shortcut to url.
@@ -79,6 +80,7 @@ def parse_shortcut_repo_url(self, repo_url):
7980
g = k3git.Git(k3git.GitOpt(), cwd=".")
8081

8182
is_shortcut = False
83+
u: str = ""
8284

8385
# ".": use cwd git
8486
# ".@foo_branch": use cwd git and specified branch
@@ -101,7 +103,7 @@ def parse_shortcut_repo_url(self, repo_url):
101103

102104
return repo_url
103105

104-
def get_remote_url(self, remote=None):
106+
def get_remote_url(self, remote: Optional[str] = None) -> str:
105107
g = k3git.Git(k3git.GitOpt(), cwd=".")
106108

107109
if remote is None:
@@ -111,10 +113,10 @@ def get_remote_url(self, remote=None):
111113
# `branch` has no remote configured.
112114
remote = g.cmdf("remote", flag=CMD_RAISE_STDOUT)[0]
113115

114-
remote_url = g.remote_get(remote, flag=[CmdFlag.RAISE])
116+
remote_url: str = g.remote_get(remote, flag=[CmdFlag.RAISE])
115117
return remote_url
116118

117-
def make_default_branch(self):
119+
def make_default_branch(self) -> str:
118120
cwd = os.getcwd().split(os.path.sep)
119121
cwdmd5 = hashlib.md5(to_bytes(os.getcwd())).hexdigest()
120122
branch = "_md2zhihu_{tail}_{md5}".format(

0 commit comments

Comments
 (0)