Skip to content

Commit c7fbad8

Browse files
committed
mypy fixes
1 parent 6c58946 commit c7fbad8

File tree

14 files changed

+102
-59
lines changed

14 files changed

+102
-59
lines changed

src/dynamodb_encryption_sdk/delegated_keys/__init__.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""Delegated keys."""
1414
import abc
1515
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
16-
from typing import Dict, Text # noqa pylint: disable=unused-import
16+
from typing import Dict, Optional, Text # noqa pylint: disable=unused-import
1717
except ImportError: # pragma: no cover
1818
# We only actually need these imports when running the mypy checks
1919
pass
@@ -43,17 +43,24 @@ class DelegatedKey(object):
4343
a ``NotImplementedError`` detailing this.
4444
"""
4545

46-
#: Most delegated keys should not be used with RawCryptographicMaterials.
47-
allowed_for_raw_materials = False
48-
4946
@abc.abstractproperty
5047
def algorithm(self):
5148
# type: () -> Text
5249
"""Text description of algorithm used by this delegated key."""
5350

51+
@property
52+
def allowed_for_raw_materials(self):
53+
# type: () -> bool
54+
"""Most delegated keys should not be used with RawCryptographicMaterials.
55+
56+
:returns: False
57+
:rtype: bool
58+
"""
59+
return False
60+
5461
@classmethod
55-
def generate(cls, algorithm, key_length):
56-
# type: (Text, int) -> None
62+
def generate(cls, algorithm, key_length): # type: ignore
63+
# type: (Text, int) -> DelegatedKey
5764
# pylint: disable=unused-argument,no-self-use
5865
"""Generate an instance of this DelegatedKey using the specified algorithm and key length.
5966
@@ -64,8 +71,8 @@ def generate(cls, algorithm, key_length):
6471
"""
6572
_raise_not_implemented('generate')
6673

67-
def encrypt(self, algorithm, name, plaintext, additional_associated_data=None):
68-
# type: (Text, Text, bytes, Dict[Text, Text]) -> bytes
74+
def encrypt(self, algorithm, name, plaintext, additional_associated_data=None): # type: ignore
75+
# type: (Text, Text, bytes, Optional[Dict[Text, Text]]) -> bytes
6976
# pylint: disable=unused-argument,no-self-use
7077
"""Encrypt data.
7178
@@ -79,8 +86,8 @@ def encrypt(self, algorithm, name, plaintext, additional_associated_data=None):
7986
"""
8087
_raise_not_implemented('encrypt')
8188

82-
def decrypt(self, algorithm, name, ciphertext, additional_associated_data=None):
83-
# type: (Text, Text, bytes, Dict[Text, Text]) -> bytes
89+
def decrypt(self, algorithm, name, ciphertext, additional_associated_data=None): # type: ignore
90+
# type: (Text, Text, bytes, Optional[Dict[Text, Text]]) -> bytes
8491
# pylint: disable=unused-argument,no-self-use
8592
"""Encrypt data.
8693
@@ -94,8 +101,8 @@ def decrypt(self, algorithm, name, ciphertext, additional_associated_data=None):
94101
"""
95102
_raise_not_implemented('decrypt')
96103

97-
def wrap(self, algorithm, content_key, additional_associated_data=None):
98-
# type: (Text, bytes, Dict[Text, Text]) -> bytes
104+
def wrap(self, algorithm, content_key, additional_associated_data=None): # type: ignore
105+
# type: (Text, bytes, Optional[Dict[Text, Text]]) -> bytes
99106
# pylint: disable=unused-argument,no-self-use
100107
"""Wrap content key.
101108
@@ -108,8 +115,15 @@ def wrap(self, algorithm, content_key, additional_associated_data=None):
108115
"""
109116
_raise_not_implemented('wrap')
110117

111-
def unwrap(self, algorithm, wrapped_key, wrapped_key_algorithm, wrapped_key_type, additional_associated_data=None):
112-
# type: (Text, bytes, Text, EncryptionKeyType, Dict[Text, Text]) -> DelegatedKey
118+
def unwrap( # type: ignore
119+
self,
120+
algorithm,
121+
wrapped_key,
122+
wrapped_key_algorithm,
123+
wrapped_key_type,
124+
additional_associated_data=None
125+
):
126+
# type: (Text, bytes, Text, EncryptionKeyType, Optional[Dict[Text, Text]]) -> DelegatedKey
113127
# pylint: disable=unused-argument,no-self-use
114128
"""Wrap content key.
115129
@@ -125,7 +139,7 @@ def unwrap(self, algorithm, wrapped_key, wrapped_key_algorithm, wrapped_key_type
125139
"""
126140
_raise_not_implemented('unwrap')
127141

