Skip to content

Commit a41a050

Browse files
aahungmndeveci
authored andcommitted
ci: Bring mypy typechecking back (aws#2503)
* * Revert "Revert "chore: Enable basic mypy typechecking"" This brings back commit 9d29984. * chore: Enable basic mypy typechecking, 2nd round after reverting * Remove import from builtins * Loose boto3-stubs version, matching boto3 aws#2493
1 parent 946e99d commit a41a050

File tree

24 files changed

+136
-83
lines changed

24 files changed

+136
-83
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ smoke-test:
3535
lint:
3636
# Linter performs static analysis to catch latent bugs
3737
pylint --rcfile .pylintrc samcli
38+
# mypy performs type check
39+
mypy setup.py samcli tests
3840

3941
# Command to run everytime you make changes to verify everything works
4042
dev: lint test

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ for:
7979
- "venv\\Scripts\\activate"
8080
- "pytest --cov samcli --cov-report term-missing --cov-fail-under 94 tests/unit"
8181
- "pylint --rcfile .pylintrc samcli"
82+
- "mypy setup.py samcli tests"
8283
- "pytest -n 4 tests/functional"
8384

8485
-
@@ -132,6 +133,7 @@ for:
132133
test_script:
133134
- "pytest --cov samcli --cov-report term-missing --cov-fail-under 94 tests/unit"
134135
- "pylint --rcfile .pylintrc samcli"
136+
- "mypy setup.py samcli tests"
135137
- "pytest -n 4 tests/functional"
136138

137139
# Runs only in Linux, logging docker hub when running canary and docker cred is available

mypy.ini

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# https://mypy.readthedocs.io/en/stable/config_file.html#config-file-format
2+
3+
[mypy]
4+
warn_return_any=True
5+
warn_unused_configs=True
6+
no_implicit_optional=True
7+
warn_redundant_casts=True
8+
warn_unused_ignores=True
9+
warn_unreachable=True
10+
11+
#
12+
# ignore errors in testdata
13+
#
14+
15+
[mypy-tests.integration.testdata.*]
16+
ignore_errors=True
17+
18+
#
19+
# below are packages/modules that do not have stubs available
20+
#
21+
22+
[mypy-pywintypes]
23+
ignore_missing_imports=True
24+
25+
[mypy-botocore,botocore.*]
26+
ignore_missing_imports=True
27+
28+
[mypy-docker,docker.*]
29+
ignore_missing_imports=True
30+
31+
[mypy-aws_lambda_builders,aws_lambda_builders.*]
32+
ignore_missing_imports=True
33+
34+
[mypy-cookiecutter,cookiecutter.*]
35+
ignore_missing_imports=True
36+
37+
[mypy-serverlessrepo,serverlessrepo.*]
38+
ignore_missing_imports=True
39+
40+
[mypy-tomlkit]
41+
ignore_missing_imports=True
42+
43+
[mypy-samtranslator,samtranslator.*]
44+
ignore_missing_imports=True
45+
46+
[mypy-jmespath]
47+
ignore_missing_imports=True
48+
49+
[mypy-chevron]
50+
ignore_missing_imports=True
51+
52+
[mypy-parameterized]
53+
ignore_missing_imports=True
54+
55+
[mypy-setuptools]
56+
ignore_missing_imports=True
57+
58+
[mypy-watchdog,watchdog.*]
59+
ignore_missing_imports=True

requirements/dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ coverage==5.3
22
pytest-cov==2.10.1
33
pylint~=2.6.0
44

5+
# type checking and related stubs
6+
mypy~=0.790
7+
boto3-stubs[essential]~=1.14
8+
59
# Test requirements
610
pytest==6.1.1
711
parameterized==0.7.4

samcli/lib/generated_sample_events/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
import base64
88
import warnings
9-
from requests.utils import quote as url_quote
9+
from urllib.parse import quote as url_quote
1010

1111
with warnings.catch_warnings():
1212
# https://github.com/aws/aws-sam-cli/issues/2381

samcli/lib/logs/formatter.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
import json
66
import functools
77

8-
try:
9-
# Python2
10-
from itertools import imap
11-
except ImportError:
12-
# Python3 already has `map` defined, alias to imap
13-
# We do this to prevent accidentally using the built-in ``map`` in Python2. In Python2, ``map`` does a full
14-
# evaluation of the iterator whereas imap does a lazy evaluation. For performance reasons, we need to use ``imap``.
15-
from builtins import map as imap
16-
178

189
class LogsFormatter:
1910
"""
@@ -106,7 +97,7 @@ def do_format(self, event_iterable):
10697

10798
# Make sure the operation has access to certain basic objects like colored
10899
partial_op = functools.partial(operation, colored=self.colored)
109-
event_iterable = imap(partial_op, event_iterable)
100+
event_iterable = map(partial_op, event_iterable)
110101

111102
return event_iterable
112103

samcli/lib/package/packageable_resources.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import os
66
import shutil
7+
from typing import Optional
78

89
import jmespath
910
from botocore.utils import set_value_from_jmespath
@@ -50,14 +51,14 @@
5051

5152

5253
class Resource:
53-
RESOURCE_TYPE = None
54-
PROPERTY_NAME = None
54+
RESOURCE_TYPE: Optional[str] = None
55+
PROPERTY_NAME: Optional[str] = None
5556
PACKAGE_NULL_PROPERTY = True
5657
# Set this property to True in base class if you want the exporter to zip
5758
# up the file before uploading This is useful for Lambda functions.
5859
FORCE_ZIP = False
59-
EXPORT_DESTINATION = None
60-
ARTIFACT_TYPE = None
60+
EXPORT_DESTINATION: Optional[str] = None
61+
ARTIFACT_TYPE: Optional[str] = None
6162

6263
def __init__(self, uploader, code_signer):
6364
self.uploader = uploader
@@ -75,8 +76,8 @@ class ResourceZip(Resource):
7576
Base class representing a CloudFormation resource that can be exported
7677
"""
7778

78-
RESOURCE_TYPE = None
79-
PROPERTY_NAME = None
79+
RESOURCE_TYPE: Optional[str] = None
80+
PROPERTY_NAME: Optional[str] = None
8081
PACKAGE_NULL_PROPERTY = True
8182
# Set this property to True in base class if you want the exporter to zip
8283
# up the file before uploading This is useful for Lambda functions.
@@ -149,8 +150,8 @@ class ResourceImageDict(Resource):
149150
Base class representing a CFN Image based resource that can be exported.
150151
"""
151152

152-
RESOURCE_TYPE = None
153-
PROPERTY_NAME = None
153+
RESOURCE_TYPE: Optional[str] = None
154+
PROPERTY_NAME: Optional[str] = None
154155
FORCE_ZIP = False
155156
ARTIFACT_TYPE = IMAGE
156157
EXPORT_DESTINATION = "ecr"
@@ -196,10 +197,10 @@ class ResourceImage(Resource):
196197
Base class representing a SAM Image based resource that can be exported.
197198
"""
198199

199-
RESOURCE_TYPE = None
200-
PROPERTY_NAME = None
200+
RESOURCE_TYPE: Optional[str] = None
201+
PROPERTY_NAME: Optional[str] = None
201202
FORCE_ZIP = False
202-
ARTIFACT_TYPE = IMAGE
203+
ARTIFACT_TYPE: Optional[str] = IMAGE
203204
EXPORT_DESTINATION = "ecr"
204205

205206
def __init__(self, uploader, code_signer):
@@ -242,9 +243,9 @@ class ResourceWithS3UrlDict(ResourceZip):
242243
an dict like {Bucket: "", Key: "", Version: ""}
243244
"""
244245

245-
BUCKET_NAME_PROPERTY = None
246-
OBJECT_KEY_PROPERTY = None
247-
VERSION_PROPERTY = None
246+
BUCKET_NAME_PROPERTY: Optional[str] = None
247+
OBJECT_KEY_PROPERTY: Optional[str] = None
248+
VERSION_PROPERTY: Optional[str] = None
248249
ARTIFACT_TYPE = ZIP
249250
EXPORT_DESTINATION = "s3"
250251

samcli/lib/providers/provider.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ def name(self):
202202
def codeuri(self):
203203
return self._codeuri
204204

205+
@codeuri.setter
206+
def codeuri(self, codeuri):
207+
self._codeuri = codeuri
208+
205209
@property
206210
def version(self):
207211
return self._version
@@ -211,10 +215,6 @@ def layer_arn(self):
211215
layer_arn, _ = self.arn.rsplit(":", 1)
212216
return layer_arn
213217

214-
@codeuri.setter
215-
def codeuri(self, codeuri):
216-
self._codeuri = codeuri
217-
218218
@property
219219
def build_method(self):
220220
return self._build_method
@@ -259,7 +259,7 @@ def binary_media_types(self):
259259

260260
_CorsTuple = namedtuple("Cors", ["allow_origin", "allow_methods", "allow_headers", "max_age"])
261261

262-
_CorsTuple.__new__.__defaults__ = (
262+
_CorsTuple.__new__.__defaults__ = ( # type: ignore
263263
None, # Allow Origin defaults to None
264264
None, # Allow Methods is optional and defaults to empty
265265
None, # Allow Headers is optional and defaults to empty

samcli/lib/schemas/schemas_aws_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import click
44

5-
from boto3 import Session
5+
from boto3.session import Session
66
from samcli.commands.local.cli_common.user_exceptions import ResourceNotFound
77

88

samcli/local/common/runtime_template.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import itertools
66
import os
77
import pathlib
8+
from typing import Set
89

910
_init_path = str(pathlib.Path(os.path.dirname(__file__)).parent.parent)
1011
_templates = os.path.join(_init_path, "lib", "init", "templates")
@@ -94,14 +95,16 @@ def get_local_lambda_images_location(mapping, runtime):
9495
"java8.al2": ["maven", "gradle"],
9596
}
9697

97-
SUPPORTED_DEP_MANAGERS = {
98-
c["dependency_manager"]
98+
SUPPORTED_DEP_MANAGERS: Set[str] = {
99+
c["dependency_manager"] # type: ignore
99100
for c in list(itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values())))
100101
if c["dependency_manager"]
101102
}
102103

103-
RUNTIMES = set(
104-
itertools.chain(*[c["runtimes"] for c in list(itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values())))])
104+
RUNTIMES: Set[str] = set(
105+
itertools.chain(
106+
*[c["runtimes"] for c in list(itertools.chain(*(RUNTIME_DEP_TEMPLATE_MAPPING.values())))] # type: ignore
107+
)
105108
)
106109

107110
# When adding new Lambda runtimes, please update SAM_RUNTIME_TO_SCHEMAS_CODE_LANG_MAPPING

0 commit comments

Comments
 (0)