[go: up one dir, main page]

Skip to content

Commit

Permalink
chore: update type hints to comply with PEP-0585 (#9974)
Browse files Browse the repository at this point in the history
## Description

Updates the type hints to comply with [PEP-0585](https://peps.python.org/pep-0585).

The script covers most cases, but a handful still required manual intervention to normalize them.

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
  • Loading branch information
davidcavazos committed May 17, 2023
1 parent 6a36fe3 commit 161448f
Show file tree
Hide file tree
Showing 167 changed files with 1,023 additions and 441 deletions.
2 changes: 2 additions & 0 deletions appengine/flexible/storage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

# [START gae_flex_storage_app]
from __future__ import annotations

import logging
import os

Expand Down
5 changes: 3 additions & 2 deletions appengine/flexible_python37_and_earlier/storage/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# limitations under the License.

# [START gae_flex_storage_app]
from __future__ import annotations

import logging
import os
from typing import Union

from flask import Flask, request
from google.cloud import storage
Expand Down Expand Up @@ -68,7 +69,7 @@ def upload() -> str:


@app.errorhandler(500)
def server_error(e: Union[Exception, int]) -> str:
def server_error(e: Exception | int) -> str:
logging.exception('An error occurred during a request.')
return """
An internal error occurred: <pre>{}</pre>
Expand Down
4 changes: 3 additions & 1 deletion batch/list/list_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.

# [START batch_list_jobs]
from typing import Iterable
from __future__ import annotations

from collections.abc import Iterable

from google.cloud import batch_v1

Expand Down
4 changes: 3 additions & 1 deletion batch/list/list_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
# limitations under the License.

# [START batch_list_tasks]
from typing import Iterable
from __future__ import annotations

from collections.abc import Iterable

from google.cloud import batch_v1

Expand Down
2 changes: 2 additions & 0 deletions batch/logs/read_job_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


# [START batch_job_logs]
from __future__ import annotations

from typing import NoReturn

from google.cloud import batch_v1
Expand Down
4 changes: 3 additions & 1 deletion batch/tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations

from collections.abc import Callable
import time
from typing import Callable
import uuid

from flaky import flaky
Expand Down
7 changes: 4 additions & 3 deletions bigquery/remote-function/translate/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# limitations under the License.

# [START bigquery_remote_function_translation]
from typing import List
from __future__ import annotations


import flask
import functions_framework
Expand Down Expand Up @@ -89,8 +90,8 @@ def extract_project_from_caller(job: str) -> str:


def translate_text(
calls: List[str], project: str, target_language_code: str
) -> List[str]:
calls: list[str], project: str, target_language_code: str
) -> list[str]:
"""Translates the input text to specified language using Translation API.
Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import os
from typing import Dict
import uuid

import pytest
Expand All @@ -40,7 +41,7 @@


@pytest.fixture(name="conn_vars")
def setup() -> Dict[str, str]:
def setup() -> dict[str, str]:
try:
conn_vars = {}
conn_vars["db_user"] = os.environ["MYSQL_USER"]
Expand All @@ -61,7 +62,7 @@ def setup() -> Dict[str, str]:

def test_init_tcp_connection_engine(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

init_tcp_connection_engine(
db_user=conn_vars["db_user"],
Expand All @@ -76,7 +77,7 @@ def test_init_tcp_connection_engine(

def test_init_unix_connection_engine(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

init_unix_connection_engine(
db_user=conn_vars["db_user"],
Expand All @@ -92,7 +93,7 @@ def test_init_unix_connection_engine(

def test_init_db(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

table_name = f"votes_{uuid.uuid4().hex}"

Expand Down
5 changes: 3 additions & 2 deletions cloud-sql/mysql/sqlalchemy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import datetime
import logging
import os
from typing import Dict

from flask import Flask, render_template, request, Response

Expand Down Expand Up @@ -92,7 +93,7 @@ def cast_vote() -> Response:


# get_index_context gets data required for rendering HTML application
def get_index_context(db: sqlalchemy.engine.base.Engine) -> Dict:
def get_index_context(db: sqlalchemy.engine.base.Engine) -> dict:
votes = []

with db.connect() as conn:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import os
from typing import Dict
import uuid

import pytest
Expand All @@ -27,7 +27,7 @@


@pytest.fixture(name="conn_vars")
def setup() -> Dict[str, str]:
def setup() -> dict[str, str]:
try:
conn_vars = {}
conn_vars["db_user"] = os.environ["POSTGRES_USER"]
Expand All @@ -48,7 +48,7 @@ def setup() -> Dict[str, str]:

def test_init_tcp_connection_engine(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

init_tcp_connection_engine(
db_user=conn_vars["db_user"],
Expand All @@ -63,7 +63,7 @@ def test_init_tcp_connection_engine(

def test_init_unix_connection_engine(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

init_unix_connection_engine(
db_user=conn_vars["db_user"],
Expand All @@ -79,7 +79,7 @@ def test_init_unix_connection_engine(

def test_init_db(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

table_name = f"votes_{uuid.uuid4().hex}"

Expand Down
5 changes: 3 additions & 2 deletions cloud-sql/postgres/sqlalchemy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import datetime
import logging
import os
from typing import Dict

from flask import Flask, render_template, request, Response

Expand Down Expand Up @@ -92,7 +93,7 @@ def cast_vote() -> Response:


# get_index_context gets data required for rendering HTML application
def get_index_context(db: sqlalchemy.engine.base.Engine) -> Dict:
def get_index_context(db: sqlalchemy.engine.base.Engine) -> dict:
votes = []

with db.connect() as conn:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import os
from typing import Dict
import uuid

import pytest
Expand All @@ -25,7 +26,7 @@


@pytest.fixture(name="conn_vars")
def setup() -> Dict[str, str]:
def setup() -> dict[str, str]:
try:
conn_vars = {}
conn_vars["db_user"] = os.environ["SQLSERVER_USER"]
Expand All @@ -45,7 +46,7 @@ def setup() -> Dict[str, str]:

def test_init_tcp_connection_engine(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

init_tcp_connection_engine(
db_user=conn_vars["db_user"],
Expand All @@ -60,7 +61,7 @@ def test_init_tcp_connection_engine(

def test_init_db(
capsys: pytest.CaptureFixture,
conn_vars: Dict[str, str]) -> None:
conn_vars: dict[str, str]) -> None:

table_name = f"votes_{uuid.uuid4().hex}"

Expand Down
5 changes: 3 additions & 2 deletions cloud-sql/sql-server/sqlalchemy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import datetime
import logging
import os
from typing import Dict

from flask import Flask, render_template, request, Response
import sqlalchemy
Expand Down Expand Up @@ -91,7 +92,7 @@ def cast_vote() -> Response:
return save_vote(db, team)


def get_index_context(db: sqlalchemy.engine.base.Engine) -> Dict:
def get_index_context(db: sqlalchemy.engine.base.Engine) -> dict:
votes = []
with db.connect() as conn:
# Execute the query and fetch all results
Expand Down
5 changes: 3 additions & 2 deletions composer/cicd_sample/utils/add_dags_to_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
# limitations under the License.

# [START composer_cicd_add_dags_to_composer_utility]
from __future__ import annotations

import argparse
import glob
import os
from shutil import copytree, ignore_patterns
import tempfile
from typing import List, Tuple

# Imports the Google Cloud client library
from google.cloud import storage


def _create_dags_list(dags_directory: str) -> Tuple[str, List[str]]:
def _create_dags_list(dags_directory: str) -> tuple[str, list[str]]:
temp_dir = tempfile.mkdtemp()

# ignore non-DAG Python files
Expand Down
2 changes: 2 additions & 0 deletions composer/rest/composer2/composer2_airflow_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

# [START composer_2_trigger_dag]
# [START composer_2_trigger_dag_for_import]
from __future__ import annotations

from typing import Any

import google.auth
Expand Down
16 changes: 9 additions & 7 deletions composer/tools/composer_dags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
# limitations under the License.
"""Standalone script to pause/unpause all the dags in the specific environment."""

from __future__ import annotations

import argparse
import json
import logging
import re
import subprocess
import sys
import typing
from typing import Any

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(message)s")
logger = logging.getLogger(__name__)
Expand All @@ -36,7 +38,7 @@ class DAG:
@staticmethod
def get_list_of_dags(project_name: str, environment: str, location: str,
sdk_endpoint: str,
airflow_version: typing.Tuple[int]) -> typing.List[str]:
airflow_version: tuple[int]) -> list[str]:
"""Retrieves the list of dags for particular project."""
sub_command = ("list_dags" if airflow_version < (2, 0, 0) else "dags list")
command = (
Expand All @@ -60,7 +62,7 @@ def _run_shell_command_locally_once(
command: str,
command_input: str = None,
log_command: bool = True,
) -> typing.Tuple[int, str]:
) -> tuple[int, str]:
"""Executes shell command and returns its output."""

p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
Expand All @@ -78,7 +80,7 @@ def _run_shell_command_locally_once(
@staticmethod
def pause_dag(project_name: str, environment: str, location: str,
sdk_endpoint: str, dag_id: str,
airflow_version: typing.List[int]) -> str:
airflow_version: list[int]) -> str:
"""Pause specific DAG in the given environment."""
sub_command = ("pause" if airflow_version < (2, 0, 0) else "dags pause")
command = (
Expand All @@ -97,7 +99,7 @@ def pause_dag(project_name: str, environment: str, location: str,
@staticmethod
def unpause_dag(project_name: str, environment: str, location: str,
sdk_endpoint: str, dag_id: str,
airflow_version: typing.List[int]) -> str:
airflow_version: list[int]) -> str:
"""UnPause specific DAG in the given environment."""
sub_command = ("unpause" if airflow_version < (2, 0, 0) else "dags unpause")
command = (
Expand All @@ -115,7 +117,7 @@ def unpause_dag(project_name: str, environment: str, location: str,

@staticmethod
def describe_environment(project_name: str, environment: str, location: str,
sdk_endpoint: str) -> typing.Any:
sdk_endpoint: str) -> Any:
"""Returns the given environment json object to parse necessary details."""
logger.info("*** Fetching details of the environment: %s...", environment)
command = (
Expand Down Expand Up @@ -175,7 +177,7 @@ def main(project_name: str,
return 0


def parse_arguments() -> typing.Dict[typing.Any, typing.Any]:
def parse_arguments() -> dict[Any, Any]:
"""Parses command line parameters."""
argument_parser = argparse.ArgumentParser(
usage="Script to Pause/UnPause DAGs in Cloud Composer Environment \n")
Expand Down

0 comments on commit 161448f

Please sign in to comment.