128-
def sign(self, algorithm, data):
142+
def sign(self, algorithm, data): # type: ignore
129143
# type: (Text, bytes) -> bytes
130144
# pylint: disable=unused-argument,no-self-use
131145
"""Sign data.
@@ -137,7 +151,7 @@ def sign(self, algorithm, data):
137151
"""
138152
_raise_not_implemented('sign')
139153

140-
def verify(self, algorithm, signature, data):
154+
def verify(self, algorithm, signature, data): # type: ignore
141155
# type: (Text, bytes, bytes) -> None
142156
# pylint: disable=unused-argument,no-self-use
143157
"""Sign data.
@@ -148,10 +162,10 @@ def verify(self, algorithm, signature, data):
148162
"""
149163
_raise_not_implemented('verify')
150164

151-
def signing_algorithm(self):
165+
def signing_algorithm(self): # type: ignore
152166
# type: () -> Text
153167
# pylint: disable=no-self-use
154-
"""Provides a description that can inform an appropriate cryptographic materials
168+
"""Provide a description that can inform an appropriate cryptographic materials
155169
provider about how to build a DelegatedKey for signature verification. If implemented,
156170
the return value of this method is included in the material description written to
157171
the encrypted item.

src/dynamodb_encryption_sdk/delegated_keys/jce.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def generate(cls, algorithm, key_length=None):
228228

229229
@property
230230
def allowed_for_raw_materials(self):
231+
# type: () -> bool
231232
"""Only ``JceNameLocalDelegatedKey`` backed by AES keys are allowed to be used with
232233
``RawCryptographicMaterials``.
233234
@@ -237,7 +238,7 @@ def allowed_for_raw_materials(self):
237238
return self.algorithm == 'AES'
238239

239240
def _encrypt(self, algorithm, name, plaintext, additional_associated_data=None):
240-
# type: (Text, Text, bytes, Dict[Text, Text]) -> bytes
241+
# type: (Text, Text, bytes, Optional[Dict[Text, Text]]) -> bytes
241242
# pylint: disable=unused-argument
242243
"""
243244
Encrypt data.
@@ -255,7 +256,7 @@ def _encrypt(self, algorithm, name, plaintext, additional_associated_data=None):
255256
return encryptor.encrypt(self.__key, plaintext)
256257

257258
def _decrypt(self, algorithm, name, ciphertext, additional_associated_data=None):
258-
# type: (Text, Text, bytes, Dict[Text, Text]) -> bytes
259+
# type: (Text, Text, bytes, Optional[Dict[Text, Text]]) -> bytes
259260
# pylint: disable=unused-argument
260261
"""Encrypt data.
261262
@@ -271,7 +272,7 @@ def _decrypt(self, algorithm, name, ciphertext, additional_associated_data=None)
271272
return decryptor.decrypt(self.__key, ciphertext)
272273

273274
def _wrap(self, algorithm, content_key, additional_associated_data=None):
274-
# type: (Text, bytes, Dict[Text, Text]) -> bytes
275+
# type: (Text, bytes, Optional[Dict[Text, Text]]) -> bytes
275276
# pylint: disable=unused-argument
276277
"""Wrap content key.
277278
@@ -288,7 +289,7 @@ def _wrap(self, algorithm, content_key, additional_associated_data=None):
288289
)
289290

290291
def _unwrap(self, algorithm, wrapped_key, wrapped_key_algorithm, wrapped_key_type, additional_associated_data=None):
291-
# type: (Text, bytes, Text, EncryptionKeyType, Dict[Text, Text]) -> DelegatedKey
292+
# type: (Text, bytes, Text, EncryptionKeyType, Optional[Dict[Text, Text]]) -> DelegatedKey
292293
# pylint: disable=unused-argument
293294
"""Wrap content key.
294295
@@ -341,7 +342,7 @@ def _verify(self, algorithm, signature, data):
341342

342343
def _signing_algorithm(self):
343344
# type: () -> Text
344-
"""Provides a description that can inform an appropriate cryptographic materials
345+
"""Provide a description that can inform an appropriate cryptographic materials
345346
provider about how to build a ``JceNameLocalDelegatedKey`` for signature verification.
346347
The return value of this method is included in the material description written to
347348
the encrypted item.

src/dynamodb_encryption_sdk/encrypted/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import botocore
1818

1919
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
20-
from typing import Any, Callable, Dict, Optional # noqa pylint: disable=unused-import
20+
from typing import Any, Callable, Dict, Iterator, Optional # noqa pylint: disable=unused-import
2121
except ImportError: # pragma: no cover
2222
# We only actually need these imports when running the mypy checks
2323
pass
@@ -88,7 +88,7 @@ def __getattr__(self, name):
8888
return getattr(self._paginator, name)
8989

9090
def paginate(self, **kwargs):
91-
# type: (**Any) -> Dict
91+
# type: (**Any) -> Iterator[Dict]
9292
# TODO: narrow this down
9393
"""Create an iterator that will paginate through responses from the underlying paginator,
9494
transparently decrypting any returned items.

