Skip to content

Commit

Permalink
Run mypy in azure-keyvault-keys CI (Azure#20545)
Browse files Browse the repository at this point in the history
  • Loading branch information
YalinLi0312 authored Sep 9, 2021
1 parent 51f09fa commit 7373db2
Show file tree
Hide file tree
Showing 16 changed files with 191 additions and 133 deletions.
1 change: 1 addition & 0 deletions eng/tox/mypy_hard_failure_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"azure-identity",
"azure-keyvault-administration",
"azure-keyvault-certificates",
"azure-keyvault-keys",
"azure-keyvault-secrets",
"azure-servicebus",
"azure-ai-textanalytics",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
# pylint:disable=unused-import
from typing import Any, Optional, Union
from azure.core.paging import ItemPaged
from azure.core.polling import LROPoller
from ._models import JsonWebKey
from ._enums import KeyType


class KeyClient(KeyVaultClientBase):
Expand Down Expand Up @@ -55,7 +57,7 @@ def _get_attributes(self, enabled, not_before, expires_on, exportable=None):

@distributed_trace
def create_key(self, name, key_type, **kwargs):
# type: (str, Union[str, azure.keyvault.keys.KeyType], **Any) -> KeyVaultKey
# type: (str, Union[str, KeyType], **Any) -> KeyVaultKey
"""Create a key or, if ``name`` is already in use, create a new version of the key.
Requires keys/create permission.
Expand Down Expand Up @@ -242,7 +244,7 @@ def create_oct_key(self, name, **kwargs):

@distributed_trace
def begin_delete_key(self, name, **kwargs):
# type: (str, **Any) -> DeletedKey
# type: (str, **Any) -> LROPoller
"""Delete all versions of a key and its cryptographic material.
Requires keys/delete permission. When this method returns Key Vault has begun deleting the key. Deletion may
Expand Down Expand Up @@ -450,7 +452,7 @@ def purge_deleted_key(self, name, **kwargs):

@distributed_trace
def begin_recover_deleted_key(self, name, **kwargs):
# type: (str, **Any) -> KeyVaultKey
# type: (str, **Any) -> LROPoller
"""Recover a deleted key to its latest version. Possible only in a vault with soft-delete enabled.
Requires keys/recover permission.
Expand Down
87 changes: 48 additions & 39 deletions sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

if TYPE_CHECKING:
# pylint:disable=unused-import
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, List
from datetime import datetime
from ._generated.v7_0 import models as _models
from ._enums import KeyOperation
from ._enums import KeyOperation, KeyType

KeyOperationResult = namedtuple("KeyOperationResult", ["id", "value"])

Expand Down Expand Up @@ -83,13 +83,15 @@ def _from_key_bundle(cls, key_bundle):
"""Construct a KeyProperties from an autorest-generated KeyBundle"""
# release_policy was added in 7.3-preview
release_policy = None
if hasattr(key_bundle, "release_policy") and key_bundle.release_policy is not None:
if (hasattr(key_bundle, "release_policy") and
key_bundle.release_policy is not None): # type: ignore[attr-defined]
release_policy = KeyReleasePolicy(
data=key_bundle.release_policy.data, content_type=key_bundle.release_policy.content_type
data=key_bundle.release_policy.data, # type: ignore[attr-defined]
content_type=key_bundle.release_policy.content_type # type: ignore[attr-defined]
)

return cls(
key_bundle.key.kid,
key_bundle.key.kid, # type: ignore
attributes=key_bundle.attributes,
managed=key_bundle.managed,
tags=key_bundle.tags,
Expand All @@ -100,7 +102,12 @@ def _from_key_bundle(cls, key_bundle):
def _from_key_item(cls, key_item):
# type: (_models.KeyItem) -> KeyProperties
"""Construct a KeyProperties from an autorest-generated KeyItem"""
return cls(key_id=key_item.kid, attributes=key_item.attributes, managed=key_item.managed, tags=key_item.tags)
return cls(
key_id=key_item.kid, # type: ignore
attributes=key_item.attributes,
managed=key_item.managed,
tags=key_item.tags
)

@property
def id(self):
Expand All @@ -122,57 +129,57 @@ def name(self):

@property
def version(self):
# type: () -> str
# type: () -> Optional[str]
"""The key's version
:rtype: str
:rtype: str or None
"""
return self._vault_id.version

@property
def enabled(self):
# type: () -> bool
# type: () -> Optional[bool]
"""Whether the key is enabled for use
:rtype: bool
:rtype: bool or None
"""
return self._attributes.enabled
return self._attributes.enabled if self._attributes else None

@property
def not_before(self):
# type: () -> datetime
# type: () -> Optional[datetime]
"""The time before which the key can not be used, in UTC
:rtype: ~datetime.datetime
:rtype: ~datetime.datetime or None
"""
return self._attributes.not_before
return self._attributes.not_before if self._attributes else None

@property
def expires_on(self):
# type: () -> datetime
# type: () -> Optional[datetime]
"""When the key will expire, in UTC
:rtype: ~datetime.datetime
:rtype: ~datetime.datetime or None
"""
return self._attributes.expires
return self._attributes.expires if self._attributes else None

@property
def created_on(self):
# type: () -> datetime
# type: () -> Optional[datetime]
"""When the key was created, in UTC
:rtype: ~datetime.datetime
:rtype: ~datetime.datetime or None
"""
return self._attributes.created
return self._attributes.created if self._attributes else None

@property
def updated_on(self):
# type: () -> datetime
# type: () -> Optional[datetime]
"""When the key was last updated, in UTC
:rtype: ~datetime.datetime
:rtype: ~datetime.datetime or None
"""
return self._attributes.updated
return self._attributes.updated if self._attributes else None

@property
def vault_url(self):
Expand All @@ -188,7 +195,7 @@ def recoverable_days(self):
# type: () -> Optional[int]
"""The number of days the key is retained before being deleted from a soft-delete enabled Key Vault.
:rtype: int
:rtype: int or None
"""
# recoverable_days was added in 7.1-preview
if self._attributes:
Expand All @@ -197,12 +204,12 @@ def recoverable_days(self):

@property
def recovery_level(self):
# type: () -> str
# type: () -> Optional[str]
"""The vault's deletion recovery level for keys
:rtype: str
:rtype: str or None
"""
return self._attributes.recovery_level
return self._attributes.recovery_level if self._attributes else None

@property
def tags(self):
Expand Down Expand Up @@ -326,7 +333,7 @@ def _from_key_bundle(cls, key_bundle):
"""Construct a KeyVaultKey from an autorest-generated KeyBundle"""
# pylint:disable=protected-access
return cls(
key_id=key_bundle.key.kid,
key_id=key_bundle.key.kid, # type: ignore
jwk={field: getattr(key_bundle.key, field, None) for field in JsonWebKey._FIELDS},
properties=KeyProperties._from_key_bundle(key_bundle),
)
Expand Down Expand Up @@ -369,21 +376,23 @@ def key(self):

@property
def key_type(self):
# type: () -> str
# type: () -> KeyType
"""The key's type. See :class:`~azure.keyvault.keys.KeyType` for possible values.
:rtype: ~azure.keyvault.keys.KeyType or str
"""
return self._key_material.kty # pylint:disable=no-member
# pylint:disable=no-member
return self._key_material.kty # type: ignore[attr-defined]

@property
def key_operations(self):
# type: () -> list[KeyOperation]
# type: () -> List[KeyOperation]
"""Permitted operations. See :class:`~azure.keyvault.keys.KeyOperation` for possible values.
:rtype: list[~azure.keyvault.keys.KeyOperation or str]
"""
return self._key_material.key_ops # pylint:disable=no-member
# pylint:disable=no-member
return self._key_material.key_ops # type: ignore[attr-defined]


class KeyVaultKeyIdentifier(object):
Expand Down Expand Up @@ -454,7 +463,7 @@ def _from_deleted_key_bundle(cls, deleted_key_bundle):
# pylint:disable=protected-access
return cls(
properties=KeyProperties._from_key_bundle(deleted_key_bundle),
key_id=deleted_key_bundle.key.kid,
key_id=deleted_key_bundle.key.kid, # type: ignore
jwk={field: getattr(deleted_key_bundle.key, field, None) for field in JsonWebKey._FIELDS},
deleted_date=deleted_key_bundle.deleted_date,
recovery_id=deleted_key_bundle.recovery_id,
Expand All @@ -475,28 +484,28 @@ def _from_deleted_key_item(cls, deleted_key_item):

@property
def deleted_date(self):
# type: () -> datetime
# type: () -> Optional[datetime]
"""When the key was deleted, in UTC
:rtype: ~datetime.datetime
:rtype: ~datetime.datetime or None
"""
return self._deleted_date

@property
def recovery_id(self):
# type: () -> str
# type: () -> Optional[str]
"""An identifier used to recover the deleted key. Returns ``None`` if soft-delete is disabled.
:rtype: str
:rtype: str or None
"""
return self._recovery_id

@property
def scheduled_purge_date(self):
# type: () -> datetime
# type: () -> Optional[datetime]
"""When the key is scheduled to be purged, in UTC. Returns ``None`` if soft-delete is disabled.
:rtype: ~datetime.datetime
:rtype: ~datetime.datetime or None
"""
return self._scheduled_purge_date

Expand Down
Loading

0 comments on commit 7373db2

Please sign in to comment.