diff --git a/eng/tox/mypy_hard_failure_packages.py b/eng/tox/mypy_hard_failure_packages.py index b85c70560081..7619732b13b4 100644 --- a/eng/tox/mypy_hard_failure_packages.py +++ b/eng/tox/mypy_hard_failure_packages.py @@ -10,6 +10,7 @@ "azure-eventhub", "azure-identity", "azure-keyvault-administration", + "azure-keyvault-certificates", "azure-servicebus", "azure-ai-textanalytics", "azure-ai-formrecognizer", diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_client.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_client.py index 0ba57e214bbb..bc741d035501 100644 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_client.py +++ b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_client.py @@ -134,7 +134,7 @@ def begin_create_certificate(self, certificate_name, policy, **kwargs): create_certificate_polling = CreateCertificatePoller( get_certificate_command=get_certificate_command, interval=polling_interval ) - return LROPoller(command, create_certificate_operation, None, create_certificate_polling) + return LROPoller(command, create_certificate_operation, lambda *_: None, create_certificate_polling) @distributed_trace def get_certificate(self, certificate_name, **kwargs): @@ -668,13 +668,15 @@ def set_contacts(self, contacts, **kwargs): :caption: Create contacts :dedent: 8 """ - contacts = self._client.set_certificate_contacts( + new_contacts = self._client.set_certificate_contacts( vault_base_url=self.vault_url, contacts=self._models.Contacts(contact_list=[c._to_certificate_contacts_item() for c in contacts]), error_map=_error_map, **kwargs ) - return [CertificateContact._from_certificate_contacts_item(contact_item=item) for item in contacts.contact_list] + return [ + CertificateContact._from_certificate_contacts_item(contact_item=item) for item in new_contacts.contact_list + ] @distributed_trace def get_contacts(self, **kwargs): @@ -885,7 +887,7 @@ def create_issuer(self, issuer_name, provider, **kwargs): phone=contact.phone, ) for contact in admin_contacts - ] + ] # type: Optional[List[Any]] else: admin_details = None if organization_id or admin_details: @@ -946,7 +948,7 @@ def update_issuer(self, issuer_name, **kwargs): phone=contact.phone, ) for contact in admin_contacts - ] + ] # type: Optional[List[Any]] else: admin_details = None if organization_id or admin_details: diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py index 3747fe89be14..2f5df7b122ce 100644 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py +++ b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py @@ -60,26 +60,26 @@ def _from_admin_detail(cls, admin_detail): @property def email(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._email @property def first_name(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._first_name @property def last_name(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._last_name @property def phone(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._phone @@ -157,7 +157,7 @@ def __repr__(self): @classmethod def _from_certificate_item(cls, certificate_item): - # type: (models.CertificateItem) -> CertificateProperties + # type: (Union[models.CertificateItem, models.CertificateBundle]) -> CertificateProperties """Construct a CertificateProperties from an autorest-generated CertificateItem""" return cls( attributes=certificate_item.attributes, @@ -279,10 +279,10 @@ def tags(self): @property def version(self): - # type: () -> str + # type: () -> Optional[str] """The version of the certificate - :rtype: str + :rtype: str or None """ return self._vault_id.version @@ -336,28 +336,28 @@ def _from_certificate_bundle(cls, certificate_bundle): @property def id(self): - # type: () -> str + # type: () -> Optional[str] """Certificate identifier. - :rtype: str + :rtype: str or None """ - return self._properties.id + return self._properties.id if self._properties else None @property def name(self): - # type: () -> str + # type: () -> Optional[str] """The name of the certificate. - :rtype: str + :rtype: str or None """ - return self._properties.name + return self._properties.name if self._properties else None @property def properties(self): - # type: () -> CertificateProperties + # type: () -> Optional[CertificateProperties] """The certificate's properties - :rtype: ~azure.keyvault.certificates.CertificateProperties + :rtype: ~azure.keyvault.certificates.CertificateProperties or None """ return self._properties @@ -369,25 +369,25 @@ def key_id(self): @property def secret_id(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: Any or None""" return self._secret_id @property def policy(self): - # type: () -> CertificatePolicy + # type: () -> Optional[CertificatePolicy] """The management policy of the certificate. - :rtype: ~azure.keyvault.certificates.CertificatePolicy + :rtype: ~azure.keyvault.certificates.CertificatePolicy or None """ return self._policy @property def cer(self): - # type: () -> bytes + # type: () -> Optional[bytes] """The CER contents of the certificate. - :rtype: bytes + :rtype: bytes or None """ return self._cer @@ -469,7 +469,7 @@ def __init__( ): # type: (...) -> None self._id = cert_operation_id - self._vault_id = parse_key_vault_id(cert_operation_id) + self._vault_id = parse_key_vault_id(cert_operation_id) if cert_operation_id else None self._issuer_name = issuer_name self._certificate_type = certificate_type self._certificate_transparency = certificate_transparency @@ -493,7 +493,7 @@ def _from_certificate_operation_bundle(cls, certificate_operation_bundle): issuer_parameters = certificate_operation_bundle.issuer_parameters return cls( cert_operation_id=certificate_operation_bundle.id, - issuer_name=issuer_parameters.name, + issuer_name=issuer_parameters.name if issuer_parameters else None, certificate_type=( certificate_operation_bundle.issuer_parameters.certificate_type if certificate_operation_bundle.issuer_parameters @@ -513,104 +513,104 @@ def _from_certificate_operation_bundle(cls, certificate_operation_bundle): @property def id(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._id @property def name(self): - # type: () -> str - """:rtype: str""" - return self._vault_id.name + # type: () -> Optional[str] + """:rtype: str or None""" + return self._vault_id.name if self._vault_id else None @property def vault_url(self): - # type: () -> str + # type: () -> Optional[str] """URL of the vault containing the CertificateOperation - :rtype: str + :rtype: str or None """ - return self._vault_id.vault_url + return self._vault_id.vault_url if self._vault_id else None @property def issuer_name(self): - # type: () -> str + # type: () -> Union[str, WellKnownIssuerNames, None] """The name of the issuer of the certificate. - :rtype: str + :rtype: str or ~azure.keyvault.certificates.WellKnownIssuerNames or None """ return self._issuer_name @property def certificate_type(self): - # type: () -> str + # type: () -> Optional[str] """Type of certificate to be requested from the issuer provider. - :rtype: str + :rtype: str or None """ return self._certificate_type @property def certificate_transparency(self): - # type: () -> bool + # type: () -> Optional[bool] """Whether certificates generated under this policy should be published to certificate transparency logs. - :rtype: bool + :rtype: bool or None """ return self._certificate_transparency @property def csr(self): - # type: () -> bytes + # type: () -> Optional[bytes] """The certificate signing request that is being used in this certificate operation. - :rtype: bytes + :rtype: bytes or None """ return self._csr @property def cancellation_requested(self): - # type: () -> bool + # type: () -> Optional[bool] """Whether cancellation was requested on the certificate operation. - :rtype: bool + :rtype: bool or None """ return self._cancellation_requested @property def status(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._status @property def status_details(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._status_details @property def error(self): - # type: () -> CertificateOperationError - """:rtype: ~azure.keyvault.certificates.CertificateOperationError""" + # type: () -> Optional[CertificateOperationError] + """:rtype: ~azure.keyvault.certificates.CertificateOperationError or None""" return self._error @property def target(self): - # type: () -> str + # type: () -> Optional[str] """Location which contains the result of the certificate operation. - :rtype: str + :rtype: str or None """ return self._target @property def request_id(self): - # type: () -> str + # type: () -> Optional[str] """Identifier for the certificate operation. - :rtype: str + :rtype: str or None """ return self._request_id @@ -701,7 +701,7 @@ def _to_certificate_policy_bundle(self): name=self.issuer_name, certificate_type=self.certificate_type, certificate_transparency=self.certificate_transparency, # 2016-10-01 model will ignore this - ) + ) # type: Optional[models.IssuerParameters] else: issuer_parameters = None @@ -732,7 +732,7 @@ def _to_certificate_policy_bundle(self): ) ) else: - lifetime_actions = None + lifetime_actions = None # type: ignore # pylint:disable=too-many-boolean-expressions if ( @@ -745,7 +745,9 @@ def _to_certificate_policy_bundle(self): or self.validity_in_months ): if self.key_usage: - key_usage = [k.value if not isinstance(k, str) else k for k in self.key_usage] + key_usage = [ + k.value if not isinstance(k, str) else k for k in self.key_usage + ] # type: Optional[List[Union[str, KeyUsageType]]] else: key_usage = None @@ -757,7 +759,7 @@ def _to_certificate_policy_bundle(self): ), key_usage=key_usage, validity_in_months=self.validity_in_months, - ) + ) # type: Optional[models.X509CertificateProperties] else: x509_certificate_properties = None @@ -768,12 +770,14 @@ def _to_certificate_policy_bundle(self): key_size=self.key_size, reuse_key=self.reuse_key, curve=self.key_curve_name, - ) + ) # type: Optional[models.KeyProperties] else: key_properties = None if self.content_type: - secret_properties = models.SecretProperties(content_type=self.content_type) + secret_properties = models.SecretProperties( + content_type=self.content_type + ) # type: Optional[models.SecretProperties] else: secret_properties = None @@ -789,22 +793,27 @@ def _to_certificate_policy_bundle(self): @classmethod def _from_certificate_policy_bundle(cls, certificate_policy_bundle): - # type: (models.CertificatePolicy) -> CertificatePolicy + # type: (Optional[models.CertificatePolicy]) -> CertificatePolicy """Construct a CertificatePolicy from an autorest-generated CertificatePolicy""" + if certificate_policy_bundle is None: + return cls() + if certificate_policy_bundle.lifetime_actions: lifetime_actions = [ LifetimeAction( - action=(CertificatePolicyAction(item.action.action_type) if item.action.action_type else None), - lifetime_percentage=item.trigger.lifetime_percentage, - days_before_expiry=item.trigger.days_before_expiry, + action=CertificatePolicyAction(item.action.action_type) if item.action else None, + lifetime_percentage=item.trigger.lifetime_percentage if item.trigger else None, + days_before_expiry=item.trigger.days_before_expiry if item.trigger else None, ) for item in certificate_policy_bundle.lifetime_actions - ] + ] # type: Optional[List[LifetimeAction]] else: lifetime_actions = None x509_certificate_properties = certificate_policy_bundle.x509_certificate_properties if x509_certificate_properties and x509_certificate_properties.key_usage: - key_usage = [KeyUsageType(k) for k in x509_certificate_properties.key_usage] + key_usage = [ + KeyUsageType(k) for k in x509_certificate_properties.key_usage + ] # type: Optional[List[KeyUsageType]] else: key_usage = None key_properties = certificate_policy_bundle.key_properties @@ -814,9 +823,9 @@ def _from_certificate_policy_bundle(cls, certificate_policy_bundle): issuer_parameters = certificate_policy_bundle.issuer_parameters return cls( - issuer_name=issuer_parameters.name, + issuer_name=issuer_parameters.name if issuer_parameters else None, subject=(x509_certificate_properties.subject if x509_certificate_properties else None), - certificate_type=issuer_parameters.certificate_type, + certificate_type=issuer_parameters.certificate_type if issuer_parameters else None, # 2016-10-01 IssuerParameters doesn't have certificate_transparency certificate_transparency=getattr(issuer_parameters, "certificate_transparency", None), lifetime_actions=lifetime_actions, @@ -937,28 +946,28 @@ def subject(self): @property def san_emails(self): - # type: () -> List[str] + # type: () -> Optional[Any] """The subject alternative email addresses. - :rtype: list[str] + :rtype: Any or None """ return self._san_emails @property def san_dns_names(self): - # type: () -> List[str] + # type: () -> Optional[Any] """The subject alternative domain names. - :rtype: list[str] + :rtype: Any or None """ return self._san_dns_names @property def san_user_principal_names(self): - # type: () -> List[str] + # type: () -> Optional[Any] """The subject alternative user principal names. - :rtype: list[str] + :rtype: Any or None """ return self._san_user_principal_names @@ -1068,20 +1077,20 @@ def _from_certificate_contacts_item(cls, contact_item): @property def email(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._email @property def name(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._name @property def phone(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._phone @@ -1103,7 +1112,7 @@ def __repr__(self): @classmethod def _from_issuer_item(cls, issuer_item): - # type: (models.CertificateIssuerItem) -> IssuerProperties + # type: (Union[models.CertificateIssuerItem, models.IssuerBundle]) -> IssuerProperties """Construct a IssuerProperties from an autorest-generated CertificateIssuerItem""" return cls(issuer_id=issuer_item.id, provider=issuer_item.provider) @@ -1115,15 +1124,15 @@ def id(self): @property def name(self): - # type: () -> str + # type: () -> Optional[str] # Issuer name is listed under version under vault_id - """:rtype: str""" + """:rtype: str or None""" return self._vault_id.version @property def provider(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._provider @@ -1140,7 +1149,7 @@ class CertificateIssuer(object): def __init__( self, - provider, # type: str + provider, # type: Optional[str] attributes=None, # type: Optional[models.IssuerAttributes] account_id=None, # type: Optional[str] password=None, # type: Optional[str] @@ -1192,80 +1201,80 @@ def id(self): @property def name(self): - # type: () -> str + # type: () -> Optional[str] # Issuer name is listed under version under vault_id. # This is because the id we pass to parse_key_vault_id has an extra segment, so where most cases the version of # the general pattern is certificates/name/version, but here we have certificates/issuers/name/version. # Issuers are not versioned. - """:rtype: str""" + """:rtype: str or None""" return self._vault_id.version @property def provider(self): - # type: () -> str + # type: () -> Optional[str] """The issuer provider. - :rtype: str + :rtype: str or None """ return self._provider @property def enabled(self): - # type: () -> bool + # type: () -> Optional[bool] """Whether the certificate is enabled or not. - :rtype: bool + :rtype: bool or None """ return self._attributes.enabled if self._attributes else None @property def created_on(self): - # type: () -> datetime + # type: () -> Optional[datetime] """The datetime when the certificate is created. - :rtype: ~datetime.datetime + :rtype: ~datetime.datetime or None """ return self._attributes.created if self._attributes else None @property def updated_on(self): - # type: () -> datetime + # type: () -> Optional[datetime] """The datetime when the certificate was last updated. - :rtype: ~datetime.datetime + :rtype: ~datetime.datetime or None """ return self._attributes.updated if self._attributes else None @property def account_id(self): - # type: () -> str + # type: () -> Optional[str] """The username/ account name/ account id. - :rtype: str + :rtype: str or None """ return self._account_id @property def password(self): - # type: () -> str + # type: () -> Optional[str] """The password / secret / account key. - :rtype: str + :rtype: str or None """ return self._password @property def organization_id(self): - # type: () -> str - """:rtype: str""" + # type: () -> Optional[str] + """:rtype: str or None""" return self._organization_id @property def admin_contacts(self): - # type: () -> List[AdministratorContact] + # type: () -> Optional[List[AdministratorContact]] """Contact details of the organization administrator of this issuer. - :rtype: list[~azure.keyvault.certificates.AdministratorContact] + :rtype: list[~azure.keyvault.certificates.AdministratorContact] or None """ return self._admin_contacts @@ -1284,7 +1293,7 @@ class LifetimeAction(object): """ def __init__(self, action, lifetime_percentage=None, days_before_expiry=None): - # type: (CertificatePolicyAction, Optional[int], Optional[int]) -> None + # type: (Optional[CertificatePolicyAction], Optional[int], Optional[int]) -> None self._lifetime_percentage = lifetime_percentage self._days_before_expiry = days_before_expiry self._action = action @@ -1297,29 +1306,29 @@ def __repr__(self): @property def lifetime_percentage(self): - # type: () -> int + # type: () -> Optional[int] """Percentage of lifetime at which to trigger. - :rtype: int + :rtype: int or None """ return self._lifetime_percentage @property def days_before_expiry(self): - # type: () -> int + # type: () -> Optional[int] """Days before expiry to attempt renewal. - :rtype: int + :rtype: int or None """ return self._days_before_expiry @property def action(self): - # type: () -> CertificatePolicyAction + # type: () -> Optional[CertificatePolicyAction] """The type of the action that will be executed. Valid values are "EmailContacts" and "AutoRenew" - :rtype: ~azure.keyvault.certificates.CertificatePolicyAction + :rtype: ~azure.keyvault.certificates.CertificatePolicyAction or None """ return self._action diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_polling.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_polling.py index a846c356cac0..1a0946d20a54 100644 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_polling.py +++ b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_polling.py @@ -7,6 +7,16 @@ from azure.core.polling import PollingMethod +try: + from typing import TYPE_CHECKING +except ImportError: + TYPE_CHECKING = False + +if TYPE_CHECKING: + # pylint: disable=ungrouped-imports + from typing import Any, Callable, Union + from azure.keyvault.certificates import KeyVaultCertificate, CertificateOperation + logger = logging.getLogger(__name__) diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_shared/_polling.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_shared/_polling.py index df63ad0a70ec..ec1898453d81 100644 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_shared/_polling.py +++ b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_shared/_polling.py @@ -33,11 +33,11 @@ class KeyVaultOperationPoller(LROPoller): # pylint: disable=arguments-differ def __init__(self, polling_method): # type: (PollingMethod) -> None - super(KeyVaultOperationPoller, self).__init__(None, None, None, NoPolling()) + super(KeyVaultOperationPoller, self).__init__(None, None, lambda *_: None, NoPolling()) self._polling_method = polling_method # pylint: disable=arguments-differ - def result(self): + def result(self): # type: ignore # type: () -> Any """Returns a representation of the final resource without waiting for the operation to complete. diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/aio/_client.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/aio/_client.py index ebe294092d39..1fe6569bfe0f 100644 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/aio/_client.py +++ b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/aio/_client.py @@ -638,13 +638,15 @@ async def set_contacts(self, contacts: Iterable[CertificateContact], **kwargs: " :caption: Create contacts :dedent: 8 """ - contacts = await self._client.set_certificate_contacts( + new_contacts = await self._client.set_certificate_contacts( vault_base_url=self.vault_url, contacts=self._models.Contacts(contact_list=[c._to_certificate_contacts_item() for c in contacts]), error_map=_error_map, **kwargs ) - return [CertificateContact._from_certificate_contacts_item(contact_item=item) for item in contacts.contact_list] + return [ + CertificateContact._from_certificate_contacts_item(contact_item=item) for item in new_contacts.contact_list + ] @distributed_trace_async async def get_contacts(self, **kwargs: "Any") -> List[CertificateContact]: @@ -861,7 +863,7 @@ async def create_issuer(self, issuer_name: str, provider: str, **kwargs: "Any") phone=contact.phone, ) for contact in admin_contacts - ] + ] # type: Optional[List[Any]] else: admin_details = None if organization_id or admin_details: @@ -922,7 +924,7 @@ async def update_issuer(self, issuer_name: str, **kwargs: "Any") -> CertificateI phone=contact.phone, ) for contact in admin_contacts - ) + ) # type: Optional[List[Any]] else: admin_details = None if organization_id or admin_details: diff --git a/sdk/keyvault/azure-keyvault-certificates/mypy.ini b/sdk/keyvault/azure-keyvault-certificates/mypy.ini new file mode 100644 index 000000000000..18b37b44c426 --- /dev/null +++ b/sdk/keyvault/azure-keyvault-certificates/mypy.ini @@ -0,0 +1,7 @@ +[mypy] +python_version = 3.6 +warn_unused_configs = True +ignore_missing_imports = True + +[mypy-azure.keyvault.*._generated.*] +ignore_errors = True