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

chore: update type hints to standard collections #1183

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions google/cloud/sql/connector/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

import ssl
from typing import Any, TYPE_CHECKING

Expand Down
8 changes: 4 additions & 4 deletions google/cloud/sql/connector/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import asyncio
import datetime
import logging
from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING
from typing import Any, Optional, TYPE_CHECKING

import aiohttp
from cryptography.hazmat.backends import default_backend
Expand Down Expand Up @@ -98,7 +98,7 @@ async def _get_metadata(
project: str,
region: str,
instance: str,
) -> Dict[str, Any]:
) -> dict[str, Any]:
"""Requests metadata from the Cloud SQL Instance
and returns a dictionary containing the IP addresses and certificate
authority of the Cloud SQL Instance.
Expand All @@ -113,7 +113,7 @@ async def _get_metadata(
:type instance: str
:param instance: A string representing the name of the instance.

:rtype: Dict[str: Union[Dict, str]]
:rtype: dict[str: Union[dict, str]]
:returns: Returns a dictionary containing a dictionary of all IP
addresses and their type and a string representing the
certificate authority.
Expand Down Expand Up @@ -161,7 +161,7 @@ async def _get_ephemeral(
instance: str,
pub_key: str,
enable_iam_auth: bool = False,
) -> Tuple[str, datetime.datetime]:
) -> tuple[str, datetime.datetime]:
"""Asynchronously requests an ephemeral certificate from the Cloud SQL Instance.

:type project: str
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/sql/connector/connection_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from dataclasses import dataclass
import logging
import ssl
from typing import Any, Dict, Optional, TYPE_CHECKING
from typing import Any, Optional, TYPE_CHECKING

from aiofiles.tempfile import TemporaryDirectory

Expand All @@ -41,7 +41,7 @@ class ConnectionInfo:
client_cert: str
server_ca_cert: str
private_key: bytes
ip_addrs: Dict[str, Any]
ip_addrs: dict[str, Any]
database_version: str
expiration: datetime.datetime
context: Optional[ssl.SSLContext] = None
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/sql/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import logging
from threading import Thread
from types import TracebackType
from typing import Any, Dict, Optional, Tuple, Type, Union
from typing import Any, Optional, Type, Union

import google.auth
from google.auth.credentials import Credentials
Expand Down Expand Up @@ -133,8 +133,8 @@ def __init__(
)
# initialize dict to store caches, key is a tuple consisting of instance
# connection name string and enable_iam_auth boolean flag
self._cache: Dict[
Tuple[str, bool], Union[RefreshAheadCache, LazyRefreshCache]
self._cache: dict[
tuple[str, bool], Union[RefreshAheadCache, LazyRefreshCache]
] = {}
self._client: Optional[CloudSQLClient] = None

Expand Down
3 changes: 1 addition & 2 deletions google/cloud/sql/connector/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from datetime import timezone
import logging
import re
from typing import Tuple

import aiohttp

Expand All @@ -43,7 +42,7 @@
CONN_NAME_REGEX = re.compile(("([^:]+(:[^:]+)?):([^:]+):([^:]+)"))


def _parse_instance_connection_name(connection_name: str) -> Tuple[str, str, str]:
def _parse_instance_connection_name(connection_name: str) -> tuple[str, str, str]:
if CONN_NAME_REGEX.fullmatch(connection_name) is None:
raise ValueError(
"Arg `instance_connection_string` must have "
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/sql/connector/refresh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import datetime
import logging
import random
from typing import Any, Callable, List
from typing import Any, Callable

import aiohttp
from google.auth.credentials import Credentials
Expand Down Expand Up @@ -77,15 +77,15 @@ async def _is_valid(task: asyncio.Task) -> bool:

def _downscope_credentials(
credentials: Credentials,
scopes: List[str] = ["https://www.googleapis.com/auth/sqlservice.login"],
scopes: list[str] = ["https://www.googleapis.com/auth/sqlservice.login"],
) -> Credentials:
"""Generate a down-scoped credential.

:type credentials: google.auth.credentials.Credentials
:param credentials
Credentials object used to generate down-scoped credentials.

:type scopes: List[str]
:type scopes: list[str]
:param scopes
List of Google scopes to include in down-scoped credentials object.

Expand Down
6 changes: 2 additions & 4 deletions google/cloud/sql/connector/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
limitations under the License.
"""

from typing import Tuple

import aiofiles
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa


async def generate_keys() -> Tuple[bytes, str]:
async def generate_keys() -> tuple[bytes, str]:
"""A helper function to generate the private and public keys.

backend - The value specified is default_backend(). This is because the
Expand Down Expand Up @@ -61,7 +59,7 @@ async def generate_keys() -> Tuple[bytes, str]:

async def write_to_file(
dir_path: str, serverCaCert: str, ephemeralCert: str, priv_key: bytes
) -> Tuple[str, str, str]:
) -> tuple[str, str, str]:
"""
Helper function to write the serverCaCert, ephemeral certificate and
private key to .pem files in a given directory
Expand Down
3 changes: 1 addition & 2 deletions tests/system/test_asyncpg_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import asyncio
import os
from typing import Tuple

import asyncpg
import sqlalchemy
Expand All @@ -31,7 +30,7 @@ async def create_sqlalchemy_engine(
password: str,
db: str,
refresh_strategy: str = "background",
) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]:
) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]:
"""Creates a connection pool for a Cloud SQL instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.
Expand Down
3 changes: 1 addition & 2 deletions tests/system/test_asyncpg_iam_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import asyncio
import os
from typing import Tuple

import asyncpg
import sqlalchemy
Expand All @@ -30,7 +29,7 @@ async def create_sqlalchemy_engine(
user: str,
db: str,
refresh_strategy: str = "background",
) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]:
) -> tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, Connector]:
"""Creates a connection pool for a Cloud SQL instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.
Expand Down
1 change: 1 addition & 0 deletions tests/system/test_ip_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

