Skip to content

Commit d6ec5ad

Browse files
chore: update type hints to standard collections (#410)
This change is analogous to CloudSQL's change: GoogleCloudPlatform/cloud-sql-python-connector#1183
1 parent ea2a524 commit d6ec5ad

16 files changed

+37
-47
lines changed

google/cloud/alloydb/connector/async_connector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import asyncio
1818
import logging
1919
from types import TracebackType
20-
from typing import Any, Dict, Optional, Type, TYPE_CHECKING, Union
20+
from typing import Any, Optional, TYPE_CHECKING, Union
2121

2222
import google.auth
2323
from google.auth.credentials import with_scopes_if_required
@@ -71,7 +71,7 @@ def __init__(
7171
user_agent: Optional[str] = None,
7272
refresh_strategy: str | RefreshStrategy = RefreshStrategy.BACKGROUND,
7373
) -> None:
74-
self._cache: Dict[str, Union[RefreshAheadCache, LazyRefreshCache]] = {}
74+
self._cache: dict[str, Union[RefreshAheadCache, LazyRefreshCache]] = {}
7575
# initialize default params
7676
self._quota_project = quota_project
7777
self._alloydb_api_endpoint = alloydb_api_endpoint
@@ -223,7 +223,7 @@ async def __aenter__(self) -> Any:
223223

224224
async def __aexit__(
225225
self,
226-
exc_type: Optional[Type[BaseException]],
226+
exc_type: Optional[type[BaseException]],
227227
exc_val: Optional[BaseException],
228228
exc_tb: Optional[TracebackType],
229229
) -> None:

google/cloud/alloydb/connector/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import asyncio
1818
import logging
19-
from typing import Dict, List, Optional, Tuple, TYPE_CHECKING
19+
from typing import Optional, TYPE_CHECKING
2020

2121
import aiohttp
2222
from cryptography import x509
@@ -100,7 +100,7 @@ async def _get_metadata(
100100
region: str,
101101
cluster: str,
102102
name: str,
103-
) -> Dict[str, Optional[str]]:
103+
) -> dict[str, Optional[str]]:
104104
"""
105105
Fetch the metadata for a given AlloyDB instance.
106106
@@ -156,7 +156,7 @@ async def _get_client_certificate(
156156
region: str,
157157
cluster: str,
158158
pub_key: str,
159-
) -> Tuple[str, List[str]]:
159+
) -> tuple[str, list[str]]:
160160
"""
161161
Fetch a client certificate for the given AlloyDB cluster.
162162
@@ -172,7 +172,7 @@ async def _get_client_certificate(
172172
pub_key (str): PEM-encoded client public key.
173173
174174
Returns:
175-
Tuple[str, list[str]]: Tuple containing the CA certificate
175+
tuple[str, list[str]]: tuple containing the CA certificate
176176
and certificate chain for the AlloyDB instance.
177177
"""
178178
headers = {

google/cloud/alloydb/connector/connection_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from dataclasses import dataclass
1818
import logging
1919
import ssl
20-
from typing import Dict, List, Optional, TYPE_CHECKING
20+
from typing import Optional, TYPE_CHECKING
2121

2222
from aiofiles.tempfile import TemporaryDirectory
2323

@@ -39,10 +39,10 @@ class ConnectionInfo:
3939
"""Contains all necessary information to connect securely to the
4040
server-side Proxy running on an AlloyDB instance."""
4141

42-
cert_chain: List[str]
42+
cert_chain: list[str]
4343
ca_cert: str
4444
key: rsa.RSAPrivateKey
45-
ip_addrs: Dict[str, Optional[str]]
45+
ip_addrs: dict[str, Optional[str]]
4646
expiration: datetime.datetime
4747
context: Optional[ssl.SSLContext] = None
4848

google/cloud/alloydb/connector/connector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import struct
2222
from threading import Thread
2323
from types import TracebackType
24-
from typing import Any, Dict, Optional, Type, TYPE_CHECKING, Union
24+
from typing import Any, Optional, TYPE_CHECKING, Union
2525

2626
from google.auth import default
2727
from google.auth.credentials import TokenState
@@ -87,7 +87,7 @@ def __init__(
8787
self._loop: asyncio.AbstractEventLoop = asyncio.new_event_loop()
8888
self._thread = Thread(target=self._loop.run_forever, daemon=True)
8989
self._thread.start()
90-
self._cache: Dict[str, Union[RefreshAheadCache, LazyRefreshCache]] = {}
90+
self._cache: dict[str, Union[RefreshAheadCache, LazyRefreshCache]] = {}
9191
# initialize default params
9292
self._quota_project = quota_project
9393
self._alloydb_api_endpoint = alloydb_api_endpoint
@@ -355,7 +355,7 @@ def __enter__(self) -> "Connector":
355355

356356
def __exit__(
357357
self,
358-
exc_type: Optional[Type[BaseException]],
358+
exc_type: Optional[type[BaseException]],
359359
exc_val: Optional[BaseException],
360360
exc_tb: Optional[TracebackType],
361361
) -> None:

google/cloud/alloydb/connector/instance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from datetime import timezone
2121
import logging
2222
import re
23-
from typing import Tuple, TYPE_CHECKING
23+
from typing import TYPE_CHECKING
2424

2525
from google.cloud.alloydb.connector.connection_info import ConnectionInfo
2626
from google.cloud.alloydb.connector.exceptions import RefreshError
@@ -40,7 +40,7 @@
4040
)
4141

4242

43-
def _parse_instance_uri(instance_uri: str) -> Tuple[str, str, str, str]:
43+
def _parse_instance_uri(instance_uri: str) -> tuple[str, str, str, str]:
4444
# should take form "projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>"
4545
if INSTANCE_URI_REGEX.fullmatch(instance_uri) is None:
4646
raise ValueError(
@@ -69,14 +69,14 @@ class RefreshAheadCache:
6969
instance_uri (str): The instance URI of the AlloyDB instance.
7070
ex. projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>
7171
client (AlloyDBClient): Client used to make requests to AlloyDB APIs.
72-
keys (Tuple[rsa.RSAPrivateKey, str]): Private and Public key pair.
72+
keys (tuple[rsa.RSAPrivateKey, str]): Private and Public key pair.
7373
"""
7474

7575
def __init__(
7676
self,
7777
instance_uri: str,
7878
client: AlloyDBClient,
79-
keys: asyncio.Future[Tuple[rsa.RSAPrivateKey, str]],
79+
keys: asyncio.Future[tuple[rsa.RSAPrivateKey, str]],
8080
) -> None:
8181
# validate and parse instance_uri
8282
self._project, self._region, self._cluster, self._name = _parse_instance_uri(

google/cloud/alloydb/connector/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
from __future__ import annotations
1616

17-
from typing import List, Tuple
18-
1917
import aiofiles
2018
from cryptography.hazmat.primitives import serialization
2119
from cryptography.hazmat.primitives.asymmetric import rsa
2220

2321

2422
async def _write_to_file(
25-
dir_path: str, ca_cert: str, cert_chain: List[str], key: rsa.RSAPrivateKey
26-
) -> Tuple[str, str, str]:
23+
dir_path: str, ca_cert: str, cert_chain: list[str], key: rsa.RSAPrivateKey
24+
) -> tuple[str, str, str]:
2725
"""
2826
Helper function to write the server_ca, client certificate and
2927
private key to .pem files in a given directory.
@@ -48,7 +46,7 @@ async def _write_to_file(
4846
return (ca_filename, cert_chain_filename, key_filename)
4947

5048

51-
async def generate_keys() -> Tuple[rsa.RSAPrivateKey, str]:
49+
async def generate_keys() -> tuple[rsa.RSAPrivateKey, str]:
5250
priv_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
5351
pub_key = (
5452
priv_key.public_key()

tests/system/test_asyncpg_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16-
from typing import Any, Tuple
16+
from typing import Any
1717

1818
# [START alloydb_sqlalchemy_connect_async_connector]
1919
import asyncpg
@@ -29,7 +29,7 @@ async def create_sqlalchemy_engine(
2929
password: str,
3030
db: str,
3131
refresh_strategy: str = "background",
32-
) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
32+
) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
3333
"""Creates a connection pool for an AlloyDB instance and returns the pool
3434
and the connector. Callers are responsible for closing the pool and the
3535
connector.

tests/system/test_asyncpg_iam_authn.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from datetime import datetime
1616
import os
17-
from typing import Tuple
1817

1918
# [START alloydb_sqlalchemy_connect_async_connector_iam_authn]
2019
import asyncpg
@@ -26,7 +25,7 @@
2625

2726
async def create_sqlalchemy_engine(
2827
inst_uri: str, user: str, db: str, refresh_strategy: str = "background"
29-
) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
28+
) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
3029
"""Creates a connection pool for an AlloyDB instance and returns the pool
3130
and the connector. Callers are responsible for closing the pool and the
3231
connector.

tests/system/test_asyncpg_psc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import os
16-
from typing import Tuple
1716

1817
import asyncpg
1918
import pytest
@@ -28,7 +27,7 @@ async def create_sqlalchemy_engine(
2827
user: str,
2928
password: str,
3029
db: str,
31-
) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
30+
) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
3231
"""Creates a connection pool for an AlloyDB instance and returns the pool
3332
and the connector. Callers are responsible for closing the pool and the
3433
connector.

tests/system/test_asyncpg_public_ip.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import os
16-
from typing import Tuple
1716

1817
# [START alloydb_sqlalchemy_connect_async_connector_public_ip]
1918
import asyncpg
@@ -29,7 +28,7 @@ async def create_sqlalchemy_engine(
2928
user: str,
3029
password: str,
3130
db: str,
32-
) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
31+
) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]:
3332
"""Creates a connection pool for an AlloyDB instance and returns the pool
3433
and the connector. Callers are responsible for closing the pool and the
3534
connector.

0 commit comments

Comments
 (0)