Skip to content

Commit 84fba34

Browse files
authored
[TOOLSLIBS-387] Adds type hinting (#193)
* adds type annotations to automation module * adds type annotations for custom_events module * fixes for circular import * adds type hinting * adds hinting * bug fixes and typing * adds type hints * adds type hints to devices module * adds typing to experiments module * adds typing to push module * adds requests typing * removes test for removed exception case * adds types-six * adds typing to common and core * adds typing to reports module * adds typing to push init * mypy cleanup * circular import fixes * rm re type for 36 compat * adds mypy to test run build script * adds mypy * adds mypy pre-commit hook * merge conflict issues * review cleanup
1 parent fb84698 commit 84fba34

33 files changed

+1196
-987
lines changed

.github/workflows/test_runner.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ jobs:
2121
python -m pip install --upgrade pip
2222
pip install flake8 pytest
2323
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
24+
- name: Run mypy checks
25+
run: |
26+
mypy urbanairship/
2427
- name: Test with pytest
2528
run: |
2629
nosetests -v --with-doctest

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ repos:
1515
rev: v2.1.0
1616
hooks:
1717
- id: codespell
18+
- repo: https://github.com/pre-commit/mirrors-mypy
19+
rev: v0.931
20+
hooks:
21+
- id: mypy

requirements.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
requests>=1.2
2+
types-requests
23
six
4+
types-six
35
nose
46
mock
57
sphinx-rtd-theme
6-
backoff>=1.11
8+
mypy
9+
mypy-extensions
10+
backoff>=1.11

tests/devices/test_sms.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ def setUp(self):
165165
def test_payload(self):
166166
self.assertEqual(
167167
self.interaction.payload,
168-
{"keyword": self.keyword, "sender_ids": self.sender_ids},
168+
{
169+
"keyword": self.keyword,
170+
"sender_ids": self.sender_ids,
171+
"timestamp": "2014-10-08T12:00:00",
172+
},
169173
)
170174

171175
def test_url(self):

tests/devices/test_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def setUp(self):
2525
ua.Airship._request.side_effect = [self.mock_response]
2626

2727
def test_ios_audience(self):
28-
self.channel_tags.set_audience("ios_audience")
28+
self.channel_tags.set_audience(ios="ios_audience")
2929
self.channel_tags.add("group_name", "tag1")
3030
result = self.channel_tags.send()
3131

tests/push/test_template.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -416,31 +416,6 @@ def test_create_template_requires_push(self):
416416

417417
self.assertRaises(ValueError, template.create)
418418

419-
def test_create_template_no_message(self):
420-
airship = ua.Airship(TEST_KEY, TEST_SECRET)
421-
template = ua.Template(airship)
422-
template.name = "Cat sound"
423-
template.description = "The cat says..."
424-
template.variables = [
425-
{
426-
"key": "SOUND",
427-
"name": "Sound",
428-
"description": "A sound",
429-
"default_value": "Meow",
430-
}
431-
]
432-
# Set message center (not allowed)
433-
template.push = {
434-
"notification": {"alert": "The cat says {{SOUND}}"},
435-
"message": {
436-
"title": "Message title",
437-
"body": "Message body",
438-
"content_type": "text/html",
439-
},
440-
}
441-
442-
self.assertRaises(ValueError, template.create)
443-
444419
def test_update_template(self):
445420
template_id = "ef34a8d9-0ad7-491c-86b0-aea74da15161"
446421
name = "Goodbye Message"
@@ -467,22 +442,6 @@ def test_update_template(self):
467442

468443
self.assertEqual(template.template_id, template_id)
469444

470-
def test_update_template_no_message(self):
471-
airship = ua.Airship(TEST_KEY, TEST_SECRET)
472-
template_id = "ef34a8d9-0ad7-491c-86b0-aea74da15161"
473-
template = ua.Template(airship)
474-
# Set message center (not allowed)
475-
template.push = {
476-
"notification": {"alert": "The cat says {{SOUND}}"},
477-
"message": {
478-
"title": "Message title",
479-
"body": "Message body",
480-
"content_type": "text/html",
481-
},
482-
}
483-
484-
self.assertRaises(ValueError, template.update, template_id)
485-
486445
def test_update_template_needs_something(self):
487446
airship = ua.Airship(TEST_KEY, TEST_SECRET)
488447
template_id = "ef34a8d9-0ad7-491c-86b0-aea74da15161"

urbanairship/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Python package for using the Airship API"""
22
import logging
3+
from typing import Any, List
34

5+
from .core import Airship
46
from .automation import Automation, Pipeline
57
from .common import AirshipFailure, Unauthorized
6-
from .core import Airship
78
from .custom_events import CustomEvent
89
from .devices import (
910
APIDList,
@@ -110,7 +111,7 @@
110111
WebResponseReport,
111112
)
112113

113-
__all__ = [
114+
__all__: List[Any] = [
114115
Airship,
115116
AirshipFailure,
116117
Unauthorized,

urbanairship/automation/core.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import json
2+
from typing import List, Union, Optional
3+
4+
from urbanairship import Airship
5+
from urbanairship.automation.pipeline import Pipeline
6+
from requests import Response
27

38

49
class Automation(object):
@@ -9,10 +14,10 @@ class Automation(object):
914
to use.
1015
"""
1116

12-
def __init__(self, airship):
17+
def __init__(self, airship: Airship) -> None:
1318
self.airship = airship
1419

15-
def create(self, pipelines):
20+
def create(self, pipelines: Union[Pipeline, List[Pipeline]]) -> Response:
1621
"""Create an automation with one or more Pipeline payloads
1722
1823
:keyword pipelines: A single Pipeline payload or list of Pipeline payloads
@@ -29,7 +34,7 @@ def create(self, pipelines):
2934

3035
return response
3136

32-
def validate(self, pipelines):
37+
def validate(self, pipelines: Union[Pipeline, List[Pipeline]]) -> Response:
3338
"""Validate a Pipeline payloads
3439
3540
:keyword pipelines: A single Pipeline payload or list of Pipeline payloads
@@ -46,7 +51,7 @@ def validate(self, pipelines):
4651

4752
return response
4853

49-
def update(self, pipeline_id, pipeline):
54+
def update(self, pipeline_id: str, pipeline: Pipeline) -> Response:
5055
"""Update an existing Automation Pipeline
5156
5257
:keyword pipeline_id: A Pipeline ID
@@ -60,7 +65,7 @@ def update(self, pipeline_id, pipeline):
6065

6166
return response
6267

63-
def delete(self, pipeline_id):
68+
def delete(self, pipeline_id: str) -> Response:
6469
"""Delete an existing Automation Pipeline
6570
6671
:keyword pipeline_id: A Pipeline ID
@@ -70,7 +75,7 @@ def delete(self, pipeline_id):
7075

7176
return response
7277

73-
def lookup(self, pipeline_id):
78+
def lookup(self, pipeline_id: str) -> Response:
7479
"""Lookup an Automation Pipeline
7580
7681
:keyword pipeline_id: A Pipeline ID
@@ -80,7 +85,9 @@ def lookup(self, pipeline_id):
8085

8186
return response
8287

83-
def list_automations(self, limit=None, enabled=False):
88+
def list_automations(
89+
self, limit: Optional[int] = None, enabled: bool = False
90+
) -> Response:
8491
"""List active Automations
8592
8693
:keyword limit: Optional, maximum pipelines to return
@@ -100,7 +107,7 @@ def list_automations(self, limit=None, enabled=False):
100107

101108
return response
102109

103-
def list_deleted_automations(self, start=None):
110+
def list_deleted_automations(self, start: Optional[str] = None) -> Response:
104111
"""List deleted Automation Pipelines
105112
106113
:keyword start: Optional starting timestamp for limiting results in ISO-8601

0 commit comments

Comments
 (0)