diff --git a/.flake8 b/.flake8 deleted file mode 100644 index e46e8e07..00000000 --- a/.flake8 +++ /dev/null @@ -1,32 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2023 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# 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. - -[flake8] -ignore = E203, E231, E266, E501, W503, ANN101, ANN102, ANN401 -exclude = - # Exclude generated code. - **/proto/** - **/gapic/** - **/services/** - **/types/** - *_pb2.py - - # Standard linting exemptions. - **/.nox/** - __pycache__, - .git, - *.pyc, - conf.py diff --git a/google/cloud/alloydb/connector/__init__.py b/google/cloud/alloydb/connector/__init__.py index 6110c151..ea3d2e27 100644 --- a/google/cloud/alloydb/connector/__init__.py +++ b/google/cloud/alloydb/connector/__init__.py @@ -11,11 +11,11 @@ # 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 google.cloud.alloydbconnector import __version__ from google.cloud.alloydbconnector import AsyncConnector from google.cloud.alloydbconnector import Connector from google.cloud.alloydbconnector import IPTypes from google.cloud.alloydbconnector import RefreshStrategy +from google.cloud.alloydbconnector import __version__ __all__ = [ "__version__", diff --git a/google/cloud/alloydbconnector/async_connector.py b/google/cloud/alloydbconnector/async_connector.py index 1ba50861..b6de4116 100644 --- a/google/cloud/alloydbconnector/async_connector.py +++ b/google/cloud/alloydbconnector/async_connector.py @@ -17,12 +17,13 @@ import asyncio import logging from types import TracebackType -from typing import Any, Optional, TYPE_CHECKING +from typing import TYPE_CHECKING +from typing import Any +from typing import Optional import google.auth from google.auth.credentials import with_scopes_if_required import google.auth.transport.requests - import google.cloud.alloydbconnector.asyncpg as asyncpg from google.cloud.alloydbconnector.client import AlloyDBClient from google.cloud.alloydbconnector.enums import IPTypes @@ -178,8 +179,7 @@ async def connect( cache = LazyRefreshCache(instance_uri, self._client, self._keys) else: logger.debug( - f"['{instance_uri}']: Refresh strategy is set to background" - " refresh" + f"['{instance_uri}']: Refresh strategy is set to background refresh" ) cache = RefreshAheadCache(instance_uri, self._client, self._keys) self._cache[instance_uri] = cache diff --git a/google/cloud/alloydbconnector/asyncpg.py b/google/cloud/alloydbconnector/asyncpg.py index 2652ee41..8476f92f 100644 --- a/google/cloud/alloydbconnector/asyncpg.py +++ b/google/cloud/alloydbconnector/asyncpg.py @@ -13,7 +13,8 @@ # limitations under the License. import ssl -from typing import Any, TYPE_CHECKING +from typing import TYPE_CHECKING +from typing import Any SERVER_PROXY_PORT = 5433 diff --git a/google/cloud/alloydbconnector/client.py b/google/cloud/alloydbconnector/client.py index 075a2af1..88b455e9 100644 --- a/google/cloud/alloydbconnector/client.py +++ b/google/cloud/alloydbconnector/client.py @@ -16,18 +16,19 @@ import asyncio import logging -from typing import Optional, TYPE_CHECKING +from typing import TYPE_CHECKING +from typing import Optional from cryptography import x509 + from google.api_core.client_options import ClientOptions from google.api_core.gapic_v1.client_info import ClientInfo from google.auth.credentials import TokenState from google.auth.transport import requests import google.cloud.alloydb_v1beta as v1beta -from google.protobuf import duration_pb2 - from google.cloud.alloydbconnector.connection_info import ConnectionInfo from google.cloud.alloydbconnector.version import __version__ as version +from google.protobuf import duration_pb2 if TYPE_CHECKING: from google.auth.credentials import Credentials diff --git a/google/cloud/alloydbconnector/connection_info.py b/google/cloud/alloydbconnector/connection_info.py index 751474ac..f9e49177 100644 --- a/google/cloud/alloydbconnector/connection_info.py +++ b/google/cloud/alloydbconnector/connection_info.py @@ -17,7 +17,8 @@ from dataclasses import dataclass import logging import ssl -from typing import Optional, TYPE_CHECKING +from typing import TYPE_CHECKING +from typing import Optional from aiofiles.tempfile import TemporaryDirectory diff --git a/google/cloud/alloydbconnector/connector.py b/google/cloud/alloydbconnector/connector.py index 061f000e..3a269e91 100644 --- a/google/cloud/alloydbconnector/connector.py +++ b/google/cloud/alloydbconnector/connector.py @@ -24,13 +24,15 @@ import struct from threading import Thread from types import TracebackType -from typing import Any, Callable, Optional, TYPE_CHECKING +from typing import TYPE_CHECKING +from typing import Any +from typing import Callable +from typing import Optional from google.auth import default from google.auth.credentials import TokenState from google.auth.credentials import with_scopes_if_required from google.auth.transport import requests - import google.cloud.alloydb_connectors_v1.proto.resources_pb2 as connectorspb from google.cloud.alloydbconnector.client import AlloyDBClient from google.cloud.alloydbconnector.enums import IPTypes @@ -221,8 +223,7 @@ async def connect_async(self, instance_uri: str, driver: str, **kwargs: Any) -> cache = LazyRefreshCache(instance_uri, self._client, self._keys) else: logger.debug( - f"['{instance_uri}']: Refresh strategy is set to background" - " refresh" + f"['{instance_uri}']: Refresh strategy is set to background refresh" ) cache = RefreshAheadCache(instance_uri, self._client, self._keys) self._cache[instance_uri] = cache diff --git a/google/cloud/alloydbconnector/instance.py b/google/cloud/alloydbconnector/instance.py index 9c3051e2..4cdb84cd 100644 --- a/google/cloud/alloydbconnector/instance.py +++ b/google/cloud/alloydbconnector/instance.py @@ -121,8 +121,7 @@ async def _perform_refresh(self) -> ConnectionInfo: self._keys, ) logger.debug( - f"['{self._instance_uri}']: Connection info refresh operation" - " complete" + f"['{self._instance_uri}']: Connection info refresh operation complete" ) logger.debug( f"['{self._instance_uri}']: Current certificate expiration = " diff --git a/google/cloud/alloydbconnector/lazy.py b/google/cloud/alloydbconnector/lazy.py index d86ea522..b05d5140 100644 --- a/google/cloud/alloydbconnector/lazy.py +++ b/google/cloud/alloydbconnector/lazy.py @@ -91,8 +91,7 @@ async def connect_info(self) -> ConnectionInfo: ) return self._cached logger.debug( - f"['{self._instance_uri}']: Connection info " - "refresh operation started" + f"['{self._instance_uri}']: Connection info refresh operation started" ) try: conn_info = await self._client.get_connection_info( diff --git a/google/cloud/alloydbconnector/pg8000.py b/google/cloud/alloydbconnector/pg8000.py index f17766c2..7cda9c92 100644 --- a/google/cloud/alloydbconnector/pg8000.py +++ b/google/cloud/alloydbconnector/pg8000.py @@ -11,7 +11,8 @@ # 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 typing import Any, TYPE_CHECKING +from typing import TYPE_CHECKING +from typing import Any if TYPE_CHECKING: import ssl diff --git a/google/cloud/alloydbconnector/psycopg.py b/google/cloud/alloydbconnector/psycopg.py index 89bac5fb..8e91c076 100644 --- a/google/cloud/alloydbconnector/psycopg.py +++ b/google/cloud/alloydbconnector/psycopg.py @@ -18,7 +18,8 @@ import ssl import tempfile import threading -from typing import Any, TYPE_CHECKING +from typing import TYPE_CHECKING +from typing import Any if TYPE_CHECKING: import psycopg diff --git a/noxfile.py b/noxfile.py index d9529868..5eae8d07 100644 --- a/noxfile.py +++ b/noxfile.py @@ -18,8 +18,7 @@ import nox -BLACK_VERSION = "black==23.12.1" -ISORT_VERSION = "isort==5.13.2" +RUFF_VERSION = "ruff==0.11.2" LINT_PATHS = ["google", "tests", "noxfile.py"] SYSTEM_TEST_PYTHON_VERSIONS = ["3.10", "3.11", "3.12", "3.13", "3.14"] @@ -36,32 +35,22 @@ def lint(session): session.install("-r", "requirements-test.txt") session.install("-r", "requirements.txt") session.install( - "flake8", - "flake8-annotations", + RUFF_VERSION, "mypy", - BLACK_VERSION, - ISORT_VERSION, "build", "twine", ) session.run( - "isort", - "--fss", - "--check-only", - "--diff", - "--profile=google", - *LINT_PATHS, - ) - session.run( - "black", + "ruff", + "format", "--check", "--diff", *LINT_PATHS, ) session.run( - "flake8", - "google", - "tests", + "ruff", + "check", + *LINT_PATHS, ) session.run( "mypy", @@ -77,35 +66,18 @@ def lint(session): @nox.session -def blacken(session): - """Run black. - - Format code to uniform standard. - """ - session.install(BLACK_VERSION) - session.run( - "black", - *LINT_PATHS, - ) - - -@nox.session() def format(session): - """ - Run isort to sort imports. Then run black - to format code to uniform standard. - """ - session.install(BLACK_VERSION, ISORT_VERSION) - # Use the --fss option to sort imports using strict alphabetical order. - # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sectionss + """Format code with ruff.""" + session.install(RUFF_VERSION) session.run( - "isort", - "--fss", - "--profile=google", + "ruff", + "check", + "--fix", *LINT_PATHS, ) session.run( - "black", + "ruff", + "format", *LINT_PATHS, ) diff --git a/pyproject.toml b/pyproject.toml index b1bb87d5..89387b12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,6 +73,39 @@ version = { attr = "google.cloud.alloydbconnector.version.__version__" } # benchmarks, etc. include = ["google*"] +[tool.ruff] +line-length = 88 +target-version = "py310" +exclude = [ + "google/api/*", + "google/cloud/alloydb_connectors_v1/*", +] + +[tool.ruff.lint] +select = [ + "E", # pycodestyle errors + "F", # pyflakes + "W", # pycodestyle warnings + "I", # isort + "ANN", # flake8-annotations +] +ignore = [ + "E203", + "E231", + "E266", + "E501", + "ANN401", +] + +[tool.ruff.lint.per-file-ignores] +"noxfile.py" = ["ANN"] +"tests/*" = ["ANN"] + +[tool.ruff.lint.isort] +force-single-line = true +force-sort-within-sections = true +known-first-party = ["google"] + [tool.mypy] python_version = "3.10" namespace_packages = true diff --git a/tests/system/test_native_asyncpg_direct_connection.py b/tests/system/test_native_asyncpg_direct_connection.py index 505d8024..38e7bf00 100644 --- a/tests/system/test_native_asyncpg_direct_connection.py +++ b/tests/system/test_native_asyncpg_direct_connection.py @@ -18,6 +18,7 @@ # [START alloydb_native_asyncpg_connect_iam_authn_direct] import asyncpg + import google.auth from google.auth.transport.requests import Request diff --git a/tests/system/test_psycopg2_direct_connection.py b/tests/system/test_psycopg2_direct_connection.py index dac3e319..1646b86d 100644 --- a/tests/system/test_psycopg2_direct_connection.py +++ b/tests/system/test_psycopg2_direct_connection.py @@ -16,12 +16,13 @@ from datetime import datetime import os +import sqlalchemy +from sqlalchemy import event + # [START alloydb_psycopg2_connect_iam_authn_direct] import google.auth from google.auth.credentials import Credentials from google.auth.transport.requests import Request -import sqlalchemy -from sqlalchemy import event # [END alloydb_psycopg2_connect_iam_authn_direct] diff --git a/tests/system/test_sqlalchemy_asyncpg_direct_connection.py b/tests/system/test_sqlalchemy_asyncpg_direct_connection.py index 59fd3cba..d69a2f0d 100644 --- a/tests/system/test_sqlalchemy_asyncpg_direct_connection.py +++ b/tests/system/test_sqlalchemy_asyncpg_direct_connection.py @@ -16,13 +16,14 @@ from datetime import datetime import os +import sqlalchemy +from sqlalchemy import event +from sqlalchemy.ext.asyncio import create_async_engine + # [START alloydb_sqlalchemy_asyncpg_connect_iam_authn_direct] import google.auth from google.auth.credentials import Credentials from google.auth.transport.requests import Request -import sqlalchemy -from sqlalchemy import event -from sqlalchemy.ext.asyncio import create_async_engine # [END alloydb_sqlalchemy_asyncpg_connect_iam_authn_direct] diff --git a/tests/unit/mocks.py b/tests/unit/mocks.py index 5bf6df57..5e254b0c 100644 --- a/tests/unit/mocks.py +++ b/tests/unit/mocks.py @@ -21,18 +21,22 @@ import json import ssl import struct -from typing import Any, Callable, Literal, Optional, Sequence +from typing import Any +from typing import Callable +from typing import Literal +from typing import Optional +from typing import Sequence from cryptography import x509 from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.x509.oid import NameOID -from google.auth.credentials import _helpers + from google.auth.credentials import Scoped from google.auth.credentials import TokenState +from google.auth.credentials import _helpers from google.auth.transport import requests - from google.cloud import alloydb_v1beta import google.cloud.alloydb_connectors_v1.proto.resources_pb2 as connectorspb from google.cloud.alloydbconnector.connection_info import ConnectionInfo diff --git a/tests/unit/test_async_connector.py b/tests/unit/test_async_connector.py index 92a345b9..546b7a59 100644 --- a/tests/unit/test_async_connector.py +++ b/tests/unit/test_async_connector.py @@ -13,10 +13,9 @@ # limitations under the License. import asyncio -from typing import Any, Union +from typing import Any +from typing import Union -from google.api_core.exceptions import RetryError -from google.api_core.retry.retry_unary_async import AsyncRetry from mock import patch from mocks import FakeAlloyDBClient from mocks import FakeConnectionInfo @@ -24,6 +23,8 @@ from mocks import FakeCredentialsRequiresScopes import pytest +from google.api_core.exceptions import RetryError +from google.api_core.retry.retry_unary_async import AsyncRetry from google.cloud.alloydbconnector import AsyncConnector from google.cloud.alloydbconnector import IPTypes from google.cloud.alloydbconnector.client import AlloyDBClient diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 2007b43e..208ca029 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -14,12 +14,12 @@ from typing import Optional -import google.cloud.alloydb_v1beta as v1beta from mocks import FakeAlloyDBAdminAsyncClient from mocks import FakeAlloyDBAdminClient from mocks import FakeCredentials import pytest +import google.cloud.alloydb_v1beta as v1beta from google.cloud.alloydbconnector.client import AlloyDBClient from google.cloud.alloydbconnector.utils import generate_keys from google.cloud.alloydbconnector.version import __version__ as version diff --git a/tests/unit/test_connection_info.py b/tests/unit/test_connection_info.py index 1607e331..86bfed71 100644 --- a/tests/unit/test_connection_info.py +++ b/tests/unit/test_connection_info.py @@ -105,7 +105,9 @@ async def test_ConnectionInfo_get_preferred_ip(ip_type: IPTypes, expected: str) assert ip_address == expected -async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError_when_ip_type_not_exist() -> None: +async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError_when_ip_type_not_exist() -> ( + None +): """Test that ConnectionInfo.get_preferred_ip throws IPTypeNotFoundError""" conn_info = ConnectionInfo( ["cert"], @@ -119,7 +121,9 @@ async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError_when_ip_type_ conn_info.get_preferred_ip(ip_type=IPTypes.PUBLIC) -async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError_when_empty_value() -> None: +async def test_ConnectionInfo_get_preferred_ip_IPTypeNotFoundError_when_empty_value() -> ( + None +): """Test that ConnectionInfo.get_preferred_ip throws IPTypeNotFoundError""" conn_info = ConnectionInfo( ["cert"], diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py index 5729be1e..c5d366eb 100644 --- a/tests/unit/test_connector.py +++ b/tests/unit/test_connector.py @@ -16,8 +16,6 @@ from threading import Thread from typing import Union -from google.api_core.exceptions import RetryError -from google.api_core.retry.retry_unary import Retry from mock import patch from mocks import FakeAlloyDBClient from mocks import FakeCredentials @@ -25,6 +23,8 @@ from mocks import write_static_info import pytest +from google.api_core.exceptions import RetryError +from google.api_core.retry.retry_unary import Retry from google.cloud.alloydbconnector import Connector from google.cloud.alloydbconnector import IPTypes from google.cloud.alloydbconnector.client import AlloyDBClient diff --git a/tests/unit/test_instance.py b/tests/unit/test_instance.py index b643d836..a1ebae84 100644 --- a/tests/unit/test_instance.py +++ b/tests/unit/test_instance.py @@ -21,8 +21,8 @@ from google.cloud.alloydbconnector.connection_info import ConnectionInfo from google.cloud.alloydbconnector.exceptions import RefreshError -from google.cloud.alloydbconnector.instance import _parse_instance_uri from google.cloud.alloydbconnector.instance import RefreshAheadCache +from google.cloud.alloydbconnector.instance import _parse_instance_uri from google.cloud.alloydbconnector.refresh_utils import _is_valid from google.cloud.alloydbconnector.utils import generate_keys