Skip to content
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

fixit: Add type-hints to Python sample at functions/ocr/app #9990

Merged
merged 23 commits into from
May 27, 2023

Conversation

m-strzelczyk
Copy link
Contributor

Description

Fixes b/280879772

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

@product-auto-label product-auto-label bot added samples Issues that are directly related to samples. api: cloudfunctions Issues related to the Cloud Run functions API. labels May 17, 2023
@m-strzelczyk m-strzelczyk marked this pull request as ready for review May 17, 2023 14:45
@m-strzelczyk m-strzelczyk requested review from a team as code owners May 17, 2023 14:45
@kweinmeister
Copy link
Contributor

LGTM (after resolving conflicts). Also there are extra blank lines, which are up to you whether to fix before merging.

@@ -75,7 +89,18 @@ def detect_text(bucket, filename):


# [START message_validatation_helper]
def validate_message(message, param):
def validate_message(message: dict, param: str) -> Any:
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to confirm, we don't know the type of message['param']?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With this definition, I can't really make any assumptions about message object and what's inside it, so I'm afraid not. Unless we put better limitations and say message: dict[str, str] or something like that - but it's not really obvious from the context that this is the case IMO.

#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an 'AS IS' BASIS,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the linter be changing the license quotes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shouldn't. Thanks for noticing.

def detect_text(bucket, filename):
def detect_text(bucket: str, filename: str) -> None:
"""
Extract the text from an image uploaded to Cloud Storage, then
Copy link
Contributor

Choose a reason for hiding this comment

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

A one line summary followed by a blank line and then more details is preferred. But it's not required.

@engelke engelke added the automerge Merge the pull request once unit tests and other checks pass. label May 18, 2023
@engelke engelke self-assigned this May 18, 2023
@gcf-merge-on-green
Copy link
Contributor

Merge-on-green attempted to merge your PR for 6 hours, but it was not mergeable because either one of your required status checks failed, one of your required reviews was not approved, or there is a do not merge label. Learn more about your required status checks here: https://help.github.com/en/github/administering-a-repository/enabling-required-status-checks. You can remove and reapply the label to re-run the bot.

@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label May 18, 2023
m-strzelczyk and others added 8 commits May 19, 2023 17:03
Co-authored-by: Charles Engelke <engelke@google.com>
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
…ases (#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 #<ISSUE-NUMBER>

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
Co-authored-by: Charles Engelke <engelke@google.com>
* chore: update docstrings for Cloud Run samples

* lint

* Update render.py
* 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
@m-strzelczyk m-strzelczyk requested a review from a team as a code owner May 19, 2023 15:03
@m-strzelczyk m-strzelczyk requested review from a team as code owners May 19, 2023 15:03
@snippet-bot
Copy link

snippet-bot bot commented May 19, 2023

No region tags are edited in this PR.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@engelke engelke assigned m-strzelczyk and unassigned engelke May 23, 2023
Copy link
Contributor

@engelke engelke left a comment

Choose a reason for hiding this comment

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

I think those changes will make the failing test pass.

@@ -16,6 +16,7 @@
import base64
import json
import os
from typing import TypedDict, TypeVar
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to from typing import Any

@@ -74,9 +89,22 @@ def detect_text(bucket, filename):

# [END functions_ocr_detect]

T = TypeVar('T')
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove.


# [START message_validatation_helper]
def validate_message(message, param):
def validate_message(message: TypedDict[str, T], param: str) -> T:
Copy link
Contributor

Choose a reason for hiding this comment

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

Change to def validate_message(message: dict[str, Any], param: str) -> Any:

@m-strzelczyk
Copy link
Contributor Author

I think those changes will make the failing test pass.

I tried the Any type before. Linter doesn't like it: https://source.cloud.google.com/results/invocations/b6ba4226-f2bc-4547-99bd-d2cc3f9f4c4c/targets/cloud-devrel%2Fpython-docs-samples%2Flint%2Fpresubmit/log

However, I think I found a working typing definition now. All that's left is this CLA check - I think we should ignore it? The commit that triggers it came from a merge with origin/main, so it is actually already a part of the repo.

@engelke engelke added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 25, 2023
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 25, 2023
@m-strzelczyk m-strzelczyk merged commit 8b4947d into main May 27, 2023
@m-strzelczyk m-strzelczyk deleted the strzelczyk/fixit/280879772 branch May 27, 2023 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants