From 8b4947d8b30b1c829d85f181cd044d30a7fd6c7a Mon Sep 17 00:00:00 2001 From: Maciej Strzelczyk Date: Sat, 27 May 2023 21:04:21 +0200 Subject: [PATCH] fixit: Add type-hints to Python sample at functions/ocr/app (#9990) * fixit: Add type-hints to Python sample at functions/ocr/app * Fixing stuff * Fixing stuff * Enforcing type checks * Enforcing type checks * Fixing any typing * chore(deps): update dependency shapely to v2 (#10004) * Adding deletion of rows with NULL dag_id (#10002) Co-authored-by: Charles Engelke * fixit: add type-hints to functions/v2/ocr (#9980) b/280879671 ## Description Fixes b/280879671 Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google. ## Checklist - [ ] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file) - [ ] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample - [ ] This sample adds a new **Product API**, and I updated the [Blunderbuss issue/PR auto-assigner](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/blunderbuss.yml) with the codeowners for this sample - [ ] Please **merge** this PR for me once it is approved * [DLP] Implemenetd dlp_inspect_image_listed_infotypes with unit test cases (#9872) ## Description Implemenetd dlp_inspect_image_listed_infotypes with unit test cases. Java equivalent: https://cloud.google.com/dlp/docs/samples/dlp-inspect-image-listed-infotypes#dlp_inspect_image_listed_infotypes-java Fixes # Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google. ## Checklist - [X] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md) - [ ] README is updated to include [all relevant information](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file) - [X] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [X] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [ ] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/CODEOWNERS) with the codeowners for this sample - [ ] This sample adds a new **Product API**, and I updated the [Blunderbuss issue/PR auto-assigner](https://togithub.com/GoogleCloudPlatform/python-docs-samples/blob/main/.github/blunderbuss.yml) with the codeowners for this sample - [X] Please **merge** this PR for me once it is approved * chore(deps): update dependency structlog to v22.3.0 (#8540) Co-authored-by: Charles Engelke * fixit: update docstrings for Cloud Run samples (#10007) * chore: update docstrings for Cloud Run samples * lint * Update render.py * fix: Add type hints for KMS snippets (#9979) * fix: Add type hints for KMS snippets * fix: type * fix: even more types * black * fix: remove debugging note * isort * manual sort, apparently * Move all imports to top of files, sort, move region tags * even more import fixes * black, isort * black, again * Fixing typing info * Trying to fix typing --------- Co-authored-by: Charles Engelke Co-authored-by: Mend Renovate Co-authored-by: kubasieron <89135874+kubasieron@users.noreply.github.com> Co-authored-by: Avani-Thakker-Crest <129363704+Avani-Thakker-Crest@users.noreply.github.com> Co-authored-by: Averi Kitsch Co-authored-by: Katie McLaughlin Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com> --- functions/ocr/app/main.py | 90 +++++++++++++++++++++++------ functions/ocr/app/main_test.py | 2 + functions/ocr/app/noxfile_config.py | 1 + 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/functions/ocr/app/main.py b/functions/ocr/app/main.py index 29b47aab40ff..a657ae22a317 100644 --- a/functions/ocr/app/main.py +++ b/functions/ocr/app/main.py @@ -1,6 +1,6 @@ # Copyright 2018 Google LLC # -# Licensed under the Apache License, Version 2.0 (the "License"); +# Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # @@ -16,6 +16,7 @@ import base64 import json import os +from typing import Dict, TypeVar from google.cloud import pubsub_v1 from google.cloud import storage @@ -32,8 +33,22 @@ # [START functions_ocr_detect] -def detect_text(bucket, filename): - print(f"Looking for text in image {filename}") +def detect_text(bucket: str, filename: str) -> None: + """ + Extract the text from an image uploaded to Cloud Storage. + + Extract the text from an image uploaded to Cloud Storage, then + publish messages requesting subscribing services translate the text + to each target language and save the result. + + Args: + bucket: name of GCS bucket in which the file is stored. + filename: name of the file to be read. + + Returns: + None; the output is written to stdout and Stackdriver Logging. + """ + print("Looking for text in image {}".format(filename)) futures = [] @@ -74,9 +89,22 @@ def detect_text(bucket, filename): # [END functions_ocr_detect] +T = TypeVar('T') + # [START message_validatation_helper] -def validate_message(message, param): +def validate_message(message: Dict[str, T], param: str) -> T: + """ + Placeholder function for validating message parts. + + Args: + message: message to be validated. + param: name of the message parameter to be validated. + + Returns: + The value of message['param'] if it's valid. Throws ValueError + if it's not valid. + """ var = message.get(param) if not var: raise ValueError( @@ -86,34 +114,49 @@ def validate_message(message, param): ) ) return var - - # [END message_validatation_helper] # [START functions_ocr_process] -def process_image(file, context): +def process_image(file_info: dict, context: dict) -> None: """Cloud Function triggered by Cloud Storage when a file is changed. + Args: - file (dict): Metadata of the changed file, provided by the triggering - Cloud Storage event. - context (google.cloud.functions.Context): Metadata of triggering event. + file_info: Metadata of the changed file, provided by the + triggering Cloud Storage event. + context: a dictionary containing metadata about the event. + Returns: - None; the output is written to stdout and Stackdriver Logging + None; the output is written to stdout and Stackdriver Logging. """ - bucket = validate_message(file, "bucket") - name = validate_message(file, "name") + bucket = validate_message(file_info, "bucket") + name = validate_message(file_info, "name") detect_text(bucket, name) - print("File {} processed.".format(file["name"])) + print("File {} processed.".format(file_info["name"])) # [END functions_ocr_process] # [START functions_ocr_translate] -def translate_text(event, context): +def translate_text(event: dict, context: dict) -> None: + """ + Cloud Function triggered by PubSub when a message is received from + a subscription. + + Translates the text in the message from the specified source language + to the requested target language, then sends a message requesting another + service save the result. + + Args: + event: dictionary containing the PubSub event. + context: a dictionary containing metadata about the event. + + Returns: + None; the output is written to stdout and Stackdriver Logging. + """ if event.get("data"): message_data = base64.b64decode(event["data"]).decode("utf-8") message = json.loads(message_data) @@ -135,9 +178,9 @@ def translate_text(event, context): "filename": filename, "lang": target_lang, } - message_data = json.dumps(message).encode("utf-8") + encoded_message = json.dumps(message).encode("utf-8") topic_path = publisher.topic_path(project_id, topic_name) - future = publisher.publish(topic_path, data=message_data) + future = publisher.publish(topic_path, data=encoded_message) future.result() @@ -145,7 +188,18 @@ def translate_text(event, context): # [START functions_ocr_save] -def save_result(event, context): +def save_result(event: dict, context: dict) -> None: + """ + Cloud Function triggered by PubSub when a message is received from + a subscription. + + Args: + event: dictionary containing the PubSub event. + context: a dictionary containing metadata about the event. + + Returns: + None; the output is written to stdout and Stackdriver Logging. + """ if event.get("data"): message_data = base64.b64decode(event["data"]).decode("utf-8") message = json.loads(message_data) diff --git a/functions/ocr/app/main_test.py b/functions/ocr/app/main_test.py index f4f90112e6e3..fdb9ce2b6871 100644 --- a/functions/ocr/app/main_test.py +++ b/functions/ocr/app/main_test.py @@ -20,6 +20,8 @@ import main +# flake8: noqa + class TestGCFPyOCRSample(): @mock.patch.object(main, 'publisher') diff --git a/functions/ocr/app/noxfile_config.py b/functions/ocr/app/noxfile_config.py index 69086b19c37a..63f3301e7da8 100644 --- a/functions/ocr/app/noxfile_config.py +++ b/functions/ocr/app/noxfile_config.py @@ -44,4 +44,5 @@ "RESULT_TOPIC": "result-topic", "RESULT_BUCKET": "result-bucket", }, + "enforce_type_hints": True, }