Skip to content

Commit 67e8382

Browse files
committed
Enable mypy types check and fix reported issues
Signed-off-by: Gang Li <ligangty@users.noreply.github.com>
1 parent 2bf58ee commit 67e8382

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+328
-298
lines changed

.github/workflows/linters.yaml

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,6 @@ jobs:
2929
- name: Run flake8 on python${{ matrix.python-version }}
3030
run: python -m tox -e flake8
3131

32-
# markdownlint:
33-
# name: Markdownlint
34-
# runs-on: ubuntu-latest
35-
36-
# steps:
37-
# - name: Check out repo
38-
# uses: actions/checkout@v2
39-
40-
# - name: Run markdownlint
41-
# uses: containerbuildsystem/actions/markdownlint@master
42-
4332
pylint:
4433
name: Pylint analyzer for Python ${{ matrix.python-version }}
4534
runs-on: ubuntu-latest
@@ -71,25 +60,25 @@ jobs:
7160
- name: Run ShellCheck
7261
uses: containerbuildsystem/actions/shellcheck@master
7362

74-
# mypy:
75-
# name: mypy type checker for Python ${{ matrix.python-version }}
76-
# runs-on: ubuntu-latest
77-
#
78-
# strategy:
79-
# matrix:
80-
# python-version: [ "3.8" ]
81-
#
82-
# steps:
83-
# - uses: actions/checkout@v3
84-
# - uses: actions/setup-python@v4
85-
# with:
86-
# python-version: ${{ matrix.python-version }}
87-
# - name: Install dependencies
88-
# run: |
89-
# python -m pip install --upgrade pip setuptools tox
90-
#
91-
# - name: Run mypy on python${{ matrix.python-version }}
92-
# run: python -m tox -e mypy
63+
mypy:
64+
name: mypy type checker for Python ${{ matrix.python-version }}
65+
runs-on: ubuntu-latest
66+
67+
strategy:
68+
matrix:
69+
python-version: [ "3.9" ]
70+
71+
steps:
72+
- uses: actions/checkout@v3
73+
- uses: actions/setup-python@v4
74+
with:
75+
python-version: ${{ matrix.python-version }}
76+
- name: Install dependencies
77+
run: |
78+
python -m pip install --upgrade pip setuptools tox
79+
80+
- name: Run mypy on python${{ matrix.python-version }}
81+
run: python -m tox -e mypy
9382

9483
# bandit:
9584
# name: Bandit analyzer for Python ${{ matrix.python-version }}

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ init-venv:
99
lint:
1010
@python -m tox -e flake8
1111
@python -m tox -e pylint
12+
@python -m tox -e mypy
1213
.PHONY: lint
1314

1415
test-only:

charon/cache.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from boto3 import session
22
from botocore.exceptions import ClientError
3-
from typing import Dict, List
3+
from typing import Dict, List, Optional
44
import os
55
import logging
66
import uuid
@@ -57,7 +57,7 @@ def __init_aws_client(
5757
endpoint_url=endpoint_url
5858
)
5959

