8000 Merge pull request #1306 from Kami/mypy_type_annotations · apache/libcloud@011e53f · GitHub
[go: up one dir, main page]

Skip to content

Commit 011e53f

Browse files
authored
Merge pull request #1306 from Kami/mypy_type_annotations
Add Python type annotation for base Compute API
2 parents 9f2d8d6 + 8e25d98 commit 011e53f

40 files changed

+468
-115
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,4 @@ lib/
3131
pip-selfcheck.json
3232
report.html
3333
.pytest_cache
34-
35-
34+
.mypy_cache/

CHANGES.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ Common
2323
Compute
2424
-------
2525

26+
- Introduce type annotations for the base compute API methods. This means you
27+
can now leverage mypy to type check (with some limitations) your code which
28+
utilizes Libcloud compute API standard API methods.
29+
30+
Keep in mind that at this point, type annotations are only available for
31+
standard compute API methods.
32+
(GITHUB-1306)
33+
[Tomaz Muraus]
2634
- [Azure ARM] Fix ``attach_volume`` method and allow maximum of 64 disks to be
2735
added when LUN is not specified. Previously there was a bug and only a
2836
maximum of 63 disks could be added.
29-
(GITHUB-1372
37+
(GITHUB-1372)
3038
[Palash Gandhi - @palashgandhi]
3139

3240
Storage

example_compute.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@
1616
from libcloud.compute.types import Provider
1717
from libcloud.compute.providers import get_driver
1818

19-
EC2 = get_driver(Provider.EC2)
20-
Rackspace = get_driver(Provider.RACKSPACE)
19+
from libcloud.compute.drivers.ec2 import EC2NodeDriver
20+
from libcloud.compute.drivers.rackspace import RackspaceNodeDriver
21+
22+
from typing import Type, cast
23+
24+
ec2_cls = get_driver(Provider.EC2)
25+
rackspace_cls = get_driver(Provider.RACKSPACE)
26+
27+
# NOTE: If you are using driver methods which are not part of the standard API,
28+
# you need to explicitly cast the driver class reference to the correct class
29+
# for type checking to work correctly
30+
EC2 = cast(Type[EC2NodeDriver], ec2_cls)
31+
Rackspace = cast(Type[RackspaceNodeDriver], rackspace_cls)
2132

2233
drivers = [EC2('access key id', 'secret key', region='us-east-1'),
2334
Rackspace('username', 'api key', region='iad')]
2435

25-
nodes = [driver.list_nodes() for driver in drivers]
36+
nodes = []
37+
for driver in drivers:
38+
nodes.extend(driver.list_nodes())
2639

2740
print(nodes)
2841
# [ <Node: provider=Amazon, status=RUNNING, name=bob, ip=1.2.3.4.5>,

libcloud/common/aws.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from typing import Dict
17+
from typing import Type
18+
1619
import base64
1720
from datetime import datetime
1821
import hashlib
@@ -23,7 +26,7 @@
2326
try:
2427
import simplejson as json
2528
except ImportError:
26-
import json
29+
import json # type: ignore
2730

2831
from libcloud.utils.py3 import ET
2932
from libcloud.utils.py3 import _real_unicode
@@ -96,7 +99,7 @@ class AWSGenericResponse(AWSBaseResponse):
9699
# exception class that is raised immediately.
97100
# If a custom exception class is not defined, errors are accumulated and
98101
# returned from the parse_error method.
99-
exceptions = {}
102+
exceptions = {} # type: Dict[str, Type[Exception]]
100103

101104
def success(self):
102105
return self.status in [httplib.OK, httplib.CREATED, httplib.ACCEPTED]

libcloud/common/azure.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from libcloud.common.base import ConnectionUserAndKey, RawResponse
3131
from libcloud.common.base import CertificateConnection
3232
from libcloud.common.base import XmlResponse
33+
from libcloud.common.base import BaseDriver
3334

3435
# The time format for headers in Azure requests
3536
AZURE_TIME_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
@@ -241,7 +242,7 @@ def _format_special_header_values(self, headers, method):
241242
return special_header_values
242243

243244

244-
class AzureBaseDriver(object):
245+
class AzureBaseDriver(BaseDriver):
245246
name = "Microsoft Azure Service Management API"
246247

247248

libcloud/common/azure_arm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616
try:
1717
import simplejson as json
1818
except ImportError:
19-
import json
19+
import json # type: ignore
2020

2121
import time
2222
from libcloud.utils.py3 import urlparse
2323

2424
from libcloud.common.base import (ConnectionUserAndKey,
2525
JsonResponse,
2626
RawResponse)
27+
from libcloud.common.base import BaseDriver
2728
from libcloud.http import LibcloudConnection
2829
from libcloud.utils.py3 import basestring, urlencode
2930

3031

31-
class AzureBaseDriver(object):
32+
class AzureBaseDriver(BaseDriver):
3233
name = "Microsoft Azure Resource Management API"
3334

3435

libcloud/common/base.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from typing import Union
17+
from typing import Type
18+
from typing import Optional
19+
1620
import json
1721
import os
1822
import ssl
@@ -115,10 +119,16 @@ class Response(object):
115119
A base Response class to derive from.
116120
"""
117121

118-
status = httplib.OK # Response status code
119-
headers = {} # Response headers
120-
body = None # Raw response body
121-
object = None # Parsed response body
122+
# Response status code
123+
status = httplib.OK # type: int
124+
# Response headers
125+
headers = {} # type: dict
126+
127+
# Raw response body
128+
body = None
129+
130+
# Parsed response body
131+
object = None
122132

123133
error = None # Reason returned by the server.
124134
connection = None # Parent connection class
@@ -298,11 +308,11 @@ class Connection(object):
298308
responseCls = Response
299309
rawResponseCls = RawResponse
300310
connection = None
301-
host = '127.0.0.1'
311+
host = '127.0.0.1' # type: str
302312
port = 443
303-
timeout = None
313+
timeout = None # type: Optional[Union[int, float]]
304314
secure = 1
305-
driver = None
315+
driver = None # type: Type[BaseDriver]
306316
action = None
307317
cache_busting = False
308318
backoff = None
@@ -898,7 +908,7 @@ class ConnectionUserAndKey(ConnectionKey):
898908
Base connection class which accepts a ``user_id`` and ``key`` argument.
899909
"""
900910

901-
user_id = None
911+
user_id = None # type: int
902912

903913
def __init__(self, user_id, key, secure=True, host=None, port=None,
904914
url=None, timeout=None, proxy_url=None,
@@ -917,7 +927,7 @@ class BaseDriver(object):
917927
Base driver class from which other classes can inherit from.
918928
"""
919929

920-
connectionCls = ConnectionKey
930+
connectionCls = ConnectionKey # type: Type[Connection]
921931

922932
def __init__(self, key, secret=None, secure=True, host=None, port=None,
923933
api_version=None, region=None, **kwargs):

libcloud/common/brightbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
try:
2424
import simplejson as json
2525
except ImportError:
26-
import json
26+
import json # type: ignore
2727

2828

2929
class BrightboxResponse(JsonResponse):

libcloud/common/buddyns.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
from typing import List
17+
from typing import Dict
18+
1619
from libcloud.common.base import ConnectionKey, JsonResponse
1720

1821

@@ -28,8 +31,8 @@
2831

2932

3033
class BuddyNSResponse(JsonResponse):
31-
errors = []
32-
objects = []
34+
errors = [] # type: List[Dict]
35+
objects = [] # type: List[Dict]
3336

3437
def __init__(self, response, connection):
3538
super(BuddyNSResponse, self).__init__(response=response,

libcloud/common/dimensiondata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,9 @@ def wait_for_state(self, state, func, poll_interval=2, timeout=60, *args,
600600
else:
601601
object_state = result.status
602602

603-
if object_state is state or object_state in state:
603+
if object_state is state or str(object_state) in state:
604604
return result
605+
605606
sleep(poll_interval)
606607
cnt += 1
607608

0 commit comments

Comments
 (0)
0