src/dynamodb_encryption_sdk/encrypted/resource.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
from boto3.resources.base import ServiceResource
1818
from boto3.resources.collection import CollectionManager
1919

20+
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
21+
from typing import Optional # noqa pylint: disable=unused-import
22+
except ImportError: # pragma: no cover
23+
# We only actually need these imports when running the mypy checks
24+
pass
25+
2026
from dynamodb_encryption_sdk.internal.utils import (
2127
crypto_config_from_cache, decrypt_batch_get_item, encrypt_batch_write_item, TableInfoCache
2228
)

src/dynamodb_encryption_sdk/encrypted/table.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
from boto3.dynamodb.table import BatchWriter
1818
from boto3.resources.base import ServiceResource
1919

20+
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
21+
from typing import Optional # noqa pylint: disable=unused-import
22+
except ImportError: # pragma: no cover
23+
# We only actually need these imports when running the mypy checks
24+
pass
25+
2026
from dynamodb_encryption_sdk.internal.utils import (
2127
crypto_config_from_kwargs, crypto_config_from_table_info,
2228
decrypt_get_item, decrypt_multi_get, encrypt_put_item

src/dynamodb_encryption_sdk/internal/crypto/jce_bridge/primitives.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import six
2424

2525
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
26-
from typing import Callable, Text # noqa pylint: disable=unused-import
26+
from typing import Any, Callable, Text # noqa pylint: disable=unused-import
2727
except ImportError: # pragma: no cover
2828
# We only actually need these imports when running the mypy checks
2929
pass
@@ -268,8 +268,10 @@ def __init__(
268268
self.java_name = java_name
269269
self.cipher = cipher
270270
attr.validate(self)
271-
if hasattr(self, '__attrs_post_init__'):
272-
self.__attrs_post_init__()
271+
self.__attrs_post_init__()
272+
273+
def __attrs_post_init__(self):
274+
"""No-op stub to standardize API."""
273275

274276
def validate_algorithm(self, algorithm):
275277
# type: (Text) -> None

src/dynamodb_encryption_sdk/internal/dynamodb_types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Types used with mypy for DynamoDB items and attributes."""
22
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
3-
from typing import Any, ByteString, Dict, List, Text, Union
3+
from typing import Any, AnyStr, ByteString, Dict, List, Text
44

5-
ATTRIBUTE = Any # TODO: narrow this down
5+
ATTRIBUTE = Dict[Text, Any] # TODO: narrow this down
66
ITEM = Dict[Text, ATTRIBUTE]
77
RAW_ATTRIBUTE = ITEM
88
NULL = bool # DynamoDB TypeSerializer converts none to {'NULL': True}
99
BOOLEAN = bool
1010
NUMBER = int # TODO: This misses long on Python 2...figure out something for this
11-
STRING = Union[Text, Text] # TODO: can be unicode but should not be bytes
11+
STRING = AnyStr # TODO: can be unicode but should not be bytes
1212
BINARY = ByteString
1313
BINARY_ATTRIBUTE = Dict[Text, BINARY]
1414
SET = List # DynamoDB TypeSerializer converts sets into lists

src/dynamodb_encryption_sdk/internal/formatting/deserialize/attribute.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import struct
1919

2020
try: # Python 3.5.0 and 3.5.1 have incompatible typing modules
21-
from typing import Callable, Dict, List, Union # noqa pylint: disable=unused-import
21+
from typing import Callable, Dict, List, Text, Union # noqa pylint: disable=unused-import
2222
from dynamodb_encryption_sdk.internal import dynamodb_types # noqa pylint: disable=unused-import,ungrouped-imports
2323
except ImportError: # pragma: no cover
2424
# We only actually need these imports when running the mypy checks
@@ -52,7 +52,7 @@ def _transform_binary_value(value):
5252
return value
5353

5454
def _deserialize_binary(stream):
55-
# type: (io.BytesIO) -> Dict[str, bytes]
55+
# type: (io.BytesIO) -> Dict[Text, bytes]
5656
"""Deserializes a binary object.
5757
5858
:param stream: Stream containing serialized object
@@ -72,7 +72,7 @@ def _transform_string_value(value):
7272
return codecs.decode(value, TEXT_ENCODING)
7373

7474
def _deserialize_string(stream):
75-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.STRING]
75+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.STRING]
7676
"""Deserializes a string object.
7777
7878
:param stream: Stream containing serialized object
@@ -94,7 +94,7 @@ def _transform_number_value(value):
9494
return '{0:f}'.format(decimal_value)
9595

9696
def _deserialize_number(stream):
97-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.STRING]
97+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.STRING]
9898
"""Deserializes a number object.
9999
100100
:param stream: Stream containing serialized object
@@ -110,7 +110,7 @@ def _deserialize_number(stream):
110110
}
111111