import os
import uuid

Expand Down
3 changes: 1 addition & 2 deletions tests/system/test_pg8000_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from datetime import datetime
import os
from typing import Tuple

# [START cloud_sql_connector_postgres_pg8000]
import pg8000
Expand All @@ -31,7 +30,7 @@ def create_sqlalchemy_engine(
password: str,
db: str,
refresh_strategy: str = "background",
) -> Tuple[sqlalchemy.engine.Engine, Connector]:
) -> tuple[sqlalchemy.engine.Engine, Connector]:
"""Creates a connection pool for a Cloud SQL instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.
Expand Down
3 changes: 1 addition & 2 deletions tests/system/test_pg8000_iam_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from datetime import datetime
import os
from typing import Tuple

import pg8000
import sqlalchemy
Expand All @@ -29,7 +28,7 @@ def create_sqlalchemy_engine(
user: str,
db: str,
refresh_strategy: str = "background",
) -> Tuple[sqlalchemy.engine.Engine, Connector]:
) -> tuple[sqlalchemy.engine.Engine, Connector]:
"""Creates a connection pool for a Cloud SQL instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.
Expand Down
3 changes: 1 addition & 2 deletions tests/system/test_pymysql_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from datetime import datetime
import os
from typing import Tuple

# [START cloud_sql_connector_mysql_pymysql]
import pymysql
Expand All @@ -31,7 +30,7 @@ def create_sqlalchemy_engine(
password: str,
db: str,
refresh_strategy: str = "background",
) -> Tuple[sqlalchemy.engine.Engine, Connector]:
) -> tuple[sqlalchemy.engine.Engine, Connector]:
"""Creates a connection pool for a Cloud SQL instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.
Expand Down
3 changes: 1 addition & 2 deletions tests/system/test_pymysql_iam_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from datetime import datetime
import os
from typing import Tuple

import pymysql
import sqlalchemy
Expand All @@ -29,7 +28,7 @@ def create_sqlalchemy_engine(
user: str,
db: str,
refresh_strategy: str = "background",
) -> Tuple[sqlalchemy.engine.Engine, Connector]:
) -> tuple[sqlalchemy.engine.Engine, Connector]:
"""Creates a connection pool for a Cloud SQL instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.
Expand Down
3 changes: 1 addition & 2 deletions tests/system/test_pytds_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""

import os
from typing import Tuple

# [START cloud_sql_connector_mysql_pytds]
import pytds
Expand All @@ -30,7 +29,7 @@ def create_sqlalchemy_engine(
password: str,
db: str,
refresh_strategy: str = "background",
) -> Tuple[sqlalchemy.engine.Engine, Connector]:
) -> tuple[sqlalchemy.engine.Engine, Connector]:
"""Creates a connection pool for a Cloud SQL instance and returns the pool
and the connector. Callers are responsible for closing the pool and the
connector.
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import datetime
import json
import ssl
from typing import Any, Callable, Dict, Literal, Optional, Tuple
from typing import Any, Callable, Literal, Optional

from aiofiles.tempfile import TemporaryDirectory
from aiohttp import web
Expand Down Expand Up @@ -113,7 +113,7 @@ def generate_cert(
cert_before: datetime.datetime = datetime.datetime.now(datetime.timezone.utc),
cert_after: datetime.datetime = datetime.datetime.now(datetime.timezone.utc)
+ datetime.timedelta(hours=1),
) -> Tuple[x509.CertificateBuilder, rsa.RSAPrivateKey]:
) -> tuple[x509.CertificateBuilder, rsa.RSAPrivateKey]:
"""
Generate a private key and cert object to be used in testing.
"""
Expand Down Expand Up @@ -221,7 +221,7 @@ def __init__(
region: str = "test-region",
name: str = "test-instance",
db_version: str = "POSTGRES_15",
ip_addrs: Dict = {
ip_addrs: dict = {
"PRIMARY": "127.0.0.1",
"PRIVATE": "10.0.0.1",
},
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

import ssl
from typing import Any

Expand Down
Loading