Skip to content

Add type annotations for the base DNS API #1434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ Storage
(GITHUB-1431)
[Tomaz Muraus]

- Add type annotations for the base storage API.
(GITHUB-1410)
[Clemens Wolff - @c-w]

DNS
~~~

Expand All @@ -84,10 +88,14 @@ DNS
didn't contain ``priority`` key.

Reported by James Montgomery - @gh-jamesmontgomery.

(GITHUB-1428, GITHUB-1429)
[Tomaz Muraus]

- Add type annotations for the base DNS API.
(GITHUB-1434)
[Tomaz Muraus]

Container
~~~~~~~~~

Expand Down
10 changes: 2 additions & 8 deletions example_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@

from typing import Type, cast

ec2_cls = get_driver(Provider.EC2)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly this doesn't work as expected.

This means we need to rely on base / parent class annotations until we have type annotations for all the driver subclasses.

#1306 (comment)

rackspace_cls = get_driver(Provider.RACKSPACE)

# NOTE: If you are using driver methods which are not part of the standard API,
# you need to explicitly cast the driver class reference to the correct class
# for type checking to work correctly
EC2 = cast(Type[EC2NodeDriver], ec2_cls)
Rackspace = cast(Type[RackspaceNodeDriver], rackspace_cls)
EC2 = get_driver(Provider.EC2)
Rackspace = get_driver(Provider.RACKSPACE)

drivers = [EC2('access key id', 'secret key', region='us-east-1'),
Rackspace('username', 'api key', region='iad')]
Expand Down
60 changes: 60 additions & 0 deletions libcloud/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@

from typing import Optional
from typing import Callable
from typing import Union
from typing import cast

from enum import Enum

from libcloud.utils.py3 import httplib
from libcloud.utils.py3 import _real_unicode

if False:
# Work around for MYPY for cyclic import problem
from libcloud.compute.base import BaseDriver

__all__ = [
"Type",
"LibcloudError",
"MalformedResponseError",
"ProviderError",
Expand All @@ -32,6 +38,60 @@
]


class Type(Enum):
@classmethod
def tostring(cls, value):
# type: (Union[Enum, str]) -> str
"""Return the string representation of the state object attribute
:param str value: the state object to turn into string
:return: the uppercase string that represents the state object
:rtype: str
"""
value = cast(Enum, value)
return str(value._value_).upper()

@classmethod
def fromstring(cls, value):
# type: (str) -> str
"""Return the state object attribute that matches the string
:param str value: the string to look up
:return: the state object attribute that matches the string
:rtype: str
"""
return getattr(cls, value.upper(), None)

"""
NOTE: These methods are here for backward compatibility reasons where
Type values were simple strings and Type didn't inherit from Enum.
"""

def __eq__(self, other):
if isinstance(other, Type):
return other.value == self.value
elif isinstance(other, (str, _real_unicode)):
return self.value == other

return super(Type, self).__eq__(other)

def upper(self):
return self.value.upper() # pylint: disable=no-member

def lower(self):
return self.value.lower() # pylint: disable=no-member

def __ne__(self, other):
return not self.__eq__(other)

def __str__(self):
return str(self.value)

def __repr__(self):
return self.value

def __hash__(self):
return id(self)


class LibcloudError(Exception):
"""The base class for other libcloud exceptions"""

Expand Down
61 changes: 1 addition & 60 deletions libcloud/compute/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
Base types used by other parts of libcloud
"""

from typing import Union
from typing import cast

from enum import Enum

from libcloud.utils.py3 import _real_unicode
from libcloud.common.types import Type
from libcloud.common.types import LibcloudError, MalformedResponseError
from libcloud.common.types import InvalidCredsError, InvalidCredsException

Expand All @@ -41,60 +36,6 @@
]


class Type(Enum):
@classmethod
def tostring(cls, value):
# type: (Union[Enum, str]) -> str
"""Return the string representation of the state object attribute
:param str value: the state object to turn into string
:return: the uppercase string that represents the state object
:rtype: str
"""
value = cast(Enum, value)
return str(value._value_).upper()

@classmethod
def fromstring(cls, value):
# type: (str) -> str
"""Return the state object attribute that matches the string
:param str value: the string to look up
:return: the state object attribute that matches the string
:rtype: str
"""
return getattr(cls, value.upper(), None)

"""
NOTE: These methods are here for backward compatibility reasons where
Type values were simple strings and Type didn't inherit from Enum.
"""

def __eq__(self, other):
if isinstance(other, Type):
return other.value == self.value
elif isinstance(other, (str, _real_unicode)):
return self.value == other

return super(Type, self).__eq__(other)

def upper(self):
return self.value.upper() # pylint: disable=no-member

def lower(self):
return self.value.lower() # pylint: disable=no-member

def __ne__(self, other):
return not self.__eq__(other)

def __str__(self):
return str(self.value)

def __repr__(self):
return self.value

def __hash__(self):
return id(self)


class Provider(Type):
"""
Defines for each of the supported providers
Expand Down
Loading