112112
def _deserialize_boolean(stream):
113-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.BOOLEAN]
113+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.BOOLEAN]
114114
"""Deserializes a boolean object.
115115
116116
:param stream: Stream containing serialized object
@@ -121,7 +121,7 @@ def _deserialize_boolean(stream):
121121
return {Tag.BOOLEAN.dynamodb_tag: _boolean_map[value]}
122122

123123
def _deserialize_null(stream): # we want a consistent API but don't use stream, so pylint: disable=unused-argument
124-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.BOOLEAN]
124+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.BOOLEAN]
125125
"""Deserializes a null object.
126126
127127
:param stream: Stream containing serialized object
@@ -145,7 +145,7 @@ def _deserialize_set(stream, member_transform):
145145
])
146146

147147
def _deserialize_binary_set(stream):
148-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.SET[dynamodb_types.BINARY]]
148+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.SET[dynamodb_types.BINARY]]
149149
"""Deserializes a binary set object.
150150
151151
:param stream: Stream containing serialized object
@@ -155,7 +155,7 @@ def _deserialize_binary_set(stream):
155155
return {Tag.BINARY_SET.dynamodb_tag: _deserialize_set(stream, _transform_binary_value)}
156156

157157
def _deserialize_string_set(stream):
158-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.SET[dynamodb_types.STRING]]
158+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.SET[dynamodb_types.STRING]]
159159
"""Deserializes a string set object.
160160
161161
:param stream: Stream containing serialized object
@@ -165,7 +165,7 @@ def _deserialize_string_set(stream):
165165
return {Tag.STRING_SET.dynamodb_tag: _deserialize_set(stream, _transform_string_value)}
166166

167167
def _deserialize_number_set(stream):
168-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.SET[dynamodb_types.STRING]]
168+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.SET[dynamodb_types.STRING]]
169169
"""Deserializes a number set object.
170170
171171
:param stream: Stream containing serialized object
@@ -175,7 +175,7 @@ def _deserialize_number_set(stream):
175175
return {Tag.NUMBER_SET.dynamodb_tag: _deserialize_set(stream, _transform_number_value)}
176176

177177
def _deserialize_list(stream):
178-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.LIST]
178+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.LIST]
179179
"""Deserializes a list object.
180180
181181
:param stream: Stream containing serialized object
@@ -189,15 +189,15 @@ def _deserialize_list(stream):
189189
]}
190190

191191
def _deserialize_map(stream):
192-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.MAP]
192+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.MAP]
193193
"""Deserializes a map object.
194194
195195
:param stream: Stream containing serialized object
196196
:type stream: io.BytesIO
197197
:rtype: dict
198198
"""
199199
member_count = decode_length(stream)
200-
members = {}
200+
members = {} # type: dynamodb_types.MAP
201201
for _ in range(member_count):
202202
key = _deserialize(stream)
203203
if Tag.STRING.dynamodb_tag not in key:
@@ -236,7 +236,7 @@ def _deserialize_function(tag):
236236
raise DeserializationError('Unsupported tag: "{}"'.format(tag))
237237

238238
def _deserialize(stream):
239-
# type: (io.BytesIO) -> Dict[str, dynamodb_types.RAW_ATTRIBUTE]
239+
# type: (io.BytesIO) -> Dict[Text, dynamodb_types.RAW_ATTRIBUTE]
240240
"""Deserializes a serialized object.
241241
242242
:param stream: Stream containing serialized object

0 commit comments

Comments
 (0)