Skip to content

Commit c5c3869

Browse files
authored
refactor: update typehints in workflows/cloud-client (GoogleCloudPlatform#9967)
* refactor: update typehints in workflows/cloud-client * update return type for test * add noxconfig * lint fix
1 parent 6c7ba14 commit c5c3869

3 files changed

Lines changed: 79 additions & 41 deletions

File tree

workflows/cloud-client/main.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
15+
# [START workflows_api_quickstart]
16+
import time
17+
18+
from google.cloud import workflows_v1
19+
from google.cloud.workflows import executions_v1
20+
from google.cloud.workflows.executions_v1 import Execution
21+
from google.cloud.workflows.executions_v1.types import executions
1622

1723

1824
def execute_workflow(
19-
project, location="us-central1", workflow="myFirstWorkflow"
20-
):
21-
"""Execute a workflow and print the execution results."""
22-
# [START workflows_api_quickstart]
23-
import time
25+
project: str, location: str = "us-central1", workflow: str = "myFirstWorkflow"
26+
) -> Execution:
27+
"""Execute a workflow and print the execution results.
2428
25-
from google.cloud import workflows_v1
26-
from google.cloud.workflows import executions_v1
27-
from google.cloud.workflows.executions_v1.types import executions
29+
A workflow consists of a series of steps described using the Workflows syntax, and can be written in either YAML or JSON.
2830
29-
# TODO(developer): Uncomment these lines and replace with your values.
30-
# project = 'my-project-id'
31-
# location = 'us-central1'
32-
# workflow = 'myFirstWorkflow'
31+
Args:
32+
project: The Google Cloud project id which contains the workflow to execute.
33+
location: The location for the workflow
34+
workflow: The ID of the workflow to execute.
3335
34-
if not project:
35-
raise Exception('GOOGLE_CLOUD_PROJECT env var is required.')
36+
Returns:
37+
The execution response.
38+
"""
3639

3740
# Set up API clients.
3841
execution_client = executions_v1.ExecutionsClient()
@@ -48,25 +51,21 @@ def execute_workflow(
4851
# Wait for execution to finish, then print results.
4952
execution_finished = False
5053
backoff_delay = 1 # Start wait with delay of 1 second
51-
print('Poll every second for result...')
52-
while (not execution_finished):
53-
execution = execution_client.get_execution(
54-
request={"name": response.name})
54+
print("Poll every second for result...")
55+
while not execution_finished:
56+
execution = execution_client.get_execution(request={"name": response.name})
5557
execution_finished = execution.state != executions.Execution.State.ACTIVE
5658

5759
# If we haven't seen the result yet, wait a second.
5860
if not execution_finished:
59-
print('- Waiting for results...')
61+
print("- Waiting for results...")
6062
time.sleep(backoff_delay)
6163
# Double the delay to provide exponential backoff.
6264
backoff_delay *= 2
6365
else:
64-
print(f'Execution finished with state: {execution.state.name}')
65-
print(f'Execution results: {execution.result}')
66+
print(f"Execution finished with state: {execution.state.name}")
67+
print(f"Execution results: {execution.result}")
6668
return execution
67-
# [END workflows_api_quickstart]
6869

6970

70-
if __name__ == "__main__":
71-
project = os.environ.get('GOOGLE_CLOUD_PROJECT')
72-
execute_workflow(project=project)
71+
# [END workflows_api_quickstart]

workflows/cloud-client/main_test.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,33 @@
2424
WORKFLOW_ID = "myFirstWorkflow"
2525

2626

27-
def test_workflow_execution():
27+
def test_workflow_execution() -> None:
2828
assert PROJECT != ""
2929

3030
if not workflow_exists():
3131
workflow_file = open("myFirstWorkflow.workflows.yaml").read()
3232

3333
workflows_client = workflows_v1.WorkflowsClient()
34-
workflows_client.create_workflow(request={
35-
# Manually construct the location
36-
# https://github.com/googleapis/python-workflows/issues/21
37-
"parent": f'projects/{PROJECT}/locations/{LOCATION}',
38-
"workflow_id": WORKFLOW_ID,
39-
"workflow": {
40-
"name": WORKFLOW_ID,
41-
"source_contents": workflow_file
34+
workflows_client.create_workflow(
35+
request={
36+
# Manually construct the location
37+
# https://github.com/googleapis/python-workflows/issues/21
38+
"parent": f"projects/{PROJECT}/locations/{LOCATION}",
39+
"workflow_id": WORKFLOW_ID,
40+
"workflow": {"name": WORKFLOW_ID, "source_contents": workflow_file},
4241
}
43-
})
42+
)
4443

4544
result = main.execute_workflow(PROJECT)
4645
assert result.state == executions.Execution.State.SUCCEEDED
4746
assert len(result.result) > 0
4847

4948

50-
def workflow_exists():
51-
"""Returns True if the workflow exists in this project
52-
"""
49+
def workflow_exists() -> bool:
50+
"""Returns True if the workflow exists in this project"""
5351
try:
5452
workflows_client = workflows_v1.WorkflowsClient()
55-
workflow_name = workflows_client.workflow_path(
56-
PROJECT, LOCATION, WORKFLOW_ID)
53+
workflow_name = workflows_client.workflow_path(PROJECT, LOCATION, WORKFLOW_ID)
5754
workflows_client.get_workflow(request={"name": workflow_name})
5855
return True
5956
except Exception as e:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
35+
# If you need to use a specific version of pip,
36+
# change pip_version_override to the string representation
37+
# of the version number, for example, "20.2.4"
38+
"pip_version_override": None,
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
"envs": {},
42+
}

0 commit comments

Comments
 (0)