60-
def __get_endpoint(self, extra_conf) -> str:
60+
def __get_endpoint(self, extra_conf) -> Optional[str]:
6161
endpoint_url = os.getenv(ENDPOINT_ENV)
6262
if not endpoint_url or not endpoint_url.strip():
6363
if isinstance(extra_conf, Dict):
@@ -97,14 +97,14 @@ def invalidate_paths(
9797
" will take more than %d seconds",
9898
len(real_paths), total_time_approx)
9999
results = []
100-
current_invalidation = {}
100+
current_invalidation: Dict[str, str] = {}
101101
processed_count = 0
102102
for batch_paths in real_paths:
103103
while (current_invalidation and
104104
INVALIDATION_STATUS_INPROGRESS == current_invalidation.get('Status', '')):
105105
time.sleep(INPRO_W_SECS)
106106
try:
107-
result = self.check_invalidation(distr_id, current_invalidation.get('Id'))
107+
result = self.check_invalidation(distr_id, current_invalidation.get('Id', ''))
108108
if result:
109109
current_invalidation = {
110110
'Id': result.get('Id', None),
@@ -159,7 +159,7 @@ def invalidate_paths(
159159
results.append(current_invalidation)
160160
return results
161161

162-
def check_invalidation(self, distr_id: str, invalidation_id: str) -> dict:
162+
def check_invalidation(self, distr_id: str, invalidation_id: str) -> Optional[dict]:
163163
try:
164164
response = self.__client.get_invalidation(
165165
DistributionId=distr_id,
@@ -177,8 +177,9 @@ def check_invalidation(self, distr_id: str, invalidation_id: str) -> dict:
177177
"[CloudFront] Error occurred while check invalidation of id %s, "
178178
"error: %s", invalidation_id, err
179179
)
180+
return None
180181

181-
def get_dist_id_by_domain(self, domain: str) -> str:
182+
def get_dist_id_by_domain(self, domain: str) -> Optional[str]:
182183
"""Get distribution id by a domain name. The id can be used to send invalidating
183184
request through #invalidate_paths function
184185
* Domain are Ronda domains, like "maven.repository.redhat.com"
@@ -200,5 +201,5 @@ def get_dist_id_by_domain(self, domain: str) -> str:
200201
)
201202
return None
202203

203-
def get_domain_by_bucket(self, bucket: str) -> str:
204+
def get_domain_by_bucket(self, bucket: str) -> Optional[str]:
204205
return DEFAULT_BUCKET_TO_DOMAIN.get(bucket, None)

charon/cmd/cmd_cache.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
"""
1616

1717
from charon.config import get_config
18-
from charon.cmd.internal import _decide_mode, _get_buckets
18+
from charon.cmd.internal import _decide_mode, _get_targets
1919
from charon.cache import CFClient
2020
from charon.pkgs.pkg_utils import invalidate_cf_paths
21+
from charon.types import TARGET_TYPE
2122
from click import command, option, argument, group
22-
from typing import List, Tuple
23+
from typing import List, Tuple, Optional
2324

2425
import traceback
2526
import logging
@@ -87,7 +88,7 @@ def invalidate(
8788
target: str,
8889
paths: List[str],
8990
path_file: str,
90-
config: str = None,
91+
config: str = "",
9192
quiet: bool = False,
9293
debug: bool = False
9394
):
@@ -119,20 +120,20 @@ def invalidate(
119120
break
120121

121122
try:
122-
(buckets, aws_profile) = _init_cmd(target, config)
123+
(targets, aws_profile) = _init_cmd(target, config)
123124

124-
for b in buckets:
125+
for t in targets:
125126
cf_client = CFClient(aws_profile=aws_profile)
126127
# Per aws official doc, if the paths contains wildcard, it is
127128
# limited to 15 as max items in one request. Otherwise it could
128129
# be 3000
129130
if use_wildcard:
130131
invalidate_cf_paths(
131-
cf_client, b, work_paths
132+
cf_client, t, work_paths
132133
)
133134
else:
134135
invalidate_cf_paths(
135-
cf_client, b, work_paths, batch_size=3000
136+
cf_client, t, work_paths, batch_size=3000
136137
)
137138
except Exception:
138139
print(traceback.format_exc())
@@ -181,7 +182,7 @@ def invalidate(
181182
def check(
182183
invalidation_id: str,
183184
target: str,
184-
config: str = None,
185+
config: str = "",
185186
quiet: bool = False,
186187
debug: bool = False
187188
):
@@ -193,14 +194,14 @@ def check(
193194
is_quiet=quiet, is_debug=debug, use_log_file=False
194195
)
195196
try:
196-
(buckets, aws_profile) = _init_cmd(target, config)
197-
if not buckets:
197+
(targets, aws_profile) = _init_cmd(target, config)
198+
if not targets:
198199
sys.exit(1)
199200

200-
for b in buckets:
201+
for t in targets:
201202
cf_client = CFClient(aws_profile=aws_profile)
202-
bucket_name = b[1]
203-
domain = b[4]
203+
bucket_name = t[1]
204+
domain: Optional[str] = t[4]
204205
if not domain:
205206
domain = cf_client.get_domain_by_bucket(bucket_name)
206207
if domain:
@@ -221,7 +222,7 @@ def check(
221222
sys.exit(2)
222223

223224

224-
def _init_cmd(target: str, config: str) -> Tuple[List[Tuple[str, str, str, str, str]], str]:
225+
def _init_cmd(target: str, config: str) -> Tuple[List[TARGET_TYPE], str]:
225226
conf = get_config(config)
226227
if not conf:
227228
sys.exit(1)
@@ -231,7 +232,7 @@ def _init_cmd(target: str, config: str) -> Tuple[List[Tuple[str, str, str, str,
231232
logger.error("No AWS profile specified!")
232233
sys.exit(1)
233234

234-
return (_get_buckets([target], conf), aws_profile)
235+
return (_get_targets([target], conf), aws_profile)
235236

236237

237238
@group()

charon/cmd/cmd_checksum.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
def validate(
105105
path: str,
106106
target: str,
107-
includes: List[str],
107+
includes: str,
108108
report_file_path: str,
109109
skips: List[str],
110110
recursive: bool = False,
@@ -259,12 +259,12 @@ def _init_cmd(target: str) -> Tuple[str, str]:
259259
conf = get_config()
260260
if not conf:
261261
sys.exit(1)
262-
aws_bucket = ""
263262
t = conf.get_target(target)
264263
if not t:
265264
sys.exit(1)
265+
aws_bucket = ''
266266
for b in t:
267-
aws_bucket = b.get('bucket')
267+
aws_bucket = b.get('bucket', '')
268268
prefix = b.get('prefix', '')
269269
return (aws_bucket, prefix)
270270

charon/cmd/cmd_delete.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from charon.pkgs.npm import handle_npm_del
2222
from charon.cmd.internal import (
2323
_decide_mode, _validate_prod_key,
24-
_get_local_repo, _get_buckets,
24+
_get_local_repo, _get_targets,
2525
_get_ignore_patterns, _safe_delete
2626
)
2727
from click import command, option, argument
@@ -158,19 +158,19 @@ def delete(
158158
npm_archive_type = detect_npm_archive(archive_path)
159159
product_key = f"{product}-{version}"
160160
manifest_bucket_name = conf.get_manifest_bucket()
161-
buckets = _get_buckets(targets, conf)
162-
if not buckets:
161+
targets_ = _get_targets(targets, conf)
162+
if not targets_:
163163
logger.error(
164164
"The targets %s can not be found! Please check"
165165
" your charon configuration to confirm the targets"
166-
" are set correctly.", targets
166+
" are set correctly.", targets_
167167
)
168168
if npm_archive_type != NpmArchiveType.NOT_NPM:
169169
logger.info("This is a npm archive")
170170
tmp_dir, succeeded = handle_npm_del(
171171
archive_path,
172172
product_key,
173-
buckets=buckets,
173+
targets=targets_,
174174
aws_profile=aws_profile,
175175
dir_=work_dir,
176176
cf_enable=conf.is_aws_cf_enable(),
@@ -191,7 +191,7 @@ def delete(
191191
product_key,
192192
ignore_patterns_list,
193193
root=root_path,
194-
buckets=buckets,
194+
targets=targets_,
195195
aws_profile=aws_profile,
196196
dir_=work_dir,
197197
cf_enable=conf.is_aws_cf_enable(),
@@ -204,5 +204,5 @@ def delete(
204204
print(traceback.format_exc())
205205
sys.exit(2) # distinguish between exception and bad config or bad state
206206
finally:
207-
if not debug:
207+
if not debug and tmp_dir:
208208
_safe_delete(tmp_dir)

charon/cmd/cmd_index.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def index(
9797
sys.exit(1)
9898

9999
for b in tgt:
100-
aws_bucket = b.get('bucket')
100+
aws_bucket = b.get('bucket', '')
101101

102102
package_type = None
103103
if "maven" in aws_bucket:
@@ -115,6 +115,7 @@ def index(
115115
"The target %s is not supported. Only maven or npm target is supported.",
116116
target
117117
)
118+
continue
118119

119120
if not aws_bucket:
120121
logger.error("No bucket specified for target %s!", target)

charon/cmd/cmd_upload.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from charon.pkgs.npm import handle_npm_uploading
2222
from charon.cmd.internal import (
2323
_decide_mode, _validate_prod_key,
24-
_get_local_repo, _get_buckets,
24+
_get_local_repo, _get_targets,
2525
_get_ignore_patterns, _safe_delete
2626
)
2727
from click import command, option, argument
@@ -177,20 +177,20 @@ def upload(
177177
npm_archive_type = detect_npm_archive(archive_path)
178178
product_key = f"{product}-{version}"
179179
manifest_bucket_name = conf.get_manifest_bucket()
180-
buckets = _get_buckets(targets, conf)
181-
if not buckets:
180+
targets_ = _get_targets(targets, conf)
181+
if not targets_:
182182
logger.error(
183183
"The targets %s can not be found! Please check"
184184
" your charon configuration to confirm the targets"
185-
" are set correctly.", targets
185+
" are set correctly.", targets_
186186
)
187187
sys.exit(1)
188188
if npm_archive_type != NpmArchiveType.NOT_NPM:
189189
logger.info("This is a npm archive")
190190
tmp_dir, succeeded = handle_npm_uploading(
191191
archive_path,
192192
product_key,
193-
buckets=buckets,
193+
targets=targets_,
194194
aws_profile=aws_profile,
195195
dir_=work_dir,
196196
gen_sign=contain_signature,
@@ -213,7 +213,7 @@ def upload(
213213
product_key,
214214
ignore_patterns_list,
215215
root=root_path,
216-
buckets=buckets,
216+
targets=targets_,
217217
aws_profile=aws_profile,
218218
dir_=work_dir,
219219
gen_sign=contain_signature,
@@ -229,5 +229,5 @@ def upload(
229229
print(traceback.format_exc())
230230
sys.exit(2) # distinguish between exception and bad config or bad state
231231
finally:
232-
if not debug:
232+
if not debug and tmp_dir:
233233
_safe_delete(tmp_dir)

0 commit comments

Comments
 (0)