Skip to content

Commit b1c02b7

Browse files
authored
Type hinting changes for mypy compatibility (#140)
This commit includes the following changes : - Add py.typed in manifest files, for including type stubs on dist files. - Added mypy.ini file with configurations - Made some generic class changes for mypy to realize the relationships bwn abstract and implemented classes - Added no-strict-optionals for some packages in mypy.ini, will enhance to remove them slowly
1 parent a6065d0 commit b1c02b7

File tree

31 files changed

+173
-141
lines changed

31 files changed

+173
-141
lines changed

ask-sdk-core/MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ include README.rst
22
include CHANGELOG.rst
33
include LICENSE
44
include requirements.txt
5-
recursive-exclude tests *
5+
recursive-exclude tests *
6+
recursive-include ask_sdk_core py.typed

ask-sdk-core/ask_sdk_core/api_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .exceptions import ApiClientException
2828

2929
if typing.TYPE_CHECKING:
30-
from typing import Callable, Dict, List, Tuple
30+
from typing import Callable, Dict, List, Tuple, Optional
3131
from ask_sdk_model.services import ApiClientRequest
3232

3333

@@ -66,15 +66,14 @@ def invoke(self, request):
6666
raise ApiClientException(
6767
"Requests against non-HTTPS endpoints are not allowed.")
6868

69+
raw_data = None # type: Optional[str]
6970
if request.body:
7071
body_content_type = http_headers.get("Content-type", None)
7172
if (body_content_type is not None and
7273
"json" in body_content_type):
7374
raw_data = json.dumps(request.body)
7475
else:
7576
raw_data = request.body
76-
else:
77-
raw_data = None
7877

7978
http_response = http_method(
8079
url=request.url, headers=http_headers, data=raw_data)
@@ -101,7 +100,11 @@ def _resolve_method(self, request):
101100
if invalid http request method is being called
102101
"""
103102
try:
104-
return getattr(requests, request.method.lower())
103+
if request.method is not None:
104+
return getattr(requests, request.method.lower())
105+
else:
106+
raise ApiClientException(
107+
"Invalid request method: {}".format(request.method))
105108
except AttributeError:
106109
raise ApiClientException(
107110
"Invalid request method: {}".format(request.method))

ask-sdk-core/ask_sdk_core/attributes_manager.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .exceptions import AttributesManagerException
2323

2424
if typing.TYPE_CHECKING:
25-
from typing import Dict, Optional
25+
from typing import Dict, Optional, Any
2626
from ask_sdk_model import RequestEnvelope
2727

2828

@@ -102,14 +102,13 @@ def __init__(self, request_envelope, persistence_adapter=None):
102102
self._persistence_adapter = persistence_adapter
103103
self._persistence_attributes = {} # type: Dict
104104
self._request_attributes = {} # type: Dict
105-
if not self._request_envelope.session:
106-
self._session_attributes = None # type: Optional[Dict]
105+
if request_envelope.session is None:
106+
self._session_attributes = None # type: Optional[Dict[str, Any]]
107+
elif request_envelope.session.attributes is None:
108+
self._session_attributes = {}
107109
else:
108-
if not self._request_envelope.session.attributes:
109-
self._session_attributes = {}
110-
else:
111-
self._session_attributes = deepcopy(
112-
request_envelope.session.attributes)
110+
self._session_attributes = deepcopy(
111+
request_envelope.session.attributes)
113112
self._persistent_attributes_set = False
114113

115114
@property
@@ -134,7 +133,7 @@ def request_attributes(self, request_attributes):
134133

135134
@property
136135
def session_attributes(self):
137-
# type: () -> Dict[str, object]
136+
# type: () -> Optional[Dict[str, Any]]
138137
"""Attributes stored at the Session level of the skill lifecycle.
139138
140139
:return: session attributes extracted from request envelope

ask-sdk-core/ask_sdk_core/serialize.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
long = int
4242

4343
if typing.TYPE_CHECKING:
44-
from typing import TypeVar, Dict, List, Tuple, Union, Any
44+
from typing import (
45+
TypeVar, Dict, List, Tuple, Union, Any, Optional)
4546
T = TypeVar('T')
4647

4748

@@ -125,7 +126,7 @@ def serialize(self, obj): # type: ignore
125126
return {key: self.serialize(val) for key, val in iteritems(obj_dict)}
126127

127128
def deserialize(self, payload, obj_type):
128-
# type: (str, Union[T, str]) -> Any
129+
# type: (Optional[str], Union[T, str]) -> Any
129130
"""Deserializes payload into an instance of provided ``obj_type``.
130131
131132
The ``obj_type`` parameter can be a primitive type, a generic
@@ -169,7 +170,7 @@ class should also have the ``__init__`` method with default
169170
return self.__deserialize(payload, obj_type)
170171

171172
def __deserialize(self, payload, obj_type):
172-
# type: (str, Union[T, str]) -> Any
173+
# type: (Optional[str], Union[T, str]) -> Any
173174
"""Deserializes payload into a model object.
174175
175176
:param payload: data to be deserialized.

ask-sdk-core/ask_sdk_core/skill.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
if typing.TYPE_CHECKING:
3535
from typing import List, Dict, Any
3636
from ask_sdk_model.services import ApiClient
37-
from ask_sdk_model import RequestEnvelope
37+
from ask_sdk_model import RequestEnvelope, Response
3838
from ask_sdk_runtime.dispatch_components import (
3939
GenericRequestMapper, GenericHandlerAdapter, GenericExceptionMapper,
4040
AbstractRequestInterceptor, AbstractResponseInterceptor)
@@ -204,7 +204,7 @@ def invoke(self, request_envelope, context):
204204
template_factory=template_factory)
205205

