TechDebt: MyPy Comprehend (#5630)
This commit is contained in:
parent
ecafc89153
commit
d3b16b3052
@ -3,7 +3,7 @@ from moto.core.exceptions import JsonRESTError
|
|||||||
|
|
||||||
|
|
||||||
class ResourceNotFound(JsonRESTError):
|
class ResourceNotFound(JsonRESTError):
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
super().__init__(
|
super().__init__(
|
||||||
"ResourceNotFoundException",
|
"ResourceNotFoundException",
|
||||||
"RESOURCE_NOT_FOUND: Could not find specified resource.",
|
"RESOURCE_NOT_FOUND: Could not find specified resource.",
|
||||||
|
@ -4,23 +4,23 @@ from moto.core import BaseBackend, BaseModel
|
|||||||
from moto.core.utils import BackendDict
|
from moto.core.utils import BackendDict
|
||||||
from moto.utilities.tagging_service import TaggingService
|
from moto.utilities.tagging_service import TaggingService
|
||||||
from .exceptions import ResourceNotFound
|
from .exceptions import ResourceNotFound
|
||||||
from typing import Dict
|
from typing import Any, Dict, List, Iterable
|
||||||
|
|
||||||
|
|
||||||
class EntityRecognizer(BaseModel):
|
class EntityRecognizer(BaseModel):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
region_name,
|
region_name: str,
|
||||||
account_id,
|
account_id: str,
|
||||||
language_code,
|
language_code: str,
|
||||||
input_data_config,
|
input_data_config: Dict[str, Any],
|
||||||
data_access_role_arn,
|
data_access_role_arn: str,
|
||||||
version_name,
|
version_name: str,
|
||||||
recognizer_name,
|
recognizer_name: str,
|
||||||
volume_kms_key_id,
|
volume_kms_key_id: str,
|
||||||
vpc_config,
|
vpc_config: Dict[str, List[str]],
|
||||||
model_kms_key_id,
|
model_kms_key_id: str,
|
||||||
model_policy,
|
model_policy: str,
|
||||||
):
|
):
|
||||||
self.name = recognizer_name
|
self.name = recognizer_name
|
||||||
self.arn = f"arn:aws:comprehend:{region_name}:{account_id}:entity-recognizer/{recognizer_name}"
|
self.arn = f"arn:aws:comprehend:{region_name}:{account_id}:entity-recognizer/{recognizer_name}"
|
||||||
@ -36,7 +36,7 @@ class EntityRecognizer(BaseModel):
|
|||||||
self.model_policy = model_policy
|
self.model_policy = model_policy
|
||||||
self.status = "TRAINED"
|
self.status = "TRAINED"
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
"EntityRecognizerArn": self.arn,
|
"EntityRecognizerArn": self.arn,
|
||||||
"LanguageCode": self.language_code,
|
"LanguageCode": self.language_code,
|
||||||
@ -54,12 +54,14 @@ class EntityRecognizer(BaseModel):
|
|||||||
class ComprehendBackend(BaseBackend):
|
class ComprehendBackend(BaseBackend):
|
||||||
"""Implementation of Comprehend APIs."""
|
"""Implementation of Comprehend APIs."""
|
||||||
|
|
||||||
def __init__(self, region_name, account_id):
|
def __init__(self, region_name: str, account_id: str):
|
||||||
super().__init__(region_name, account_id)
|
super().__init__(region_name, account_id)
|
||||||
self.recognizers: Dict[str, EntityRecognizer] = dict()
|
self.recognizers: Dict[str, EntityRecognizer] = dict()
|
||||||
self.tagger = TaggingService()
|
self.tagger = TaggingService()
|
||||||
|
|
||||||
def list_entity_recognizers(self, _filter):
|
def list_entity_recognizers(
|
||||||
|
self, _filter: Dict[str, Any]
|
||||||
|
) -> Iterable[EntityRecognizer]:
|
||||||
"""
|
"""
|
||||||
Pagination is not yet implemented.
|
Pagination is not yet implemented.
|
||||||
The following filters are not yet implemented: Status, SubmitTimeBefore, SubmitTimeAfter
|
The following filters are not yet implemented: Status, SubmitTimeBefore, SubmitTimeAfter
|
||||||
@ -74,17 +76,17 @@ class ComprehendBackend(BaseBackend):
|
|||||||
|
|
||||||
def create_entity_recognizer(
|
def create_entity_recognizer(
|
||||||
self,
|
self,
|
||||||
recognizer_name,
|
recognizer_name: str,
|
||||||
version_name,
|
version_name: str,
|
||||||
data_access_role_arn,
|
data_access_role_arn: str,
|
||||||
tags,
|
tags: List[Dict[str, str]],
|
||||||
input_data_config,
|
input_data_config: Dict[str, Any],
|
||||||
language_code,
|
language_code: str,
|
||||||
volume_kms_key_id,
|
volume_kms_key_id: str,
|
||||||
vpc_config,
|
vpc_config: Dict[str, List[str]],
|
||||||
model_kms_key_id,
|
model_kms_key_id: str,
|
||||||
model_policy,
|
model_policy: str,
|
||||||
):
|
) -> str:
|
||||||
"""
|
"""
|
||||||
The ClientRequestToken-parameter is not yet implemented
|
The ClientRequestToken-parameter is not yet implemented
|
||||||
"""
|
"""
|
||||||
@ -105,26 +107,28 @@ class ComprehendBackend(BaseBackend):
|
|||||||
self.tagger.tag_resource(recognizer.arn, tags)
|
self.tagger.tag_resource(recognizer.arn, tags)
|
||||||
return recognizer.arn
|
return recognizer.arn
|
||||||
|
|
||||||
def describe_entity_recognizer(self, entity_recognizer_arn) -> EntityRecognizer:
|
def describe_entity_recognizer(
|
||||||
|
self, entity_recognizer_arn: str
|
||||||
|
) -> EntityRecognizer:
|
||||||
if entity_recognizer_arn not in self.recognizers:
|
if entity_recognizer_arn not in self.recognizers:
|
||||||
raise ResourceNotFound
|
raise ResourceNotFound
|
||||||
return self.recognizers[entity_recognizer_arn]
|
return self.recognizers[entity_recognizer_arn]
|
||||||
|
|
||||||
def stop_training_entity_recognizer(self, entity_recognizer_arn):
|
def stop_training_entity_recognizer(self, entity_recognizer_arn: str) -> None:
|
||||||
recognizer = self.describe_entity_recognizer(entity_recognizer_arn)
|
recognizer = self.describe_entity_recognizer(entity_recognizer_arn)
|
||||||
if recognizer.status == "TRAINING":
|
if recognizer.status == "TRAINING":
|
||||||
recognizer.status = "STOP_REQUESTED"
|
recognizer.status = "STOP_REQUESTED"
|
||||||
|
|
||||||
def list_tags_for_resource(self, resource_arn):
|
def list_tags_for_resource(self, resource_arn: str) -> List[Dict[str, str]]:
|
||||||
return self.tagger.list_tags_for_resource(resource_arn)["Tags"]
|
return self.tagger.list_tags_for_resource(resource_arn)["Tags"]
|
||||||
|
|
||||||
def delete_entity_recognizer(self, entity_recognizer_arn):
|
def delete_entity_recognizer(self, entity_recognizer_arn: str) -> None:
|
||||||
self.recognizers.pop(entity_recognizer_arn, None)
|
self.recognizers.pop(entity_recognizer_arn, None)
|
||||||
|
|
||||||
def tag_resource(self, resource_arn, tags):
|
def tag_resource(self, resource_arn: str, tags: List[Dict[str, str]]) -> None:
|
||||||
self.tagger.tag_resource(resource_arn, tags)
|
self.tagger.tag_resource(resource_arn, tags)
|
||||||
|
|
||||||
def untag_resource(self, resource_arn, tag_keys):
|
def untag_resource(self, resource_arn: str, tag_keys: List[str]) -> None:
|
||||||
self.tagger.untag_resource_using_names(resource_arn, tag_keys)
|
self.tagger.untag_resource_using_names(resource_arn, tag_keys)
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,21 +2,21 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from moto.core.responses import BaseResponse
|
from moto.core.responses import BaseResponse
|
||||||
from .models import comprehend_backends
|
from .models import comprehend_backends, ComprehendBackend
|
||||||
|
|
||||||
|
|
||||||
class ComprehendResponse(BaseResponse):
|
class ComprehendResponse(BaseResponse):
|
||||||
"""Handler for Comprehend requests and responses."""
|
"""Handler for Comprehend requests and responses."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
super().__init__(service_name="comprehend")
|
super().__init__(service_name="comprehend")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def comprehend_backend(self):
|
def comprehend_backend(self) -> ComprehendBackend:
|
||||||
"""Return backend instance specific for this region."""
|
"""Return backend instance specific for this region."""
|
||||||
return comprehend_backends[self.current_account][self.region]
|
return comprehend_backends[self.current_account][self.region]
|
||||||
|
|
||||||
def list_entity_recognizers(self):
|
def list_entity_recognizers(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
_filter = params.get("Filter", {})
|
_filter = params.get("Filter", {})
|
||||||
recognizers = self.comprehend_backend.list_entity_recognizers(_filter=_filter)
|
recognizers = self.comprehend_backend.list_entity_recognizers(_filter=_filter)
|
||||||
@ -24,7 +24,7 @@ class ComprehendResponse(BaseResponse):
|
|||||||
dict(EntityRecognizerPropertiesList=[r.to_dict() for r in recognizers])
|
dict(EntityRecognizerPropertiesList=[r.to_dict() for r in recognizers])
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_entity_recognizer(self):
|
def create_entity_recognizer(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
recognizer_name = params.get("RecognizerName")
|
recognizer_name = params.get("RecognizerName")
|
||||||
version_name = params.get("VersionName")
|
version_name = params.get("VersionName")
|
||||||
@ -50,7 +50,7 @@ class ComprehendResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
return json.dumps(dict(EntityRecognizerArn=entity_recognizer_arn))
|
return json.dumps(dict(EntityRecognizerArn=entity_recognizer_arn))
|
||||||
|
|
||||||
def describe_entity_recognizer(self):
|
def describe_entity_recognizer(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
entity_recognizer_arn = params.get("EntityRecognizerArn")
|
entity_recognizer_arn = params.get("EntityRecognizerArn")
|
||||||
recognizer = self.comprehend_backend.describe_entity_recognizer(
|
recognizer = self.comprehend_backend.describe_entity_recognizer(
|
||||||
@ -58,7 +58,7 @@ class ComprehendResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
return json.dumps(dict(EntityRecognizerProperties=recognizer.to_dict()))
|
return json.dumps(dict(EntityRecognizerProperties=recognizer.to_dict()))
|
||||||
|
|
||||||
def stop_training_entity_recognizer(self):
|
def stop_training_entity_recognizer(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
entity_recognizer_arn = params.get("EntityRecognizerArn")
|
entity_recognizer_arn = params.get("EntityRecognizerArn")
|
||||||
self.comprehend_backend.stop_training_entity_recognizer(
|
self.comprehend_backend.stop_training_entity_recognizer(
|
||||||
@ -66,7 +66,7 @@ class ComprehendResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
return json.dumps(dict())
|
return json.dumps(dict())
|
||||||
|
|
||||||
def list_tags_for_resource(self):
|
def list_tags_for_resource(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
resource_arn = params.get("ResourceArn")
|
resource_arn = params.get("ResourceArn")
|
||||||
tags = self.comprehend_backend.list_tags_for_resource(
|
tags = self.comprehend_backend.list_tags_for_resource(
|
||||||
@ -74,7 +74,7 @@ class ComprehendResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
return json.dumps(dict(ResourceArn=resource_arn, Tags=tags))
|
return json.dumps(dict(ResourceArn=resource_arn, Tags=tags))
|
||||||
|
|
||||||
def delete_entity_recognizer(self):
|
def delete_entity_recognizer(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
entity_recognizer_arn = params.get("EntityRecognizerArn")
|
entity_recognizer_arn = params.get("EntityRecognizerArn")
|
||||||
self.comprehend_backend.delete_entity_recognizer(
|
self.comprehend_backend.delete_entity_recognizer(
|
||||||
@ -82,14 +82,14 @@ class ComprehendResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
return "{}"
|
return "{}"
|
||||||
|
|
||||||
def tag_resource(self):
|
def tag_resource(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
resource_arn = params.get("ResourceArn")
|
resource_arn = params.get("ResourceArn")
|
||||||
tags = params.get("Tags")
|
tags = params.get("Tags")
|
||||||
self.comprehend_backend.tag_resource(resource_arn, tags)
|
self.comprehend_backend.tag_resource(resource_arn, tags)
|
||||||
return "{}"
|
return "{}"
|
||||||
|
|
||||||
def untag_resource(self):
|
def untag_resource(self) -> str:
|
||||||
params = json.loads(self.body)
|
params = json.loads(self.body)
|
||||||
resource_arn = params.get("ResourceArn")
|
resource_arn = params.get("ResourceArn")
|
||||||
tag_keys = params.get("TagKeys")
|
tag_keys = params.get("TagKeys")
|
||||||
|
@ -18,7 +18,7 @@ disable = W,C,R,E
|
|||||||
enable = anomalous-backslash-in-string, arguments-renamed, dangerous-default-value, deprecated-module, function-redefined, import-self, redefined-builtin, redefined-outer-name, reimported, pointless-statement, super-with-arguments, unused-argument, unused-import, unused-variable, useless-else-on-loop, wildcard-import
|
enable = anomalous-backslash-in-string, arguments-renamed, dangerous-default-value, deprecated-module, function-redefined, import-self, redefined-builtin, redefined-outer-name, reimported, pointless-statement, super-with-arguments, unused-argument, unused-import, unused-variable, useless-else-on-loop, wildcard-import
|
||||||
|
|
||||||
[mypy]
|
[mypy]
|
||||||
files= moto/a*,moto/b*,moto/ce,moto/cloudformation,moto/cloudfront,moto/cloudtrail,moto/codebuild,moto/cloudwatch,moto/codepipeline,moto/codecommit,moto/cognito*
|
files= moto/a*,moto/b*,moto/ce,moto/cloudformation,moto/cloudfront,moto/cloudtrail,moto/codebuild,moto/cloudwatch,moto/codepipeline,moto/codecommit,moto/cognito*,moto/comprehend
|
||||||
show_column_numbers=True
|
show_column_numbers=True
|
||||||
show_error_codes = True
|
show_error_codes = True
|
||||||
disable_error_code=abstract
|
disable_error_code=abstract
|
||||||
|
Loading…
Reference in New Issue
Block a user