206206
response = self.request_dispatcher.dispatch(
207-
handler_input=handler_input)
207+
handler_input=handler_input) # type: Response
208208
session_attributes = None
209209

210210
if request_envelope.session is not None:

ask-sdk-core/ask_sdk_core/skill_builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def skill_configuration(self):
5050
"""Create the skill configuration object using the
5151
registered components.
5252
"""
53-
self.runtime_configuration = self.runtime_configuration_builder.get_runtime_configuration()
53+
self.runtime_configuration = self.runtime_configuration_builder.get_runtime_configuration()
54+
self.runtime_configuration = typing.cast(SkillConfiguration, self.runtime_configuration)
5455
self.runtime_configuration.custom_user_agent = self.custom_user_agent
5556
self.runtime_configuration.skill_id = self.skill_id
5657
self.runtime_configuration = self.__populate_missing_attributes(

ask-sdk-core/ask_sdk_core/utils/view_resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import re
2323

2424
if typing.TYPE_CHECKING:
25-
from typing import Any, Sequence
25+
from typing import Any, Sequence, Optional
2626

2727

2828
def split_locale(locale):
29-
# type: (str) -> Sequence[str]
29+
# type: (str) -> Sequence[Optional[str]]
3030
"""Function to extract language and country codes from the locale.
3131
3232
:param locale: A string indicating the user’s locale. For example: en-US.

ask-sdk-core/ask_sdk_core/view_resolvers/locale_template_enumerator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class LocaleTemplateEnumerator(AbstractTemplateEnumerator):
4242
__instance = None
4343

4444
def __new__(cls):
45-
# type: (Type[object]) -> AbstractTemplateEnumerator
45+
# type: (Type[object]) -> LocaleTemplateEnumerator
4646
"""Creating a singleton class to re-use same enumerator instance for
4747
different locale and template values.
4848
"""

ask-sdk-dynamodb-persistence-adapter/MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ include README.rst
22
include CHANGELOG.rst
33
include LICENSE
44
include requirements.txt
5-
recursive-exclude tests *
5+
recursive-exclude tests *
6+
recursive-include ask_sdk_dynamodb py.typed

ask-sdk-jinja-renderer/MANIFEST.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ include README.rst
22
include CHANGELOG.rst
33
include LICENSE
44
include requirements.txt
5-
recursive-exclude tests *
5+
recursive-exclude tests *
6+
recursive-include ask_sdk_jinja_renderer py.typed

0 commit comments

Comments
 (0)