diff --git a/mypy.ini b/mypy.ini index 19b239414608c..066436033c4a1 100644 --- a/mypy.ini +++ b/mypy.ini @@ -42,6 +42,9 @@ ignore_missing_imports = True [mypy-google.oauth2,google.oauth2.*] ignore_missing_imports = True +[mypy-google_auth_oauthlib,google_auth_oauthlib.*] +ignore_missing_imports = True + [mypy-google.protobuf,google.protobuf.*] ignore_missing_imports = True diff --git a/packages/bigquery-magics/bigquery_magics/_versions_helpers.py b/packages/bigquery-magics/bigquery_magics/_versions_helpers.py index 192011a5b6339..4c7d5337e928f 100644 --- a/packages/bigquery-magics/bigquery_magics/_versions_helpers.py +++ b/packages/bigquery-magics/bigquery_magics/_versions_helpers.py @@ -16,8 +16,8 @@ from typing import Any -from google.cloud.bigquery import exceptions import packaging.version +from google.cloud.bigquery import exceptions _MIN_BQ_STORAGE_VERSION = packaging.version.Version("2.0.0") diff --git a/packages/bigquery-magics/bigquery_magics/bigquery.py b/packages/bigquery-magics/bigquery_magics/bigquery.py index d6fb565e4ea23..cd8dbb3eb3e5f 100644 --- a/packages/bigquery-magics/bigquery_magics/bigquery.py +++ b/packages/bigquery-magics/bigquery_magics/bigquery.py @@ -106,33 +106,33 @@ from __future__ import print_function import ast -from concurrent import futures import copy import json import re import sys import threading import time -from typing import Any, List, Tuple import warnings +from concurrent import futures +from typing import Any, List, Tuple import IPython # type: ignore -from IPython.core import magic_arguments # type: ignore -from IPython.core.getipython import get_ipython +import pandas from google.api_core.exceptions import NotFound from google.cloud import bigquery from google.cloud.bigquery import exceptions from google.cloud.bigquery.dataset import DatasetReference from google.cloud.bigquery.dbapi import _helpers from google.cloud.bigquery.job import QueryJobConfig -import pandas +from IPython.core import magic_arguments # type: ignore +from IPython.core.getipython import get_ipython -from bigquery_magics import core -from bigquery_magics import line_arg_parser as lap import bigquery_magics._versions_helpers import bigquery_magics.config import bigquery_magics.graph_server as graph_server import bigquery_magics.pyformat +from bigquery_magics import core +from bigquery_magics import line_arg_parser as lap try: from google.cloud import bigquery_storage # type: ignore @@ -471,8 +471,9 @@ def _parse_magic_args(line: str) -> Tuple[List[Any], Any]: except lap.ParseError as exc: raise ValueError( - "Unrecognized input, are option values correct? " - "Error details: {}".format(exc.args[0]) + "Unrecognized input, are option values correct? Error details: {}".format( + exc.args[0] + ) ) from exc params = [] diff --git a/packages/bigquery-magics/bigquery_magics/config.py b/packages/bigquery-magics/bigquery_magics/config.py index 2b5719ca31633..4596ddf61ccae 100644 --- a/packages/bigquery-magics/bigquery_magics/config.py +++ b/packages/bigquery-magics/bigquery_magics/config.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import warnings from dataclasses import dataclass from typing import Optional -import warnings import google.api_core.client_options as client_options import google.cloud.bigquery as bigquery diff --git a/packages/bigquery-magics/bigquery_magics/core.py b/packages/bigquery-magics/bigquery_magics/core.py index 7fc52207ebafa..e9fab22078e9a 100644 --- a/packages/bigquery-magics/bigquery_magics/core.py +++ b/packages/bigquery-magics/bigquery_magics/core.py @@ -18,9 +18,9 @@ from google.api_core import client_info from google.cloud import bigquery -from bigquery_magics import environment import bigquery_magics.config import bigquery_magics.version +from bigquery_magics import environment context = bigquery_magics.config.context diff --git a/packages/bigquery-magics/bigquery_magics/line_arg_parser/lexer.py b/packages/bigquery-magics/bigquery_magics/line_arg_parser/lexer.py index 6e8b4cc9637bc..180dd8b9528f3 100644 --- a/packages/bigquery-magics/bigquery_magics/line_arg_parser/lexer.py +++ b/packages/bigquery-magics/bigquery_magics/line_arg_parser/lexer.py @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from collections import OrderedDict, namedtuple import enum import itertools import re +from collections import OrderedDict, namedtuple Token = namedtuple("Token", ("type_", "lexeme", "pos")) StateTransition = namedtuple("StateTransition", ("new_state", "total_offset")) diff --git a/packages/bigquery-magics/noxfile.py b/packages/bigquery-magics/noxfile.py index 7a273ac284197..1b6f820f1c033 100644 --- a/packages/bigquery-magics/noxfile.py +++ b/packages/bigquery-magics/noxfile.py @@ -151,10 +151,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install("setuptools", FLAKE8_VERSION, BLACK_VERSION) + session.install("setuptools", FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", "bigquery_magics", "tests") diff --git a/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py b/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py index 55dd8de50119c..2260406faa610 100644 --- a/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py +++ b/packages/bigquery-magics/tests/unit/bigquery/test_bigquery.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from concurrent import futures import contextlib import copy import json @@ -21,22 +20,23 @@ import re import sys import tempfile -from unittest import mock import warnings +from concurrent import futures +from unittest import mock +import google.auth.credentials +import google.cloud.bigquery._http +import google.cloud.bigquery.exceptions import IPython -from IPython.testing import globalipapp import IPython.utils.io as io +import pandas +import pytest from google.api_core import exceptions -import google.auth.credentials from google.cloud import bigquery from google.cloud.bigquery import exceptions as bq_exceptions from google.cloud.bigquery import job, table -import google.cloud.bigquery._http -import google.cloud.bigquery.exceptions from google.cloud.bigquery.retry import DEFAULT_TIMEOUT -import pandas -import pytest +from IPython.testing import globalipapp import bigquery_magics import bigquery_magics.bigquery as magics @@ -366,8 +366,9 @@ def test__make_bqstorage_client_true_obsolete_dependency(): "google-cloud-bigquery-storage is outdated" ), ) - with patcher, pytest.raises( - google.cloud.bigquery.exceptions.LegacyBigQueryStorageError + with ( + patcher, + pytest.raises(google.cloud.bigquery.exceptions.LegacyBigQueryStorageError), ): magics._make_bqstorage_client(test_client, {}) @@ -509,9 +510,11 @@ def test_bigquery_graph_spanner_graph_notebook_missing(monkeypatch): ) query_job_mock.to_dataframe.return_value = result - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock return_value = ip.run_cell_magic("bigquery", "--graph", sql) @@ -564,9 +567,11 @@ def test_bigquery_graph_int_result(monkeypatch): ) query_job_mock.to_dataframe.return_value = result - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock return_value = ip.run_cell_magic("bigquery", "--graph", sql) @@ -619,9 +624,11 @@ def test_bigquery_graph_str_result(monkeypatch): ) query_job_mock.to_dataframe.return_value = result - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock return_value = ip.run_cell_magic("bigquery", "--graph", sql) @@ -692,9 +699,11 @@ def test_bigquery_graph_json_json_result(monkeypatch): query_job_mock.configuration.destination.dataset_id = DATASET_ID query_job_mock.configuration.destination.table_id = TABLE_ID - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock try: return_value = ip.run_cell_magic("bigquery", "--graph", sql) @@ -761,9 +770,11 @@ def test_bigquery_graph_json_result(monkeypatch): query_job_mock.configuration.destination.dataset_id = DATASET_ID query_job_mock.configuration.destination.table_id = TABLE_ID - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock return_value = ip.run_cell_magic("bigquery", "--graph", sql) @@ -871,9 +882,11 @@ def test_bigquery_graph_size_exceeds_max(monkeypatch): query_job_mock.configuration.destination.dataset_id = DATASET_ID query_job_mock.configuration.destination.table_id = TABLE_ID - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock ip.run_cell_magic("bigquery", "--graph", sql) @@ -932,9 +945,11 @@ def test_bigquery_graph_size_exceeds_query_result_max(monkeypatch): query_job_mock.configuration.destination.dataset_id = DATASET_ID query_job_mock.configuration.destination.table_id = TABLE_ID - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock ip.run_cell_magic("bigquery", "--graph", sql) @@ -994,9 +1009,11 @@ def test_bigquery_graph_with_args_serialization(monkeypatch): query_job_mock.configuration.destination.dataset_id = DATASET_ID query_job_mock.configuration.destination.table_id = TABLE_ID - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock endpoint = "https://example.com" @@ -1076,9 +1093,11 @@ def test_bigquery_graph_colab(monkeypatch): query_job_mock.configuration.destination.dataset_id = DATASET_ID query_job_mock.configuration.destination.table_id = "test_destination_table" - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock try: return_value = ip.run_cell_magic("bigquery", "--graph", sql) @@ -1205,9 +1224,11 @@ def test_bigquery_graph_missing_spanner_deps(monkeypatch): ) query_job_mock.to_dataframe.return_value = result - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), display_patch as display_mock: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + display_patch as display_mock, + ): run_query_mock.return_value = query_job_mock with pytest.raises(ImportError): try: @@ -1485,9 +1506,13 @@ def test_bigquery_magic_default_connection_user_agent_vscode_extension( home_dir_patch = mock.patch("pathlib.Path.home", return_value=user_home) - with conn_patch as conn, ( - run_query_patch - ), default_patch, env_patch, home_dir_patch: + with ( + conn_patch as conn, + run_query_patch, + default_patch, + env_patch, + home_dir_patch, + ): ip.run_cell_magic("bigquery", "", "SELECT 17 as num") expected_user_agents = [ @@ -1564,9 +1589,13 @@ def custom_import_module_side_effect(name, package=None): "importlib.import_module", side_effect=custom_import_module_side_effect ) - with conn_patch as conn, ( - run_query_patch - ), default_patch, env_patch, extension_import_patch: + with ( + conn_patch as conn, + run_query_patch, + default_patch, + env_patch, + extension_import_patch, + ): ip.run_cell_magic("bigquery", "", "SELECT 17 as num") client_info_arg = conn.call_args[1].get("client_info") @@ -1695,9 +1724,11 @@ def test_bigquery_magic_with_bqstorage_from_argument(monkeypatch): google.cloud.bigquery.job.QueryJob, instance=True ) query_job_mock.to_dataframe.return_value = result - with run_query_patch as run_query_mock, ( - bqstorage_client_patch - ), warnings.catch_warnings(record=True) as warned: + with ( + run_query_patch as run_query_mock, + bqstorage_client_patch, + warnings.catch_warnings(record=True) as warned, + ): run_query_mock.return_value = query_job_mock return_value = ip.run_cell_magic("bigquery", "--use_bqstorage_api", sql) @@ -1901,11 +1932,12 @@ def test_bigquery_magic_w_max_results_query_job_results_fails(): ) query_job_mock.result.side_effect = [[], OSError] - with pytest.raises( - OSError - ), client_query_patch as client_query_mock, ( - default_patch - ), close_transports_patch as close_transports: + with ( + pytest.raises(OSError), + client_query_patch as client_query_mock, + default_patch, + close_transports_patch as close_transports, + ): client_query_mock.return_value = query_job_mock ip.run_cell_magic("bigquery", "--max_results=5", sql) @@ -2911,9 +2943,10 @@ def test_bigquery_magic_nonexisting_query_variable(): ip.user_ns.pop("custom_query", None) # Make sure the variable does NOT exist. cell_body = "$custom_query" # Referring to a non-existing variable name. - with pytest.raises( - NameError, match=r".*custom_query does not exist.*" - ), run_query_patch as run_query_mock: + with ( + pytest.raises(NameError, match=r".*custom_query does not exist.*"), + run_query_patch as run_query_mock, + ): ip.run_cell_magic("bigquery", "", cell_body) run_query_mock.assert_not_called() @@ -2928,9 +2961,10 @@ def test_bigquery_magic_empty_query_variable_name(): run_query_patch = mock.patch("bigquery_magics.bigquery._run_query", autospec=True) cell_body = "$" # Not referring to any variable (name omitted). - with pytest.raises( - NameError, match=r"(?i).*missing query variable name.*" - ), run_query_patch as run_query_mock: + with ( + pytest.raises(NameError, match=r"(?i).*missing query variable name.*"), + run_query_patch as run_query_mock, + ): ip.run_cell_magic("bigquery", "", cell_body) run_query_mock.assert_not_called() @@ -2949,9 +2983,10 @@ def test_bigquery_magic_query_variable_non_string(ipython_ns_cleanup): ip.user_ns["custom_query"] = object() cell_body = "$custom_query" # Referring to a non-string variable. - with pytest.raises( - TypeError, match=r".*must be a string or a bytes-like.*" - ), run_query_patch as run_query_mock: + with ( + pytest.raises(TypeError, match=r".*must be a string or a bytes-like.*"), + run_query_patch as run_query_mock, + ): ip.run_cell_magic("bigquery", "", cell_body) run_query_mock.assert_not_called() @@ -3076,9 +3111,11 @@ def test_bigquery_magic_create_dataset_fails(): autospec=True, ) - with pytest.raises( - OSError - ), create_dataset_if_necessary_patch, close_transports_patch as close_transports: + with ( + pytest.raises(OSError), + create_dataset_if_necessary_patch, + close_transports_patch as close_transports, + ): ip.run_cell_magic( "bigquery", "--destination_table dataset_id.table_id", diff --git a/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py b/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py index deca89943ebcc..a4b59bf0eb871 100644 --- a/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py +++ b/packages/bigquery-magics/tests/unit/bigquery/test_deprecation.py @@ -14,8 +14,8 @@ import pytest -from bigquery_magics import bigquery as magics import bigquery_magics.config +from bigquery_magics import bigquery as magics @pytest.fixture(autouse=True) diff --git a/packages/bigquery-magics/tests/unit/bigquery/test_pyformat.py b/packages/bigquery-magics/tests/unit/bigquery/test_pyformat.py index 3b9e8dc9f5dea..97be2ca0afed3 100644 --- a/packages/bigquery-magics/tests/unit/bigquery/test_pyformat.py +++ b/packages/bigquery-magics/tests/unit/bigquery/test_pyformat.py @@ -17,8 +17,8 @@ from typing import List from unittest import mock -from IPython.testing import globalipapp import pytest +from IPython.testing import globalipapp @pytest.mark.parametrize( diff --git a/packages/db-dtypes/db_dtypes/__init__.py b/packages/db-dtypes/db_dtypes/__init__.py index 51bb934fc9edf..3943c2a0e2857 100644 --- a/packages/db-dtypes/db_dtypes/__init__.py +++ b/packages/db-dtypes/db_dtypes/__init__.py @@ -17,20 +17,19 @@ import datetime import re -from typing import Optional, Union import warnings +from typing import Optional, Union import numpy import pandas import pandas.api.extensions -from pandas.errors import OutOfBoundsDatetime import pyarrow import pyarrow.compute +from pandas.errors import OutOfBoundsDatetime from db_dtypes import core from db_dtypes.json import JSONArray, JSONArrowType, JSONDtype # noqa: F401 - date_dtype_name = "dbdate" time_dtype_name = "dbtime" _EPOCH = datetime.datetime(1970, 1, 1) @@ -60,7 +59,7 @@ def construct_array_type(cls): @staticmethod def __from_arrow__( - array: Union[pyarrow.Array, pyarrow.ChunkedArray] + array: Union[pyarrow.Array, pyarrow.ChunkedArray], ) -> "TimeArray": """Convert to dbtime data from an Arrow array. @@ -219,7 +218,7 @@ def construct_array_type(cls): @staticmethod def __from_arrow__( - array: Union[pyarrow.Array, pyarrow.ChunkedArray] + array: Union[pyarrow.Array, pyarrow.ChunkedArray], ) -> "DateArray": """Convert to dbdate data from an Arrow array. diff --git a/packages/db-dtypes/db_dtypes/core.py b/packages/db-dtypes/db_dtypes/core.py index 8f265a52283ae..5a40b0c4fc590 100644 --- a/packages/db-dtypes/db_dtypes/core.py +++ b/packages/db-dtypes/db_dtypes/core.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional, Callable, Any +from typing import Any, Callable, Optional import numpy import pandas diff --git a/packages/db-dtypes/noxfile.py b/packages/db-dtypes/noxfile.py index b26f72f7a2854..60a999b1d39c6 100644 --- a/packages/db-dtypes/noxfile.py +++ b/packages/db-dtypes/noxfile.py @@ -110,11 +110,25 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) session.run("python", "-m", "pip", "freeze") + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", "db_dtypes", "tests") diff --git a/packages/db-dtypes/tests/compliance/date/test_date_compliance.py b/packages/db-dtypes/tests/compliance/date/test_date_compliance.py index 4b8a99b647198..a37c4664862f0 100644 --- a/packages/db-dtypes/tests/compliance/date/test_date_compliance.py +++ b/packages/db-dtypes/tests/compliance/date/test_date_compliance.py @@ -22,8 +22,8 @@ import pandas import pandas._testing as tm -from pandas.tests.extension import base import pytest +from pandas.tests.extension import base import db_dtypes diff --git a/packages/db-dtypes/tests/compliance/time/test_time_compliance.py b/packages/db-dtypes/tests/compliance/time/test_time_compliance.py index 0e8ddd37489c0..bb0f4f7b7b9c0 100644 --- a/packages/db-dtypes/tests/compliance/time/test_time_compliance.py +++ b/packages/db-dtypes/tests/compliance/time/test_time_compliance.py @@ -22,8 +22,8 @@ import pandas import pandas._testing as tm -from pandas.tests.extension import base import pytest +from pandas.tests.extension import base import db_dtypes diff --git a/packages/db-dtypes/tests/unit/test__init__.py b/packages/db-dtypes/tests/unit/test__init__.py index 7fa02fe779e04..04408d3e293cc 100644 --- a/packages/db-dtypes/tests/unit/test__init__.py +++ b/packages/db-dtypes/tests/unit/test__init__.py @@ -44,9 +44,10 @@ def test_check_python_version_warns_on_unsupported(mock_version_tuple, version_s mock_version = VersionInfo(*mock_version_tuple) # Mock sys.version_info and warnings.warn - with mock.patch("sys.version_info", new=mock_version), mock.patch( - MOCK_WARN - ) as mock_warn_call: + with ( + mock.patch("sys.version_info", new=mock_version), + mock.patch(MOCK_WARN) as mock_warn_call, + ): _check_python_version() # Call the function # Assert that warnings.warn was called exactly once @@ -83,9 +84,10 @@ def test_check_python_version_does_not_warn_on_supported(mock_version_tuple): mock_version = VersionInfo(*mock_version_tuple) # Mock sys.version_info and warnings.warn - with mock.patch("sys.version_info", new=mock_version), mock.patch( - MOCK_WARN - ) as mock_warn_call: + with ( + mock.patch("sys.version_info", new=mock_version), + mock.patch(MOCK_WARN) as mock_warn_call, + ): _check_python_version() # Assert that warnings.warn was NOT called diff --git a/packages/db-dtypes/tests/unit/test_date.py b/packages/db-dtypes/tests/unit/test_date.py index 4fb41a2e45317..717f3c80ccb2d 100644 --- a/packages/db-dtypes/tests/unit/test_date.py +++ b/packages/db-dtypes/tests/unit/test_date.py @@ -18,9 +18,9 @@ import numpy import numpy.testing import pandas -from pandas.errors import OutOfBoundsDatetime import pandas.testing import pytest +from pandas.errors import OutOfBoundsDatetime import db_dtypes from db_dtypes import pandas_backports diff --git a/packages/google-auth-httplib2/google_auth_httplib2.py b/packages/google-auth-httplib2/google_auth_httplib2.py index cd32264ec519e..266c56083dd1b 100644 --- a/packages/google-auth-httplib2/google_auth_httplib2.py +++ b/packages/google-auth-httplib2/google_auth_httplib2.py @@ -19,8 +19,8 @@ import http.client import logging -from google.auth import exceptions, transport import httplib2 +from google.auth import exceptions, transport _LOGGER = logging.getLogger(__name__) # Properties present in file-like streams / buffers. @@ -192,7 +192,7 @@ def request( headers=None, redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None, - **kwargs + **kwargs, ): """Implementation of httplib2's Http.request.""" @@ -218,7 +218,7 @@ def request( headers=request_headers, redirections=redirections, connection_type=connection_type, - **kwargs + **kwargs, ) # If the response indicated that the credentials needed to be @@ -252,7 +252,7 @@ def request( redirections=redirections, connection_type=connection_type, _credential_refresh_attempt=_credential_refresh_attempt + 1, - **kwargs + **kwargs, ) return response, content diff --git a/packages/google-auth-httplib2/noxfile.py b/packages/google-auth-httplib2/noxfile.py index df530819a42dc..e0a9b93502a45 100644 --- a/packages/google-auth-httplib2/noxfile.py +++ b/packages/google-auth-httplib2/noxfile.py @@ -93,10 +93,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", "google_auth_httplib2.py", "tests") diff --git a/packages/google-auth-httplib2/tests/compliance.py b/packages/google-auth-httplib2/tests/compliance.py index 9d8d7e79d0842..ac9863060573d 100644 --- a/packages/google-auth-httplib2/tests/compliance.py +++ b/packages/google-auth-httplib2/tests/compliance.py @@ -15,8 +15,8 @@ import http.client import flask -from google.auth import exceptions import pytest +from google.auth import exceptions from pytest_localserver.http import WSGIServer # .invalid will never resolve, see https://tools.ietf.org/html/rfc2606 diff --git a/packages/google-auth-oauthlib/google_auth_oauthlib/flow.py b/packages/google-auth-oauthlib/google_auth_oauthlib/flow.py index eb1ed799223ae..ba87a214a28a7 100644 --- a/packages/google-auth-oauthlib/google_auth_oauthlib/flow.py +++ b/packages/google-auth-oauthlib/google_auth_oauthlib/flow.py @@ -48,20 +48,21 @@ https://developers.google.com/identity/protocols/oauth2 """ -from base64 import urlsafe_b64encode + import hashlib import json import logging +from base64 import urlsafe_b64encode try: from secrets import SystemRandom except ImportError: # pragma: NO COVER from random import SystemRandom -from string import ascii_letters, digits import webbrowser import wsgiref.simple_server import wsgiref.util +from string import ascii_letters, digits import google.auth.transport.requests import google.oauth2.credentials @@ -380,7 +381,7 @@ def run_local_server( timeout_seconds=None, token_audience=None, browser=None, - **kwargs + **kwargs, ): """Run the flow using the server strategy. diff --git a/packages/google-auth-oauthlib/google_auth_oauthlib/helpers.py b/packages/google-auth-oauthlib/google_auth_oauthlib/helpers.py index 663c1c9832bf5..7da50d7e12185 100644 --- a/packages/google-auth-oauthlib/google_auth_oauthlib/helpers.py +++ b/packages/google-auth-oauthlib/google_auth_oauthlib/helpers.py @@ -24,9 +24,9 @@ import datetime import json -from google.auth import external_account_authorized_user import google.oauth2.credentials import requests_oauthlib +from google.auth import external_account_authorized_user _REQUIRED_CONFIG_KEYS = frozenset(("auth_uri", "token_uri", "client_id")) @@ -123,7 +123,7 @@ def credentials_from_session(session, client_config=None): if not session.token: raise ValueError( - "There is no access token for this session, did you call " "fetch_token?" + "There is no access token for this session, did you call fetch_token?" ) if "3pi" in client_config: diff --git a/packages/google-auth-oauthlib/noxfile.py b/packages/google-auth-oauthlib/noxfile.py index eb5419d94ec2e..1aff20a1e3d96 100644 --- a/packages/google-auth-oauthlib/noxfile.py +++ b/packages/google-auth-oauthlib/noxfile.py @@ -93,10 +93,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", *LINT_PATHS) diff --git a/packages/google-auth-oauthlib/setup.py b/packages/google-auth-oauthlib/setup.py index 77c9a8ace1005..9b2c842b8dfd7 100644 --- a/packages/google-auth-oauthlib/setup.py +++ b/packages/google-auth-oauthlib/setup.py @@ -51,7 +51,7 @@ extras_require={"tool": TOOL_DEPENDENCIES}, entry_points={ "console_scripts": [ - "google-oauthlib-tool" "=google_auth_oauthlib.tool.__main__:main [tool]" + "google-oauthlib-tool=google_auth_oauthlib.tool.__main__:main [tool]" ] }, python_requires=">=3.10", diff --git a/packages/google-auth-oauthlib/tests/unit/test_flow.py b/packages/google-auth-oauthlib/tests/unit/test_flow.py index ed197069d6336..fff68e0ce2943 100644 --- a/packages/google-auth-oauthlib/tests/unit/test_flow.py +++ b/packages/google-auth-oauthlib/tests/unit/test_flow.py @@ -14,15 +14,15 @@ import concurrent.futures import datetime -from functools import partial import json import logging import os import re import socket -from unittest import mock import urllib import webbrowser +from functools import partial +from unittest import mock import pytest import requests diff --git a/packages/google-auth-oauthlib/tests/unit/test_helpers.py b/packages/google-auth-oauthlib/tests/unit/test_helpers.py index 59d97c74687e3..fb494214c99c7 100644 --- a/packages/google-auth-oauthlib/tests/unit/test_helpers.py +++ b/packages/google-auth-oauthlib/tests/unit/test_helpers.py @@ -17,9 +17,9 @@ import os from unittest import mock -from google.auth import external_account_authorized_user import google.oauth2.credentials import pytest +from google.auth import external_account_authorized_user from google_auth_oauthlib import helpers @@ -133,9 +133,9 @@ def test_credentials_from_session_3pi(session): client_secrets_info = CLIENT_SECRETS_INFO["web"].copy() client_secrets_info["3pi"] = True - client_secrets_info[ - "token_info_url" - ] = "https://accounts.google.com/o/oauth2/introspect" + client_secrets_info["token_info_url"] = ( + "https://accounts.google.com/o/oauth2/introspect" + ) credentials = helpers.credentials_from_session(session, client_secrets_info) assert isinstance(credentials, external_account_authorized_user.Credentials) diff --git a/packages/google-auth-oauthlib/tests/unit/test_interactive.py b/packages/google-auth-oauthlib/tests/unit/test_interactive.py index 88a5bf94f09d4..2c648846dad8a 100644 --- a/packages/google-auth-oauthlib/tests/unit/test_interactive.py +++ b/packages/google-auth-oauthlib/tests/unit/test_interactive.py @@ -86,9 +86,12 @@ def mock_find_open_port(start=8080, stop=None): monkeypatch.setattr(module_under_test, "find_open_port", mock_find_open_port) mock_flow = mock.create_autospec(flow.InstalledAppFlow, instance=True) - with mock.patch( - "google_auth_oauthlib.flow.InstalledAppFlow", autospec=True - ) as mock_flow, pytest.raises(ConnectionError): + with ( + mock.patch( + "google_auth_oauthlib.flow.InstalledAppFlow", autospec=True + ) as mock_flow, + pytest.raises(ConnectionError), + ): mock_flow.from_client_config.return_value = mock_flow module_under_test.get_user_credentials( ["scopes"], "some-client-id", "shh-secret" diff --git a/packages/google-auth/google/auth/__init__.py b/packages/google-auth/google/auth/__init__.py index 927efdd9f8074..985745c45c3cd 100644 --- a/packages/google-auth/google/auth/__init__.py +++ b/packages/google-auth/google/auth/__init__.py @@ -23,7 +23,6 @@ load_credentials_from_file, ) - __version__ = google_auth_version.__version__ diff --git a/packages/google-auth/google/auth/_agent_identity_utils.py b/packages/google-auth/google/auth/_agent_identity_utils.py index 4de5d709b4b55..0fa6a92f57ff2 100644 --- a/packages/google-auth/google/auth/_agent_identity_utils.py +++ b/packages/google-auth/google/auth/_agent_identity_utils.py @@ -20,8 +20,8 @@ import re import stat import time -from urllib.parse import quote, urlparse import warnings +from urllib.parse import quote, urlparse from google.auth import environment_vars, exceptions diff --git a/packages/google-auth/google/auth/_cloud_sdk.py b/packages/google-auth/google/auth/_cloud_sdk.py index 85b3c4f99be37..05f0e9e725152 100644 --- a/packages/google-auth/google/auth/_cloud_sdk.py +++ b/packages/google-auth/google/auth/_cloud_sdk.py @@ -17,10 +17,7 @@ import os import subprocess -from google.auth import _helpers -from google.auth import environment_vars -from google.auth import exceptions - +from google.auth import _helpers, environment_vars, exceptions # The ~/.config subdirectory containing gcloud credentials. _CONFIG_DIRECTORY = "gcloud" diff --git a/packages/google-auth/google/auth/_credentials_async.py b/packages/google-auth/google/auth/_credentials_async.py index 937f6e8fb6df9..dcc1dac8044ee 100644 --- a/packages/google-auth/google/auth/_credentials_async.py +++ b/packages/google-auth/google/auth/_credentials_async.py @@ -18,8 +18,7 @@ import abc import inspect -from google.auth import _regional_access_boundary_utils -from google.auth import credentials +from google.auth import _regional_access_boundary_utils, credentials class Credentials(credentials.Credentials, metaclass=abc.ABCMeta): diff --git a/packages/google-auth/google/auth/_default.py b/packages/google-auth/google/auth/_default.py index cb40c1fa6d778..63b7c5dd3b92f 100644 --- a/packages/google-auth/google/auth/_default.py +++ b/packages/google-auth/google/auth/_default.py @@ -16,17 +16,17 @@ Implements application default credentials and project ID detection. """ + from __future__ import annotations import io import json import logging import os -from typing import Optional, Sequence, TYPE_CHECKING import warnings +from typing import TYPE_CHECKING, Optional, Sequence -from google.auth import environment_vars -from google.auth import exceptions +from google.auth import environment_vars, exceptions if TYPE_CHECKING: # pragma: NO COVER import google.auth.credentials.Credentials # type: ignore @@ -393,9 +393,9 @@ def _get_gce_credentials(request=None, quota_project_id=None): # some cases where it's not available, so we tolerate ImportError. # Compute Engine requires optional `requests` dependency. try: + import google.auth.transport.requests from google.auth import compute_engine from google.auth.compute_engine import _metadata - import google.auth.transport.requests except ImportError: _LOGGER.warning("Import of Compute Engine auth library failed.") return None, None @@ -692,8 +692,10 @@ def default( If no credentials were found, or if the credentials found were invalid. """ - from google.auth.credentials import with_scopes_if_required - from google.auth.credentials import CredentialsWithQuotaProject + from google.auth.credentials import ( + CredentialsWithQuotaProject, + with_scopes_if_required, + ) explicit_project_id = os.environ.get( environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT) diff --git a/packages/google-auth/google/auth/_default_async.py b/packages/google-auth/google/auth/_default_async.py index 44bc6719f97a5..13bd4bfae2a5e 100644 --- a/packages/google-auth/google/auth/_default_async.py +++ b/packages/google-auth/google/auth/_default_async.py @@ -22,9 +22,7 @@ import os import warnings -from google.auth import _default -from google.auth import environment_vars -from google.auth import exceptions +from google.auth import _default, environment_vars, exceptions def load_credentials_from_file(filename, scopes=None, quota_project_id=None): diff --git a/packages/google-auth/google/auth/_helpers.py b/packages/google-auth/google/auth/_helpers.py index 86c48c1e525cf..568def959d5a6 100644 --- a/packages/google-auth/google/auth/_helpers.py +++ b/packages/google-auth/google/auth/_helpers.py @@ -17,17 +17,16 @@ import base64 import calendar import datetime -from email.message import Message import hashlib import json import logging import sys -from typing import Any, Dict, Mapping, Optional, Union import urllib +from email.message import Message +from typing import Any, Dict, Mapping, Optional, Union from google.auth import exceptions - DEFAULT_UNIVERSE_DOMAIN = "googleapis.com" # _BASE_LOGGER_NAME is the base logger for all google-based loggers. diff --git a/packages/google-auth/google/auth/_jwt_async.py b/packages/google-auth/google/auth/_jwt_async.py index ce3bfe4eba572..7ef33e9f4fbd8 100644 --- a/packages/google-auth/google/auth/_jwt_async.py +++ b/packages/google-auth/google/auth/_jwt_async.py @@ -43,10 +43,12 @@ change in minor releases. """ -from google.auth import _credentials_async -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import jwt +from google.auth import ( + _credentials_async, + _helpers, + _regional_access_boundary_utils, + jwt, +) def encode(signer, payload, header=None, key_id=None): diff --git a/packages/google-auth/google/auth/_oauth2client.py b/packages/google-auth/google/auth/_oauth2client.py index 8032b26ad2edc..77f0efc2ddc79 100644 --- a/packages/google-auth/google/auth/_oauth2client.py +++ b/packages/google-auth/google/auth/_oauth2client.py @@ -21,11 +21,11 @@ from __future__ import absolute_import -from google.auth import _helpers import google.auth.app_engine import google.auth.compute_engine import google.oauth2.credentials import google.oauth2.service_account +from google.auth import _helpers try: import oauth2client.client # type: ignore @@ -128,9 +128,9 @@ def _convert_appengine_app_assertion_credentials(credentials): } if _HAS_APPENGINE: # pragma: no cover - _CLASS_CONVERSION_MAP[ - oauth2client.contrib.appengine.AppAssertionCredentials - ] = _convert_appengine_app_assertion_credentials + _CLASS_CONVERSION_MAP[oauth2client.contrib.appengine.AppAssertionCredentials] = ( + _convert_appengine_app_assertion_credentials + ) def convert(credentials): diff --git a/packages/google-auth/google/auth/_regional_access_boundary_utils.py b/packages/google-auth/google/auth/_regional_access_boundary_utils.py index d09c5f0ff0162..39c5699c4ce02 100644 --- a/packages/google-auth/google/auth/_regional_access_boundary_utils.py +++ b/packages/google-auth/google/auth/_regional_access_boundary_utils.py @@ -21,7 +21,7 @@ import inspect import logging import threading -from typing import NamedTuple, Optional, TYPE_CHECKING +from typing import TYPE_CHECKING, NamedTuple, Optional from google.auth import _helpers diff --git a/packages/google-auth/google/auth/_service_account_info.py b/packages/google-auth/google/auth/_service_account_info.py index c432080a907d4..a60a7b9b50e0c 100644 --- a/packages/google-auth/google/auth/_service_account_info.py +++ b/packages/google-auth/google/auth/_service_account_info.py @@ -17,8 +17,7 @@ import io import json -from google.auth import crypt -from google.auth import exceptions +from google.auth import crypt, exceptions def from_dict(data, require=None, use_rsa_signer=True): diff --git a/packages/google-auth/google/auth/aio/__init__.py b/packages/google-auth/google/auth/aio/__init__.py index 331708cba62c4..7926fb88404dc 100644 --- a/packages/google-auth/google/auth/aio/__init__.py +++ b/packages/google-auth/google/auth/aio/__init__.py @@ -18,7 +18,6 @@ from google.auth import version as google_auth_version - __version__ = google_auth_version.__version__ # Set default logging handler to avoid "No handler found" warnings. diff --git a/packages/google-auth/google/auth/aio/credentials.py b/packages/google-auth/google/auth/aio/credentials.py index 3bc6a5a6762a5..10320f2b060e1 100644 --- a/packages/google-auth/google/auth/aio/credentials.py +++ b/packages/google-auth/google/auth/aio/credentials.py @@ -15,9 +15,7 @@ """Interfaces for asynchronous credentials.""" - -from google.auth import _helpers -from google.auth import exceptions +from google.auth import _helpers, exceptions from google.auth._credentials_base import _BaseCredentials diff --git a/packages/google-auth/google/auth/aio/transport/__init__.py b/packages/google-auth/google/auth/aio/transport/__init__.py index 343711272a95b..091bd29fbaff7 100644 --- a/packages/google-auth/google/auth/aio/transport/__init__.py +++ b/packages/google-auth/google/auth/aio/transport/__init__.py @@ -29,7 +29,6 @@ import google.auth.transport - _DEFAULT_TIMEOUT_SECONDS = 180 DEFAULT_RETRYABLE_STATUS_CODES = google.auth.transport.DEFAULT_RETRYABLE_STATUS_CODES @@ -111,7 +110,7 @@ async def __call__( body: Optional[bytes], headers: Optional[Mapping[str, str]], timeout: float, - **kwargs + **kwargs, ) -> Response: """Make an HTTP request. diff --git a/packages/google-auth/google/auth/aio/transport/aiohttp.py b/packages/google-auth/google/auth/aio/transport/aiohttp.py index f6a222e05cb38..831e5234d2b14 100644 --- a/packages/google-auth/google/auth/aio/transport/aiohttp.py +++ b/packages/google-auth/google/auth/aio/transport/aiohttp.py @@ -16,7 +16,7 @@ import asyncio import logging -from typing import AsyncGenerator, Mapping, Optional, TYPE_CHECKING, Union +from typing import TYPE_CHECKING, AsyncGenerator, Mapping, Optional, Union try: import aiohttp # type: ignore @@ -25,8 +25,7 @@ "The aiohttp library is not installed from please install the aiohttp package to use the aiohttp transport." ) from caught_exc -from google.auth import _helpers -from google.auth import exceptions +from google.auth import _helpers, exceptions from google.auth.aio import _helpers as _helpers_async from google.auth.aio import transport diff --git a/packages/google-auth/google/auth/aio/transport/mtls.py b/packages/google-auth/google/auth/aio/transport/mtls.py index a7d1baf7355d3..6b4b2ea234551 100644 --- a/packages/google-auth/google/auth/aio/transport/mtls.py +++ b/packages/google-auth/google/auth/aio/transport/mtls.py @@ -22,9 +22,9 @@ import ssl from typing import Optional +import google.auth.transport.mtls from google.auth import exceptions from google.auth.transport._mtls_helper import secure_cert_key_paths -import google.auth.transport.mtls _LOGGER = logging.getLogger(__name__) diff --git a/packages/google-auth/google/auth/aio/transport/sessions.py b/packages/google-auth/google/auth/aio/transport/sessions.py index d88162667bdad..6d4da4d1c2a11 100644 --- a/packages/google-auth/google/auth/aio/transport/sessions.py +++ b/packages/google-auth/google/auth/aio/transport/sessions.py @@ -13,18 +13,18 @@ # limitations under the License. import asyncio -from contextlib import asynccontextmanager import functools import time -from typing import Mapping, Optional, TYPE_CHECKING, Union import warnings +from contextlib import asynccontextmanager +from typing import TYPE_CHECKING, Mapping, Optional, Union +import google.auth.transport._mtls_helper from google.auth import _exponential_backoff, exceptions from google.auth.aio import transport from google.auth.aio.credentials import Credentials from google.auth.aio.transport import mtls from google.auth.exceptions import TimeoutError -import google.auth.transport._mtls_helper if TYPE_CHECKING: # pragma: NO COVER import aiohttp diff --git a/packages/google-auth/google/auth/api_key.py b/packages/google-auth/google/auth/api_key.py index 4fdf7f2769ca8..7aaca90b69388 100644 --- a/packages/google-auth/google/auth/api_key.py +++ b/packages/google-auth/google/auth/api_key.py @@ -18,9 +18,7 @@ https://cloud.google.com/docs/authentication/api-keys/ """ -from google.auth import _helpers -from google.auth import credentials -from google.auth import exceptions +from google.auth import _helpers, credentials, exceptions class Credentials(credentials.Credentials): diff --git a/packages/google-auth/google/auth/app_engine.py b/packages/google-auth/google/auth/app_engine.py index 49f6457f4af13..6937c842dc666 100644 --- a/packages/google-auth/google/auth/app_engine.py +++ b/packages/google-auth/google/auth/app_engine.py @@ -22,11 +22,7 @@ https://cloud.google.com/appengine/docs/python/appidentity/ """ - -from google.auth import _helpers -from google.auth import credentials -from google.auth import crypt -from google.auth import exceptions +from google.auth import _helpers, credentials, crypt, exceptions # pytype: disable=import-error try: diff --git a/packages/google-auth/google/auth/aws.py b/packages/google-auth/google/auth/aws.py index 46c913a7a96f6..1b5f2b275393a 100644 --- a/packages/google-auth/google/auth/aws.py +++ b/packages/google-auth/google/auth/aws.py @@ -39,7 +39,6 @@ """ import abc -from dataclasses import dataclass import hashlib import hmac import http.client as http_client @@ -47,14 +46,12 @@ import os import posixpath import re -from typing import Optional import urllib +from dataclasses import dataclass +from typing import Optional from urllib.parse import urljoin -from google.auth import _helpers -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import external_account +from google.auth import _helpers, environment_vars, exceptions, external_account # AWS Signature Version 4 signing algorithm identifier. _AWS_ALGORITHM = "AWS4-HMAC-SHA256" @@ -273,9 +270,9 @@ def _generate_authentication_header_map( full_headers[key.lower()] = additional_headers[key] # Add AWS session token if available. if aws_security_credentials.session_token is not None: - full_headers[ - _AWS_SECURITY_TOKEN_HEADER - ] = aws_security_credentials.session_token + full_headers[_AWS_SECURITY_TOKEN_HEADER] = ( + aws_security_credentials.session_token + ) # Required headers full_headers["host"] = host @@ -618,7 +615,7 @@ def __init__( credential_source=None, aws_security_credentials_supplier=None, *args, - **kwargs + **kwargs, ): """Instantiates an AWS workload external account credentials object. @@ -666,7 +663,7 @@ def __init__( token_url=token_url, credential_source=credential_source, *args, - **kwargs + **kwargs, ) if credential_source is None and aws_security_credentials_supplier is None: raise exceptions.InvalidValue( diff --git a/packages/google-auth/google/auth/compute_engine/__init__.py b/packages/google-auth/google/auth/compute_engine/__init__.py index 7e1206fc1b28a..6179cb37f7a53 100644 --- a/packages/google-auth/google/auth/compute_engine/__init__.py +++ b/packages/google-auth/google/auth/compute_engine/__init__.py @@ -15,8 +15,6 @@ """Google Compute Engine authentication.""" from google.auth.compute_engine._metadata import detect_gce_residency_linux -from google.auth.compute_engine.credentials import Credentials -from google.auth.compute_engine.credentials import IDTokenCredentials - +from google.auth.compute_engine.credentials import Credentials, IDTokenCredentials __all__ = ["Credentials", "IDTokenCredentials", "detect_gce_residency_linux"] diff --git a/packages/google-auth/google/auth/compute_engine/_metadata.py b/packages/google-auth/google/auth/compute_engine/_metadata.py index 1ea7792c2cddf..58c509dfa768a 100644 --- a/packages/google-auth/google/auth/compute_engine/_metadata.py +++ b/packages/google-auth/google/auth/compute_engine/_metadata.py @@ -27,15 +27,10 @@ import requests -from google.auth import _helpers -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import metrics -from google.auth import transport +from google.auth import _helpers, environment_vars, exceptions, metrics, transport from google.auth._exponential_backoff import ExponentialBackoff from google.auth.compute_engine import _mtls - _LOGGER = logging.getLogger(__name__) _SERVICE_ACCOUNT_EMAIL_PATTERN = re.compile( diff --git a/packages/google-auth/google/auth/compute_engine/_mtls.py b/packages/google-auth/google/auth/compute_engine/_mtls.py index c4d3a3c12bdff..dc7b8b31c84c3 100644 --- a/packages/google-auth/google/auth/compute_engine/_mtls.py +++ b/packages/google-auth/google/auth/compute_engine/_mtls.py @@ -16,12 +16,12 @@ # """Mutual TLS for Google Compute Engine metadata server.""" -from dataclasses import dataclass, field import enum import logging import os -from pathlib import Path import ssl +from dataclasses import dataclass, field +from pathlib import Path from urllib.parse import urlparse, urlunparse import requests @@ -29,7 +29,6 @@ from google.auth import environment_vars, exceptions - _LOGGER = logging.getLogger(__name__) _WINDOWS_OS_NAME = "nt" diff --git a/packages/google-auth/google/auth/compute_engine/credentials.py b/packages/google-auth/google/auth/compute_engine/credentials.py index 3701751bda2bb..d0d24b41f4e47 100644 --- a/packages/google-auth/google/auth/compute_engine/credentials.py +++ b/packages/google-auth/google/auth/compute_engine/credentials.py @@ -21,16 +21,17 @@ import datetime import logging -from typing import Optional, TYPE_CHECKING - - -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import credentials -from google.auth import exceptions -from google.auth import iam -from google.auth import jwt -from google.auth import metrics +from typing import TYPE_CHECKING, Optional + +from google.auth import ( + _helpers, + _regional_access_boundary_utils, + credentials, + exceptions, + iam, + jwt, + metrics, +) from google.auth.compute_engine import _metadata from google.oauth2 import _client @@ -174,7 +175,8 @@ def _is_regional_access_boundary_lookup_required(self): return _metadata._is_service_account_email(self.service_account_email) def _build_regional_access_boundary_lookup_url( - self, request: "Optional[google.auth.transport.Request]" = None # noqa: F821 + self, + request: "Optional[google.auth.transport.Request]" = None, # noqa: F821 ): """Builds and returns the URL for the regional access boundary lookup API for GCE. @@ -467,7 +469,7 @@ def with_token_uri(self, token_uri): # the request is not needed if self._use_metadata_identity_endpoint: raise ValueError( - "If use_metadata_identity_endpoint is set, token_uri" " must not be set" + "If use_metadata_identity_endpoint is set, token_uri must not be set" ) else: return self.__class__( diff --git a/packages/google-auth/google/auth/credentials.py b/packages/google-auth/google/auth/credentials.py index 3975dab48ad49..66319c0d04fcd 100644 --- a/packages/google-auth/google/auth/credentials.py +++ b/packages/google-auth/google/auth/credentials.py @@ -16,18 +16,20 @@ """Interfaces for credentials.""" import abc -from enum import Enum import logging import os -from typing import Dict, List, Optional, TYPE_CHECKING -from urllib.parse import urlparse import warnings +from enum import Enum +from typing import TYPE_CHECKING, Dict, List, Optional +from urllib.parse import urlparse - -from google.auth import _helpers, environment_vars -from google.auth import _regional_access_boundary_utils -from google.auth import exceptions -from google.auth import metrics +from google.auth import ( + _helpers, + _regional_access_boundary_utils, + environment_vars, + exceptions, + metrics, +) from google.auth._credentials_base import _BaseCredentials from google.auth._refresh_worker import RefreshThreadManager @@ -546,7 +548,8 @@ def _lookup_regional_access_boundary( @abc.abstractmethod def _build_regional_access_boundary_lookup_url( - self, request: "Optional[google.auth.transport.Request]" = None # noqa: F821 + self, + request: "Optional[google.auth.transport.Request]" = None, # noqa: F821 ): """ Builds and returns the URL for the Regional Access Boundary lookup API. diff --git a/packages/google-auth/google/auth/crypt/__init__.py b/packages/google-auth/google/auth/crypt/__init__.py index e56bc7b82df70..b61ba02fc6149 100644 --- a/packages/google-auth/google/auth/crypt/__init__.py +++ b/packages/google-auth/google/auth/crypt/__init__.py @@ -37,10 +37,7 @@ version is at least 1.4.0. """ -from google.auth.crypt import base -from google.auth.crypt import es -from google.auth.crypt import es256 -from google.auth.crypt import rsa +from google.auth.crypt import base, es, es256, rsa EsSigner = es.EsSigner EsVerifier = es.EsVerifier diff --git a/packages/google-auth/google/auth/crypt/_cryptography_rsa.py b/packages/google-auth/google/auth/crypt/_cryptography_rsa.py index 1a3e9ff52c664..baa614026007a 100644 --- a/packages/google-auth/google/auth/crypt/_cryptography_rsa.py +++ b/packages/google-auth/google/auth/crypt/_cryptography_rsa.py @@ -20,11 +20,10 @@ """ import cryptography.exceptions +import cryptography.x509 from cryptography.hazmat import backends -from cryptography.hazmat.primitives import hashes -from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import padding -import cryptography.x509 from google.auth import _helpers from google.auth.crypt import base diff --git a/packages/google-auth/google/auth/crypt/_python_rsa.py b/packages/google-auth/google/auth/crypt/_python_rsa.py index d9305e835dc96..743a7484ec6f4 100644 --- a/packages/google-auth/google/auth/crypt/_python_rsa.py +++ b/packages/google-auth/google/auth/crypt/_python_rsa.py @@ -24,14 +24,13 @@ import io import warnings +import rsa # type: ignore from pyasn1.codec.der import decoder # type: ignore from pyasn1_modules import pem # type: ignore from pyasn1_modules.rfc2459 import Certificate # type: ignore from pyasn1_modules.rfc5208 import PrivateKeyInfo # type: ignore -import rsa # type: ignore -from google.auth import _helpers -from google.auth import exceptions +from google.auth import _helpers, exceptions from google.auth.crypt import base _POW2 = (128, 64, 32, 16, 8, 4, 2, 1) diff --git a/packages/google-auth/google/auth/crypt/base.py b/packages/google-auth/google/auth/crypt/base.py index ad871c311566b..f9bb2bf0c9671 100644 --- a/packages/google-auth/google/auth/crypt/base.py +++ b/packages/google-auth/google/auth/crypt/base.py @@ -103,7 +103,7 @@ def from_service_account_info(cls, info): """ if _JSON_FILE_PRIVATE_KEY not in info: raise exceptions.MalformedError( - "The private_key field was not found in the service account " "info." + "The private_key field was not found in the service account info." ) return cls.from_string( diff --git a/packages/google-auth/google/auth/crypt/es.py b/packages/google-auth/google/auth/crypt/es.py index dbbe56b3f8fb7..ac656c88cc114 100644 --- a/packages/google-auth/google/auth/crypt/es.py +++ b/packages/google-auth/google/auth/crypt/es.py @@ -12,26 +12,24 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""ECDSA verifier and signer that use the ``cryptography`` library. -""" +"""ECDSA verifier and signer that use the ``cryptography`` library.""" from dataclasses import dataclass from typing import Any, Dict, Optional, Union import cryptography.exceptions -from cryptography.hazmat import backends -from cryptography.hazmat.primitives import hashes -from cryptography.hazmat.primitives import serialization -from cryptography.hazmat.primitives.asymmetric import ec -from cryptography.hazmat.primitives.asymmetric import padding -from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature -from cryptography.hazmat.primitives.asymmetric.utils import encode_dss_signature import cryptography.x509 +from cryptography.hazmat import backends +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ec, padding +from cryptography.hazmat.primitives.asymmetric.utils import ( + decode_dss_signature, + encode_dss_signature, +) from google.auth import _helpers from google.auth.crypt import base - _CERTIFICATE_MARKER = b"-----BEGIN CERTIFICATE-----" _BACKEND = backends.default_backend() _PADDING = padding.PKCS1v15() diff --git a/packages/google-auth/google/auth/crypt/es256.py b/packages/google-auth/google/auth/crypt/es256.py index e7bda5d3fc292..3160c48383900 100644 --- a/packages/google-auth/google/auth/crypt/es256.py +++ b/packages/google-auth/google/auth/crypt/es256.py @@ -12,11 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""ECDSA (ES256) verifier and signer that use the ``cryptography`` library. -""" +"""ECDSA (ES256) verifier and signer that use the ``cryptography`` library.""" -from google.auth.crypt.es import EsSigner -from google.auth.crypt.es import EsVerifier +from google.auth.crypt.es import EsSigner, EsVerifier class ES256Verifier(EsVerifier): diff --git a/packages/google-auth/google/auth/crypt/rsa.py b/packages/google-auth/google/auth/crypt/rsa.py index 639be90695491..436dcc2d9d058 100644 --- a/packages/google-auth/google/auth/crypt/rsa.py +++ b/packages/google-auth/google/auth/crypt/rsa.py @@ -19,12 +19,10 @@ for implmentations using different third party libraries """ -from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey -from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey +from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey, RSAPublicKey from google.auth import _helpers -from google.auth.crypt import _cryptography_rsa -from google.auth.crypt import base +from google.auth.crypt import _cryptography_rsa, base RSA_KEY_MODULE_PREFIX = "rsa.key" diff --git a/packages/google-auth/google/auth/downscoped.py b/packages/google-auth/google/auth/downscoped.py index ea75be90fe4e0..06a9ba5f771f5 100644 --- a/packages/google-auth/google/auth/downscoped.py +++ b/packages/google-auth/google/auth/downscoped.py @@ -50,9 +50,7 @@ import datetime -from google.auth import _helpers -from google.auth import credentials -from google.auth import exceptions +from google.auth import _helpers, credentials, exceptions from google.oauth2 import sts # The maximum number of access boundary rules a Credential Access Boundary can diff --git a/packages/google-auth/google/auth/environment_vars.py b/packages/google-auth/google/auth/environment_vars.py index 7d82d288a24cc..54550945f994f 100644 --- a/packages/google-auth/google/auth/environment_vars.py +++ b/packages/google-auth/google/auth/environment_vars.py @@ -14,7 +14,6 @@ """Environment variables used by :mod:`google.auth`.""" - PROJECT = "GOOGLE_CLOUD_PROJECT" """Environment variable defining default project. diff --git a/packages/google-auth/google/auth/external_account.py b/packages/google-auth/google/auth/external_account.py index b90fcab4c0ee5..e5d4fadc72f13 100644 --- a/packages/google-auth/google/auth/external_account.py +++ b/packages/google-auth/google/auth/external_account.py @@ -29,7 +29,6 @@ import abc import copy -from dataclasses import dataclass import datetime import functools import io @@ -37,17 +36,18 @@ import logging import re import threading -from typing import Optional, TYPE_CHECKING - - -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import credentials -from google.auth import exceptions -from google.auth import impersonated_credentials -from google.auth import metrics -from google.oauth2 import sts -from google.oauth2 import utils +from dataclasses import dataclass +from typing import TYPE_CHECKING, Optional + +from google.auth import ( + _helpers, + _regional_access_boundary_utils, + credentials, + exceptions, + impersonated_credentials, + metrics, +) +from google.oauth2 import sts, utils if TYPE_CHECKING: # pragma: NO COVER import google.auth.transport @@ -536,7 +536,8 @@ def _perform_refresh_token(self, request, cert_fingerprint=None): self.expiry = now + lifetime def _build_regional_access_boundary_lookup_url( - self, request: "Optional[google.auth.transport.Request]" = None # noqa: F821 + self, + request: "Optional[google.auth.transport.Request]" = None, # noqa: F821 ): """Builds and returns the URL for the Regional Access Boundary lookup API.""" if getattr(self, "_impersonated_credentials", None): @@ -746,7 +747,7 @@ def from_info(cls, info, **kwargs): "universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN ), trust_boundary=info.get("trust_boundary"), - **kwargs + **kwargs, ) @classmethod diff --git a/packages/google-auth/google/auth/external_account_authorized_user.py b/packages/google-auth/google/auth/external_account_authorized_user.py index 35144f15d69ed..0b387053baa91 100644 --- a/packages/google-auth/google/auth/external_account_authorized_user.py +++ b/packages/google-auth/google/auth/external_account_authorized_user.py @@ -38,15 +38,15 @@ import json import logging import re -from typing import Optional, TYPE_CHECKING +from typing import TYPE_CHECKING, Optional - -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import credentials -from google.auth import exceptions -from google.oauth2 import sts -from google.oauth2 import utils +from google.auth import ( + _helpers, + _regional_access_boundary_utils, + credentials, + exceptions, +) +from google.oauth2 import sts, utils if TYPE_CHECKING: # pragma: NO COVER import google.auth.transport @@ -317,7 +317,8 @@ def _perform_refresh_token(self, request): self._refresh_token = response_data["refresh_token"] def _build_regional_access_boundary_lookup_url( - self, request: "Optional[google.auth.transport.Request]" = None # noqa: F821 + self, + request: "Optional[google.auth.transport.Request]" = None, # noqa: F821 ): """Builds and returns the URL for the Regional Access Boundary lookup API. @@ -442,7 +443,7 @@ def from_info(cls, info, **kwargs): "universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN ), trust_boundary=info.get("trust_boundary"), - **kwargs + **kwargs, ) @classmethod diff --git a/packages/google-auth/google/auth/iam.py b/packages/google-auth/google/auth/iam.py index 2ecb1b0014b88..46ae558c51980 100644 --- a/packages/google-auth/google/auth/iam.py +++ b/packages/google-auth/google/auth/iam.py @@ -23,11 +23,7 @@ import http.client as http_client import json -from google.auth import _exponential_backoff -from google.auth import _helpers -from google.auth import credentials -from google.auth import crypt -from google.auth import exceptions +from google.auth import _exponential_backoff, _helpers, credentials, crypt, exceptions from google.auth.transport import _mtls_helper IAM_RETRY_CODES = { diff --git a/packages/google-auth/google/auth/identity_pool.py b/packages/google-auth/google/auth/identity_pool.py index dd5f103b72502..41b312e4f8414 100644 --- a/packages/google-auth/google/auth/identity_pool.py +++ b/packages/google-auth/google/auth/identity_pool.py @@ -46,9 +46,7 @@ import os from typing import NamedTuple -from google.auth import _helpers -from google.auth import exceptions -from google.auth import external_account +from google.auth import _helpers, exceptions, external_account from google.auth.transport import _mtls_helper @@ -277,7 +275,7 @@ def __init__( credential_source=None, subject_token_supplier=None, *args, - **kwargs + **kwargs, ): """Instantiates an external account credentials object from a file/URL. @@ -335,7 +333,7 @@ def __init__( token_url=token_url, credential_source=credential_source, *args, - **kwargs + **kwargs, ) if credential_source is None and subject_token_supplier is None: raise exceptions.InvalidValue( diff --git a/packages/google-auth/google/auth/impersonated_credentials.py b/packages/google-auth/google/auth/impersonated_credentials.py index 7239a37b4ec67..f9faab5e73360 100644 --- a/packages/google-auth/google/auth/impersonated_credentials.py +++ b/packages/google-auth/google/auth/impersonated_credentials.py @@ -27,21 +27,22 @@ import base64 import copy -from datetime import datetime import http.client as http_client import json import logging -from typing import Optional, TYPE_CHECKING - - -from google.auth import _exponential_backoff -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import credentials -from google.auth import exceptions -from google.auth import iam -from google.auth import jwt -from google.auth import metrics +from datetime import datetime +from typing import TYPE_CHECKING, Optional + +from google.auth import ( + _exponential_backoff, + _helpers, + _regional_access_boundary_utils, + credentials, + exceptions, + iam, + jwt, + metrics, +) from google.oauth2 import _client if TYPE_CHECKING: # pragma: NO COVER @@ -352,7 +353,8 @@ def _perform_refresh_token(self, request): ) def _build_regional_access_boundary_lookup_url( - self, request: "Optional[google.auth.transport.Request]" = None # noqa: F821 + self, + request: "Optional[google.auth.transport.Request]" = None, # noqa: F821 ): """Builds and returns the URL for the Regional Access Boundary lookup API. @@ -569,7 +571,7 @@ def __init__( if not isinstance(target_credentials, Credentials): raise exceptions.GoogleAuthError( - "Provided Credential must be " "impersonated_credentials" + "Provided Credential must be impersonated_credentials" ) self._target_credentials = target_credentials self._target_audience = target_audience diff --git a/packages/google-auth/google/auth/jwt.py b/packages/google-auth/google/auth/jwt.py index 754079eb796fb..9eff792e7bc2a 100644 --- a/packages/google-auth/google/auth/jwt.py +++ b/packages/google-auth/google/auth/jwt.py @@ -50,13 +50,15 @@ import json import urllib -from google.auth import _cache -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import _service_account_info -from google.auth import crypt -from google.auth import exceptions import google.auth.credentials +from google.auth import ( + _cache, + _helpers, + _regional_access_boundary_utils, + _service_account_info, + crypt, + exceptions, +) try: from google.auth.crypt import es diff --git a/packages/google-auth/google/auth/metrics.py b/packages/google-auth/google/auth/metrics.py index 89a15d740d7eb..c8b09b8cabd16 100644 --- a/packages/google-auth/google/auth/metrics.py +++ b/packages/google-auth/google/auth/metrics.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -""" We use x-goog-api-client header to report metrics. This module provides +"""We use x-goog-api-client header to report metrics. This module provides the constants and helper methods to construct x-goog-api-client header. """ @@ -20,7 +20,6 @@ from google.auth import version - API_CLIENT_HEADER = "x-goog-api-client" # BYOID Specific consts diff --git a/packages/google-auth/google/auth/pluggable.py b/packages/google-auth/google/auth/pluggable.py index b7d832da9a4c9..1dc6f0252ce4d 100644 --- a/packages/google-auth/google/auth/pluggable.py +++ b/packages/google-auth/google/auth/pluggable.py @@ -42,9 +42,7 @@ import sys import time -from google.auth import _helpers -from google.auth import exceptions -from google.auth import external_account +from google.auth import _helpers, exceptions, external_account # The max supported executable spec version. EXECUTABLE_SUPPORTED_MAX_VERSION = 1 @@ -76,7 +74,7 @@ def __init__( token_url, credential_source, *args, - **kwargs + **kwargs, ): """Instantiates an external account credentials object from a executables. @@ -118,7 +116,7 @@ def __init__( token_url=token_url, credential_source=credential_source, *args, - **kwargs + **kwargs, ) if not isinstance(credential_source, Mapping): self._credential_source_executable = None @@ -352,13 +350,13 @@ def _inject_env_variables(self, env): env["GOOGLE_EXTERNAL_ACCOUNT_INTERACTIVE"] = "1" if self.interactive else "0" if self._service_account_impersonation_url is not None: - env[ - "GOOGLE_EXTERNAL_ACCOUNT_IMPERSONATED_EMAIL" - ] = self.service_account_email + env["GOOGLE_EXTERNAL_ACCOUNT_IMPERSONATED_EMAIL"] = ( + self.service_account_email + ) if self._credential_source_executable_output_file is not None: - env[ - "GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE" - ] = self._credential_source_executable_output_file + env["GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILE"] = ( + self._credential_source_executable_output_file + ) def _parse_subject_token(self, response): self._validate_response_schema(response) diff --git a/packages/google-auth/google/auth/transport/_aiohttp_requests.py b/packages/google-auth/google/auth/transport/_aiohttp_requests.py index 0360cb40bab61..0513602bf69fb 100644 --- a/packages/google-auth/google/auth/transport/_aiohttp_requests.py +++ b/packages/google-auth/google/auth/transport/_aiohttp_requests.py @@ -27,13 +27,10 @@ import aiohttp # type: ignore import urllib3 # type: ignore -from google.auth import _helpers -from google.auth import exceptions -from google.auth import transport +from google.auth import _helpers, exceptions, transport from google.auth.aio import _helpers as _helpers_async from google.auth.transport import requests - _LOGGER = logging.getLogger(__name__) diff --git a/packages/google-auth/google/auth/transport/_http_client.py b/packages/google-auth/google/auth/transport/_http_client.py index bcfc2b27cb8ee..a7c40853b13bd 100644 --- a/packages/google-auth/google/auth/transport/_http_client.py +++ b/packages/google-auth/google/auth/transport/_http_client.py @@ -19,9 +19,7 @@ import socket import urllib -from google.auth import _helpers -from google.auth import exceptions -from google.auth import transport +from google.auth import _helpers, exceptions, transport _LOGGER = logging.getLogger(__name__) diff --git a/packages/google-auth/google/auth/transport/_mtls_helper.py b/packages/google-auth/google/auth/transport/_mtls_helper.py index 7779c484c7137..a393bc414843a 100644 --- a/packages/google-auth/google/auth/transport/_mtls_helper.py +++ b/packages/google-auth/google/auth/transport/_mtls_helper.py @@ -18,17 +18,14 @@ import json import logging import os -from os import environ, getenv, path import re import subprocess import sys import tempfile -from typing import cast, Generator, List, Optional, Tuple, Union +from os import environ, getenv, path +from typing import Generator, List, Optional, Tuple, Union, cast -from google.auth import _agent_identity_utils -from google.auth import _cloud_sdk -from google.auth import environment_vars -from google.auth import exceptions +from google.auth import _agent_identity_utils, _cloud_sdk, environment_vars, exceptions CONTEXT_AWARE_METADATA_PATH = "~/.secureConnect/context_aware_metadata.json" @@ -132,9 +129,11 @@ def secure_cert_key_paths( key_path is None or os.path.exists(key_path) ): if _can_read(cert_path) and _can_read(key_path): - yield cast(str, cert_path or cert), cast( - str, key_path or key - ), passphrase + yield ( + cast(str, cert_path or cert), + cast(str, key_path or key), + passphrase, + ) return except _MemfdCreationError: pass # Fallback to Tier 3 on failure. @@ -159,9 +158,10 @@ def _encrypt_key_if_plaintext( returned as-is (plaintext) as a fallback. This allows the caller (underlying SSL context) to attempt loading the key directly and handle any failures. """ + import secrets + import cryptography from cryptography.hazmat.primitives import serialization - import secrets try: pkey = serialization.load_pem_private_key(key_bytes, password=None) diff --git a/packages/google-auth/google/auth/transport/_requests_base.py b/packages/google-auth/google/auth/transport/_requests_base.py index 0608223d8c20c..650c4a0656e60 100644 --- a/packages/google-auth/google/auth/transport/_requests_base.py +++ b/packages/google-auth/google/auth/transport/_requests_base.py @@ -18,7 +18,6 @@ import abc - _DEFAULT_TIMEOUT = 120 # in second @@ -44,7 +43,7 @@ def request( headers=None, max_allowed_time=None, timeout=_DEFAULT_TIMEOUT, - **kwargs + **kwargs, ): raise NotImplementedError("Request must be implemented") diff --git a/packages/google-auth/google/auth/transport/grpc.py b/packages/google-auth/google/auth/transport/grpc.py index df6e5fa828829..df1117b91a1fb 100644 --- a/packages/google-auth/google/auth/transport/grpc.py +++ b/packages/google-auth/google/auth/transport/grpc.py @@ -20,8 +20,7 @@ import warnings from google.auth import exceptions -from google.auth.transport import _mtls_helper -from google.auth.transport import mtls +from google.auth.transport import _mtls_helper, mtls from google.oauth2 import service_account try: @@ -129,7 +128,7 @@ def secure_authorized_channel( target, ssl_credentials=None, client_cert_callback=None, - **kwargs + **kwargs, ): """Creates a secure authorized gRPC channel. diff --git a/packages/google-auth/google/auth/transport/mtls.py b/packages/google-auth/google/auth/transport/mtls.py index bc92b4295eaf9..54b675b3a1fa2 100644 --- a/packages/google-auth/google/auth/transport/mtls.py +++ b/packages/google-auth/google/auth/transport/mtls.py @@ -16,12 +16,11 @@ import enum import logging -from os import getenv import ssl +from os import getenv from typing import Optional -from google.auth import environment_vars -from google.auth import exceptions +from google.auth import environment_vars, exceptions from google.auth.transport import _mtls_helper _LOGGER = logging.getLogger(__name__) diff --git a/packages/google-auth/google/auth/transport/requests.py b/packages/google-auth/google/auth/transport/requests.py index 822cf687f5d09..94de63f7fd0be 100644 --- a/packages/google-auth/google/auth/transport/requests.py +++ b/packages/google-auth/google/auth/transport/requests.py @@ -35,11 +35,9 @@ create_urllib3_context, ) # pylint: disable=ungrouped-imports -from google.auth import _helpers -from google.auth import exceptions -from google.auth import transport -from google.auth.transport import _mtls_helper import google.auth.transport._mtls_helper +from google.auth import _helpers, exceptions, transport +from google.auth.transport import _mtls_helper from google.oauth2 import service_account _LOGGER = logging.getLogger(__name__) @@ -161,7 +159,7 @@ def __call__( body=None, headers=None, timeout=_DEFAULT_TIMEOUT, - **kwargs + **kwargs, ): """Make an HTTP request using requests. @@ -209,9 +207,10 @@ class _MutualTlsAdapter(requests.adapters.HTTPAdapter): """ def __init__(self, cert, key, **kwargs): - import certifi import ssl + import certifi + ctx_poolmanager = create_urllib3_context() ctx_poolmanager.load_verify_locations(cafile=certifi.where()) @@ -285,6 +284,7 @@ class _MutualTlsOffloadAdapter(requests.adapters.HTTPAdapter): def __init__(self, enterprise_cert_file_path): import certifi + from google.auth.transport import _custom_tls_signer self.signer = _custom_tls_signer.CustomTlsSigner(enterprise_cert_file_path) @@ -570,7 +570,7 @@ def request( headers=None, max_allowed_time=None, timeout=_DEFAULT_TIMEOUT, - **kwargs + **kwargs, ): """Implementation of Requests' request. @@ -632,7 +632,7 @@ def request( data=data, headers=request_headers, timeout=timeout, - **kwargs + **kwargs, ) remaining_time = guard.remaining_timeout @@ -711,7 +711,7 @@ def request( max_allowed_time=remaining_time, timeout=timeout, _credential_refresh_attempt=_credential_refresh_attempt + 1, - **kwargs + **kwargs, ) return response diff --git a/packages/google-auth/google/auth/transport/urllib3.py b/packages/google-auth/google/auth/transport/urllib3.py index 18e6128e03bd8..801309e9b64e3 100644 --- a/packages/google-auth/google/auth/transport/urllib3.py +++ b/packages/google-auth/google/auth/transport/urllib3.py @@ -50,9 +50,7 @@ ) from caught_exc -from google.auth import _helpers -from google.auth import exceptions -from google.auth import transport +from google.auth import _helpers, exceptions, transport from google.auth.transport import _mtls_helper from google.oauth2 import service_account @@ -176,9 +174,10 @@ def _make_mutual_tls_http(cert, key): Raises: google.auth.exceptions.MutualTLSChannelError: If the cert or key is invalid. """ - import certifi import ssl + import certifi + ctx = urllib3.util.ssl_.create_urllib3_context() ctx.load_verify_locations(cafile=certifi.where()) diff --git a/packages/google-auth/google/oauth2/_client.py b/packages/google-auth/google/oauth2/_client.py index 464849e4f3e0d..1afa7ab55d325 100644 --- a/packages/google-auth/google/oauth2/_client.py +++ b/packages/google-auth/google/oauth2/_client.py @@ -29,13 +29,15 @@ import logging import urllib -from google.auth import _exponential_backoff -from google.auth import _helpers -from google.auth import credentials -from google.auth import exceptions -from google.auth import jwt -from google.auth import metrics -from google.auth import transport +from google.auth import ( + _exponential_backoff, + _helpers, + credentials, + exceptions, + jwt, + metrics, + transport, +) _LOGGER = logging.getLogger(__name__) @@ -145,7 +147,7 @@ def _token_endpoint_request_no_throw( use_json=False, can_retry=True, headers=None, - **kwargs + **kwargs, ): """Makes a request to the OAuth 2.0 authorization server's token endpoint. This function doesn't throw on response errors. @@ -229,7 +231,7 @@ def _token_endpoint_request( use_json=False, can_retry=True, headers=None, - **kwargs + **kwargs, ): """Makes a request to the OAuth 2.0 authorization server's token endpoint. @@ -272,7 +274,7 @@ def _token_endpoint_request( use_json=use_json, can_retry=can_retry, headers=headers, - **kwargs + **kwargs, ) if not response_status_ok: _handle_error_response(response_data, retryable_error) diff --git a/packages/google-auth/google/oauth2/_client_async.py b/packages/google-auth/google/oauth2/_client_async.py index 6e921d23f9aa1..1cb3ee0243f52 100644 --- a/packages/google-auth/google/oauth2/_client_async.py +++ b/packages/google-auth/google/oauth2/_client_async.py @@ -28,10 +28,7 @@ import json import urllib -from google.auth import _exponential_backoff -from google.auth import _helpers -from google.auth import exceptions -from google.auth import jwt +from google.auth import _exponential_backoff, _helpers, exceptions, jwt from google.oauth2 import _client as client diff --git a/packages/google-auth/google/oauth2/_credentials_async.py b/packages/google-auth/google/oauth2/_credentials_async.py index b5561aae02293..06641e37cf10b 100644 --- a/packages/google-auth/google/oauth2/_credentials_async.py +++ b/packages/google-auth/google/oauth2/_credentials_async.py @@ -32,8 +32,7 @@ """ from google.auth import _credentials_async as credentials -from google.auth import _helpers -from google.auth import exceptions +from google.auth import _helpers, exceptions from google.oauth2 import _reauth_async as reauth from google.oauth2 import credentials as oauth2_credentials diff --git a/packages/google-auth/google/oauth2/_id_token_async.py b/packages/google-auth/google/oauth2/_id_token_async.py index a7f77a1c785b8..38d79ef5d6124 100644 --- a/packages/google-auth/google/oauth2/_id_token_async.py +++ b/packages/google-auth/google/oauth2/_id_token_async.py @@ -62,9 +62,7 @@ import json import os -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import jwt +from google.auth import environment_vars, exceptions, jwt from google.auth.transport import requests from google.oauth2 import id_token as sync_id_token diff --git a/packages/google-auth/google/oauth2/_reauth_async.py b/packages/google-auth/google/oauth2/_reauth_async.py index eeb8e9fb02739..516e8641c556b 100644 --- a/packages/google-auth/google/oauth2/_reauth_async.py +++ b/packages/google-auth/google/oauth2/_reauth_async.py @@ -35,10 +35,7 @@ import sys from google.auth import exceptions -from google.oauth2 import _client -from google.oauth2 import _client_async -from google.oauth2 import challenges -from google.oauth2 import reauth +from google.oauth2 import _client, _client_async, challenges, reauth async def _get_challenges( diff --git a/packages/google-auth/google/oauth2/_service_account_async.py b/packages/google-auth/google/oauth2/_service_account_async.py index 69b80a2531d26..50ba8e82530c1 100644 --- a/packages/google-auth/google/oauth2/_service_account_async.py +++ b/packages/google-auth/google/oauth2/_service_account_async.py @@ -23,10 +23,8 @@ """ from google.auth import _credentials_async as credentials_async -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.oauth2 import _client_async -from google.oauth2 import service_account +from google.auth import _helpers, _regional_access_boundary_utils +from google.oauth2 import _client_async, service_account class Credentials( diff --git a/packages/google-auth/google/oauth2/challenges.py b/packages/google-auth/google/oauth2/challenges.py index 59a2f9be4f43b..9f06ad9ef13e6 100644 --- a/packages/google-auth/google/oauth2/challenges.py +++ b/packages/google-auth/google/oauth2/challenges.py @@ -12,16 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -""" Challenges for reauthentication. -""" +"""Challenges for reauthentication.""" import abc import base64 import getpass import sys -from google.auth import _helpers -from google.auth import exceptions +from google.auth import _helpers, exceptions from google.oauth2 import webauthn_handler_factory from google.oauth2.webauthn_types import ( AuthenticationExtensionsClientInputs, @@ -29,7 +27,6 @@ PublicKeyCredentialDescriptor, ) - REAUTH_ORIGIN = "https://accounts.google.com" SAML_CHALLENGE_MESSAGE = ( "Please run `gcloud auth login` to complete reauthentication with SAML." diff --git a/packages/google-auth/google/oauth2/credentials.py b/packages/google-auth/google/oauth2/credentials.py index 5edea697bfdc1..cff8647d3bd9d 100644 --- a/packages/google-auth/google/oauth2/credentials.py +++ b/packages/google-auth/google/oauth2/credentials.py @@ -31,18 +31,20 @@ .. _rfc6749 section 4.1: https://tools.ietf.org/html/rfc6749#section-4.1 """ -from datetime import datetime import io import json import logging import warnings +from datetime import datetime -from google.auth import _cloud_sdk -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import credentials -from google.auth import exceptions -from google.auth import metrics +from google.auth import ( + _cloud_sdk, + _helpers, + _regional_access_boundary_utils, + credentials, + exceptions, + metrics, +) from google.oauth2 import reauth _LOGGER = logging.getLogger(__name__) diff --git a/packages/google-auth/google/oauth2/gdch_credentials.py b/packages/google-auth/google/oauth2/gdch_credentials.py index 7410cfc2e05ea..be94b2360ab66 100644 --- a/packages/google-auth/google/oauth2/gdch_credentials.py +++ b/packages/google-auth/google/oauth2/gdch_credentials.py @@ -12,19 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Experimental GDCH credentials support. -""" +"""Experimental GDCH credentials support.""" import datetime -from google.auth import _helpers -from google.auth import _service_account_info -from google.auth import credentials -from google.auth import exceptions -from google.auth import jwt +from google.auth import _helpers, _service_account_info, credentials, exceptions, jwt from google.oauth2 import _client - TOKEN_EXCHANGE_TYPE = "urn:ietf:params:oauth:token-type:token-exchange" ACCESS_TOKEN_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token" SERVICE_ACCOUNT_TOKEN_TYPE = "urn:k8s:params:oauth:token-type:serviceaccount" diff --git a/packages/google-auth/google/oauth2/id_token.py b/packages/google-auth/google/oauth2/id_token.py index 729d4376fe070..1cab9440a61ae 100644 --- a/packages/google-auth/google/oauth2/id_token.py +++ b/packages/google-auth/google/oauth2/id_token.py @@ -54,6 +54,7 @@ http://openid.net/specs/openid-connect-core-1_0.html#IDToken .. _CacheControl: https://cachecontrol.readthedocs.io """ + from __future__ import annotations import http.client as http_client @@ -61,11 +62,7 @@ import os from typing import Any, Mapping, Union -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import jwt -from google.auth import transport - +from google.auth import environment_vars, exceptions, jwt, transport # The URL that provides public certificates for verifying ID tokens issued # by Google's OAuth 2.0 authorization server. diff --git a/packages/google-auth/google/oauth2/reauth.py b/packages/google-auth/google/oauth2/reauth.py index abf691d58a640..270e3d5745b06 100644 --- a/packages/google-auth/google/oauth2/reauth.py +++ b/packages/google-auth/google/oauth2/reauth.py @@ -34,11 +34,8 @@ import sys -from google.auth import exceptions -from google.auth import metrics -from google.oauth2 import _client -from google.oauth2 import challenges - +from google.auth import exceptions, metrics +from google.oauth2 import _client, challenges _REAUTH_SCOPE = "https://www.googleapis.com/auth/accounts.reauth" _REAUTH_API = "https://reauth.googleapis.com/v2/sessions" diff --git a/packages/google-auth/google/oauth2/service_account.py b/packages/google-auth/google/oauth2/service_account.py index 0abc78b5000d3..6744e6f14d9b9 100644 --- a/packages/google-auth/google/oauth2/service_account.py +++ b/packages/google-auth/google/oauth2/service_account.py @@ -73,17 +73,18 @@ import copy import datetime import logging -from typing import Optional, TYPE_CHECKING - - -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import _service_account_info -from google.auth import credentials -from google.auth import exceptions -from google.auth import iam -from google.auth import jwt -from google.auth import metrics +from typing import TYPE_CHECKING, Optional + +from google.auth import ( + _helpers, + _regional_access_boundary_utils, + _service_account_info, + credentials, + exceptions, + iam, + jwt, + metrics, +) from google.oauth2 import _client if TYPE_CHECKING: # pragma: NO COVER @@ -504,7 +505,8 @@ def _create_self_signed_jwt(self, audience): ) def _build_regional_access_boundary_lookup_url( - self, request: "Optional[google.auth.transport.Request]" = None # noqa: F821 + self, + request: "Optional[google.auth.transport.Request]" = None, # noqa: F821 ): """Builds and returns the URL for the Regional Access Boundary lookup API. diff --git a/packages/google-auth/google/oauth2/sts.py b/packages/google-auth/google/oauth2/sts.py index 60d6f83c4d9f2..a48db0a4580b4 100644 --- a/packages/google-auth/google/oauth2/sts.py +++ b/packages/google-auth/google/oauth2/sts.py @@ -37,7 +37,6 @@ from google.oauth2 import utils - _URLENCODED_HEADERS = {"Content-Type": "application/x-www-form-urlencoded"} diff --git a/packages/google-auth/google/oauth2/webauthn_types.py b/packages/google-auth/google/oauth2/webauthn_types.py index 24e984f3d3363..d69566ec6b77a 100644 --- a/packages/google-auth/google/oauth2/webauthn_types.py +++ b/packages/google-auth/google/oauth2/webauthn_types.py @@ -1,5 +1,5 @@ -from dataclasses import dataclass import json +from dataclasses import dataclass from typing import Any, Dict, List, Optional from google.auth import exceptions diff --git a/packages/google-auth/noxfile.py b/packages/google-auth/noxfile.py index 86a31f3b46607..0f0654a4ddaec 100644 --- a/packages/google-auth/noxfile.py +++ b/packages/google-auth/noxfile.py @@ -83,15 +83,29 @@ @nox.session(python=DEFAULT_PYTHON_VERSION) def lint(session): - session.install( - "flake8", "flake8-import-order", "docutils", CLICK_VERSION, BLACK_VERSION - ) + session.install("setuptools", "flake8", "docutils", CLICK_VERSION, RUFF_VERSION) session.install("-e", ".") - session.run("black", "--check", *BLACK_PATHS) + # 1. Check imports + session.run( + "ruff", + "check", + "--select", + "I", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", + *BLACK_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", + "--check", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", + *BLACK_PATHS, + ) session.run( "flake8", - "--import-order-style=google", - "--application-import-names=google,tests,system_tests", "google", "tests", "tests_async", diff --git a/packages/google-auth/tests/compute_engine/test__metadata.py b/packages/google-auth/tests/compute_engine/test__metadata.py index 0fae4bd6ef163..0b151722af078 100644 --- a/packages/google-auth/tests/compute_engine/test__metadata.py +++ b/packages/google-auth/tests/compute_engine/test__metadata.py @@ -22,10 +22,7 @@ import pytest # type: ignore import requests -from google.auth import _helpers -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import transport +from google.auth import _helpers, environment_vars, exceptions, transport from google.auth.compute_engine import _metadata from google.auth.transport import requests as google_auth_requests diff --git a/packages/google-auth/tests/compute_engine/test_credentials.py b/packages/google-auth/tests/compute_engine/test_credentials.py index ab171c5a60414..6e30d8807bf33 100644 --- a/packages/google-auth/tests/compute_engine/test_credentials.py +++ b/packages/google-auth/tests/compute_engine/test_credentials.py @@ -19,10 +19,7 @@ import pytest # type: ignore import responses # type: ignore -from google.auth import _helpers -from google.auth import exceptions -from google.auth import jwt -from google.auth import transport +from google.auth import _helpers, exceptions, jwt, transport from google.auth.compute_engine import credentials from google.auth.transport import requests diff --git a/packages/google-auth/tests/crypt/test__cryptography_rsa.py b/packages/google-auth/tests/crypt/test__cryptography_rsa.py index 7f5406b626a65..ee57b7e1a4186 100644 --- a/packages/google-auth/tests/crypt/test__cryptography_rsa.py +++ b/packages/google-auth/tests/crypt/test__cryptography_rsa.py @@ -16,13 +16,11 @@ import os import pickle -from cryptography.hazmat.primitives.asymmetric import rsa import pytest # type: ignore +from cryptography.hazmat.primitives.asymmetric import rsa from google.auth import _helpers -from google.auth.crypt import _cryptography_rsa -from google.auth.crypt import base - +from google.auth.crypt import _cryptography_rsa, base DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") diff --git a/packages/google-auth/tests/crypt/test__python_rsa.py b/packages/google-auth/tests/crypt/test__python_rsa.py index 7d0b651e33802..3b5478524145d 100644 --- a/packages/google-auth/tests/crypt/test__python_rsa.py +++ b/packages/google-auth/tests/crypt/test__python_rsa.py @@ -17,8 +17,8 @@ import os from unittest import mock -from pyasn1_modules import pem # type: ignore import pytest # type: ignore +from pyasn1_modules import pem # type: ignore try: import rsa @@ -26,9 +26,7 @@ pytest.skip("rsa module not available", allow_module_level=True) from google.auth import _helpers -from google.auth.crypt import _python_rsa -from google.auth.crypt import base - +from google.auth.crypt import _python_rsa, base DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") diff --git a/packages/google-auth/tests/crypt/test_crypt.py b/packages/google-auth/tests/crypt/test_crypt.py index e80502e9be592..dad9c4465cde2 100644 --- a/packages/google-auth/tests/crypt/test_crypt.py +++ b/packages/google-auth/tests/crypt/test_crypt.py @@ -16,7 +16,6 @@ from google.auth import crypt - DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") # To generate privatekey.pem, privatekey.pub, and public_cert.pem: diff --git a/packages/google-auth/tests/crypt/test_es.py b/packages/google-auth/tests/crypt/test_es.py index fdbf9d2edfefd..2afcffa63bb42 100644 --- a/packages/google-auth/tests/crypt/test_es.py +++ b/packages/google-auth/tests/crypt/test_es.py @@ -17,13 +17,11 @@ import os import pickle -from cryptography.hazmat.primitives.asymmetric import ec import pytest # type: ignore +from cryptography.hazmat.primitives.asymmetric import ec from google.auth import _helpers -from google.auth.crypt import base -from google.auth.crypt import es - +from google.auth.crypt import base, es DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") diff --git a/packages/google-auth/tests/crypt/test_es256.py b/packages/google-auth/tests/crypt/test_es256.py index fa5070f89d55d..a465beb4e7dab 100644 --- a/packages/google-auth/tests/crypt/test_es256.py +++ b/packages/google-auth/tests/crypt/test_es256.py @@ -17,13 +17,11 @@ import os import pickle -from cryptography.hazmat.primitives.asymmetric import ec import pytest # type: ignore +from cryptography.hazmat.primitives.asymmetric import ec from google.auth import _helpers -from google.auth.crypt import base -from google.auth.crypt import es256 - +from google.auth.crypt import base, es256 DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") diff --git a/packages/google-auth/tests/crypt/test_rsa.py b/packages/google-auth/tests/crypt/test_rsa.py index a54beb7632cfd..26390a713d7e3 100644 --- a/packages/google-auth/tests/crypt/test_rsa.py +++ b/packages/google-auth/tests/crypt/test_rsa.py @@ -15,20 +15,19 @@ import os from unittest import mock +import pytest from cryptography.hazmat import backends from cryptography.hazmat.primitives import serialization -import pytest try: import rsa as rsa_lib + from google.auth.crypt import _python_rsa except ImportError: rsa_lib = None # type: ignore _pyrhon_rsa = None # type: ignore -from google.auth.crypt import _cryptography_rsa -from google.auth.crypt import rsa - +from google.auth.crypt import _cryptography_rsa, rsa DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") diff --git a/packages/google-auth/tests/oauth2/test__client.py b/packages/google-auth/tests/oauth2/test__client.py index 0d17b33178565..7ccac8a4dafa9 100644 --- a/packages/google-auth/tests/oauth2/test__client.py +++ b/packages/google-auth/tests/oauth2/test__client.py @@ -16,20 +16,14 @@ import http.client as http_client import json import os -from unittest import mock import urllib +from unittest import mock import pytest # type: ignore -from google.auth import _helpers -from google.auth import crypt -from google.auth import exceptions -from google.auth import iam -from google.auth import jwt -from google.auth import transport +from google.auth import _helpers, crypt, exceptions, iam, jwt, transport from google.oauth2 import _client - DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh: diff --git a/packages/google-auth/tests/oauth2/test_credentials.py b/packages/google-auth/tests/oauth2/test_credentials.py index 43df9b3a0cc4e..fc36f911bad98 100644 --- a/packages/google-auth/tests/oauth2/test_credentials.py +++ b/packages/google-auth/tests/oauth2/test_credentials.py @@ -21,13 +21,10 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import exceptions -from google.auth import transport +from google.auth import _helpers, exceptions, transport from google.auth.credentials import TokenState from google.oauth2 import credentials - DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") AUTH_USER_JSON_FILE = os.path.join(DATA_DIR, "authorized_user.json") diff --git a/packages/google-auth/tests/oauth2/test_gdch_credentials.py b/packages/google-auth/tests/oauth2/test_gdch_credentials.py index b889744e60bad..e147401c84924 100644 --- a/packages/google-auth/tests/oauth2/test_gdch_credentials.py +++ b/packages/google-auth/tests/oauth2/test_gdch_credentials.py @@ -21,9 +21,8 @@ import pytest # type: ignore import requests -from google.auth import exceptions -from google.auth import jwt import google.auth.transport.requests +from google.auth import exceptions, jwt from google.oauth2 import gdch_credentials from google.oauth2.gdch_credentials import ServiceAccountCredentials diff --git a/packages/google-auth/tests/oauth2/test_id_token.py b/packages/google-auth/tests/oauth2/test_id_token.py index 09e0a6c884038..456ca58b89f64 100644 --- a/packages/google-auth/tests/oauth2/test_id_token.py +++ b/packages/google-auth/tests/oauth2/test_id_token.py @@ -18,12 +18,13 @@ import pytest # type: ignore -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import impersonated_credentials -from google.auth import transport -from google.oauth2 import id_token -from google.oauth2 import service_account +from google.auth import ( + environment_vars, + exceptions, + impersonated_credentials, + transport, +) +from google.oauth2 import id_token, service_account SERVICE_ACCOUNT_FILE = os.path.join( os.path.dirname(__file__), "../data/service_account.json" diff --git a/packages/google-auth/tests/oauth2/test_reauth.py b/packages/google-auth/tests/oauth2/test_reauth.py index 0949def39528a..74cf6d9443f2e 100644 --- a/packages/google-auth/tests/oauth2/test_reauth.py +++ b/packages/google-auth/tests/oauth2/test_reauth.py @@ -20,7 +20,6 @@ from google.auth import exceptions from google.oauth2 import reauth - MOCK_REQUEST = mock.Mock() CHALLENGES_RESPONSE_TEMPLATE = { "status": "CHALLENGE_REQUIRED", diff --git a/packages/google-auth/tests/oauth2/test_service_account.py b/packages/google-auth/tests/oauth2/test_service_account.py index 9573aad7e970b..84074ec37b9a1 100644 --- a/packages/google-auth/tests/oauth2/test_service_account.py +++ b/packages/google-auth/tests/oauth2/test_service_account.py @@ -19,16 +19,10 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import crypt -from google.auth import exceptions -from google.auth import iam -from google.auth import jwt -from google.auth import transport +from google.auth import _helpers, crypt, exceptions, iam, jwt, transport from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN from google.oauth2 import service_account - DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data") with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh: diff --git a/packages/google-auth/tests/oauth2/test_sts.py b/packages/google-auth/tests/oauth2/test_sts.py index 5d0f557e3c843..acbe485f3d2f7 100644 --- a/packages/google-auth/tests/oauth2/test_sts.py +++ b/packages/google-auth/tests/oauth2/test_sts.py @@ -14,15 +14,13 @@ import http.client as http_client import json -from unittest import mock import urllib +from unittest import mock import pytest # type: ignore -from google.auth import exceptions -from google.auth import transport -from google.oauth2 import sts -from google.oauth2 import utils +from google.auth import exceptions, transport +from google.oauth2 import sts, utils CLIENT_ID = "username" CLIENT_SECRET = "password" diff --git a/packages/google-auth/tests/oauth2/test_utils.py b/packages/google-auth/tests/oauth2/test_utils.py index 543a693a98bfe..ea845aa6046a4 100644 --- a/packages/google-auth/tests/oauth2/test_utils.py +++ b/packages/google-auth/tests/oauth2/test_utils.py @@ -19,7 +19,6 @@ from google.auth import exceptions from google.oauth2 import utils - CLIENT_ID = "username" CLIENT_SECRET = "password" # Base64 encoding of "username:password" diff --git a/packages/google-auth/tests/oauth2/test_webauthn_handler.py b/packages/google-auth/tests/oauth2/test_webauthn_handler.py index 15261aa1647a8..0c92a24807b8c 100644 --- a/packages/google-auth/tests/oauth2/test_webauthn_handler.py +++ b/packages/google-auth/tests/oauth2/test_webauthn_handler.py @@ -5,8 +5,7 @@ import pytest # type: ignore from google.auth import exceptions -from google.oauth2 import webauthn_handler -from google.oauth2 import webauthn_types +from google.oauth2 import webauthn_handler, webauthn_types @pytest.fixture diff --git a/packages/google-auth/tests/oauth2/test_webauthn_handler_factory.py b/packages/google-auth/tests/oauth2/test_webauthn_handler_factory.py index 1323de9096f9f..52e21cd450201 100644 --- a/packages/google-auth/tests/oauth2/test_webauthn_handler_factory.py +++ b/packages/google-auth/tests/oauth2/test_webauthn_handler_factory.py @@ -2,8 +2,7 @@ import pytest # type: ignore -from google.oauth2 import webauthn_handler -from google.oauth2 import webauthn_handler_factory +from google.oauth2 import webauthn_handler, webauthn_handler_factory @pytest.fixture diff --git a/packages/google-auth/tests/test__cloud_sdk.py b/packages/google-auth/tests/test__cloud_sdk.py index dd14bcebe5971..6865d0d9e76d5 100644 --- a/packages/google-auth/tests/test__cloud_sdk.py +++ b/packages/google-auth/tests/test__cloud_sdk.py @@ -21,10 +21,7 @@ import pytest # type: ignore -from google.auth import _cloud_sdk -from google.auth import environment_vars -from google.auth import exceptions - +from google.auth import _cloud_sdk, environment_vars, exceptions DATA_DIR = os.path.join(os.path.dirname(__file__), "data") AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json") diff --git a/packages/google-auth/tests/test__default.py b/packages/google-auth/tests/test__default.py index fa2de3b9084eb..f1a71143dda6b 100644 --- a/packages/google-auth/tests/test__default.py +++ b/packages/google-auth/tests/test__default.py @@ -15,28 +15,28 @@ import json import os import sys -from unittest import mock import warnings +from unittest import mock import pytest # type: ignore -from google.auth import _default -from google.auth import api_key -from google.auth import app_engine -from google.auth import aws -from google.auth import compute_engine -from google.auth import credentials -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import external_account -from google.auth import external_account_authorized_user -from google.auth import identity_pool -from google.auth import impersonated_credentials -from google.auth import pluggable -from google.oauth2 import gdch_credentials -from google.oauth2 import service_account import google.oauth2.credentials - +from google.auth import ( + _default, + api_key, + app_engine, + aws, + compute_engine, + credentials, + environment_vars, + exceptions, + external_account, + external_account_authorized_user, + identity_pool, + impersonated_credentials, + pluggable, +) +from google.oauth2 import gdch_credentials, service_account DATA_DIR = os.path.join(os.path.dirname(__file__), "data") AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json") @@ -404,9 +404,9 @@ def test_load_credentials_from_file_impersonated_passing_scopes(): def test_load_credentials_from_file_impersonated_wrong_target_principal(tmpdir): with open(IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE) as fh: impersonated_credentials_info = json.load(fh) - impersonated_credentials_info[ - "service_account_impersonation_url" - ] = "something_wrong" + impersonated_credentials_info["service_account_impersonation_url"] = ( + "something_wrong" + ) jsonfile = tmpdir.join("invalid.json") jsonfile.write(json.dumps(impersonated_credentials_info)) @@ -774,9 +774,9 @@ def test__get_gae_credentials_gen1(app_identity): @mock.patch.dict(os.environ) def test__get_gae_credentials_gen2(): - os.environ[ - "GAE_RUNTIME" - ] = f"python{sys.version_info.major}{sys.version_info.minor}" + os.environ["GAE_RUNTIME"] = ( + f"python{sys.version_info.major}{sys.version_info.minor}" + ) credentials, project_id = _default._get_gae_credentials() assert credentials is None assert project_id is None diff --git a/packages/google-auth/tests/test__exponential_backoff.py b/packages/google-auth/tests/test__exponential_backoff.py index 55cb918dbc012..1d8ebee8343ea 100644 --- a/packages/google-auth/tests/test__exponential_backoff.py +++ b/packages/google-auth/tests/test__exponential_backoff.py @@ -16,8 +16,7 @@ import pytest # type: ignore -from google.auth import _exponential_backoff -from google.auth import exceptions +from google.auth import _exponential_backoff, exceptions @mock.patch("time.sleep", return_value=None) diff --git a/packages/google-auth/tests/test__helpers.py b/packages/google-auth/tests/test__helpers.py index e2d8e7b20c038..5755e2a357d24 100644 --- a/packages/google-auth/tests/test__helpers.py +++ b/packages/google-auth/tests/test__helpers.py @@ -15,8 +15,8 @@ import datetime import json import logging -from unittest import mock import urllib +from unittest import mock import pytest # type: ignore diff --git a/packages/google-auth/tests/test__oauth2client.py b/packages/google-auth/tests/test__oauth2client.py index 26ed0e4edafe5..3e238a5b9da3d 100644 --- a/packages/google-auth/tests/test__oauth2client.py +++ b/packages/google-auth/tests/test__oauth2client.py @@ -32,7 +32,6 @@ from google.auth import _oauth2client - DATA_DIR = os.path.join(os.path.dirname(__file__), "data") SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json") diff --git a/packages/google-auth/tests/test__regional_access_boundary_utils.py b/packages/google-auth/tests/test__regional_access_boundary_utils.py index 04fc5928ea83b..ae33cc4332630 100644 --- a/packages/google-auth/tests/test__regional_access_boundary_utils.py +++ b/packages/google-auth/tests/test__regional_access_boundary_utils.py @@ -18,10 +18,12 @@ import pytest # type: ignore -from google.auth import _credentials_async -from google.auth import _helpers -from google.auth import _regional_access_boundary_utils -from google.auth import credentials +from google.auth import ( + _credentials_async, + _helpers, + _regional_access_boundary_utils, + credentials, +) from google.oauth2 import credentials as oauth2_credentials diff --git a/packages/google-auth/tests/test__service_account_info.py b/packages/google-auth/tests/test__service_account_info.py index 7e836861e4a73..b81e27879e6b3 100644 --- a/packages/google-auth/tests/test__service_account_info.py +++ b/packages/google-auth/tests/test__service_account_info.py @@ -17,9 +17,7 @@ import pytest # type: ignore -from google.auth import _service_account_info -from google.auth import crypt - +from google.auth import _service_account_info, crypt DATA_DIR = os.path.join(os.path.dirname(__file__), "data") SERVICE_ACCOUNT_JSON_FILE = os.path.join(DATA_DIR, "service_account.json") diff --git a/packages/google-auth/tests/test_agent_identity_utils.py b/packages/google-auth/tests/test_agent_identity_utils.py index 6b830e048412c..7448dfa7aa0b2 100644 --- a/packages/google-auth/tests/test_agent_identity_utils.py +++ b/packages/google-auth/tests/test_agent_identity_utils.py @@ -16,11 +16,11 @@ import hashlib import json import os -from unittest import mock import urllib.parse +from unittest import mock -from cryptography import x509 import pytest +from cryptography import x509 from google.auth import _agent_identity_utils, environment_vars, exceptions diff --git a/packages/google-auth/tests/test_aws.py b/packages/google-auth/tests/test_aws.py index 8c09c5453f9f6..ccfd412f6cc94 100644 --- a/packages/google-auth/tests/test_aws.py +++ b/packages/google-auth/tests/test_aws.py @@ -16,16 +16,19 @@ import http.client as http_client import json import os -from unittest import mock import urllib.parse +from unittest import mock import pytest # type: ignore -from google.auth import _helpers, external_account -from google.auth import aws -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import transport +from google.auth import ( + _helpers, + aws, + environment_vars, + exceptions, + external_account, + transport, +) from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN IMPERSONATE_ACCESS_TOKEN_REQUEST_METRICS_HEADER_VALUE = "gl-python/ auth/ auth-request-type/at cred-type/imp" @@ -768,9 +771,11 @@ def make_serialized_aws_signed_request( } ) # Append x-goog-cloud-target-resource header. - reformatted_signed_request.get("headers").append( - {"key": "x-goog-cloud-target-resource", "value": AUDIENCE} - ), + ( + reformatted_signed_request.get("headers").append( + {"key": "x-goog-cloud-target-resource", "value": AUDIENCE} + ), + ) return urllib.parse.quote( json.dumps( reformatted_signed_request, separators=(",", ":"), sort_keys=True @@ -1364,9 +1369,9 @@ def test_retrieve_subject_token_success_temp_creds_no_environment_vars_idmsv2( imdsv2_session_token_data=self.AWS_IMDSV2_SESSION_TOKEN, ) credential_source_token_url = self.CREDENTIAL_SOURCE.copy() - credential_source_token_url[ - "imdsv2_session_token_url" - ] = IMDSV2_SESSION_TOKEN_URL + credential_source_token_url["imdsv2_session_token_url"] = ( + IMDSV2_SESSION_TOKEN_URL + ) credentials = self.make_credentials( credential_source=credential_source_token_url ) @@ -1470,9 +1475,9 @@ def test_retrieve_subject_token_success_temp_creds_environment_vars_missing_secr imdsv2_session_token_data=self.AWS_IMDSV2_SESSION_TOKEN, ) credential_source_token_url = self.CREDENTIAL_SOURCE.copy() - credential_source_token_url[ - "imdsv2_session_token_url" - ] = IMDSV2_SESSION_TOKEN_URL + credential_source_token_url["imdsv2_session_token_url"] = ( + IMDSV2_SESSION_TOKEN_URL + ) credentials = self.make_credentials( credential_source=credential_source_token_url ) @@ -1526,9 +1531,9 @@ def test_retrieve_subject_token_success_temp_creds_environment_vars_missing_acce imdsv2_session_token_data=self.AWS_IMDSV2_SESSION_TOKEN, ) credential_source_token_url = self.CREDENTIAL_SOURCE.copy() - credential_source_token_url[ - "imdsv2_session_token_url" - ] = IMDSV2_SESSION_TOKEN_URL + credential_source_token_url["imdsv2_session_token_url"] = ( + IMDSV2_SESSION_TOKEN_URL + ) credentials = self.make_credentials( credential_source=credential_source_token_url ) @@ -1576,9 +1581,9 @@ def test_retrieve_subject_token_success_temp_creds_environment_vars_missing_cred imdsv2_session_token_data=self.AWS_IMDSV2_SESSION_TOKEN, ) credential_source_token_url = self.CREDENTIAL_SOURCE.copy() - credential_source_token_url[ - "imdsv2_session_token_url" - ] = IMDSV2_SESSION_TOKEN_URL + credential_source_token_url["imdsv2_session_token_url"] = ( + IMDSV2_SESSION_TOKEN_URL + ) credentials = self.make_credentials( credential_source=credential_source_token_url ) @@ -1626,9 +1631,9 @@ def test_retrieve_subject_token_success_temp_creds_idmsv2(self, utcnow): role_status=http_client.OK, role_name=self.AWS_ROLE ) credential_source_token_url = self.CREDENTIAL_SOURCE.copy() - credential_source_token_url[ - "imdsv2_session_token_url" - ] = IMDSV2_SESSION_TOKEN_URL + credential_source_token_url["imdsv2_session_token_url"] = ( + IMDSV2_SESSION_TOKEN_URL + ) credentials = self.make_credentials( credential_source=credential_source_token_url ) @@ -1706,9 +1711,9 @@ def test_retrieve_subject_token_session_error_idmsv2(self, utcnow): imdsv2_session_token_data="unauthorized", ) credential_source_token_url = self.CREDENTIAL_SOURCE.copy() - credential_source_token_url[ - "imdsv2_session_token_url" - ] = IMDSV2_SESSION_TOKEN_URL + credential_source_token_url["imdsv2_session_token_url"] = ( + IMDSV2_SESSION_TOKEN_URL + ) credentials = self.make_credentials( credential_source=credential_source_token_url ) diff --git a/packages/google-auth/tests/test_credentials.py b/packages/google-auth/tests/test_credentials.py index 24cbb98afd949..95c8f2455e0e1 100644 --- a/packages/google-auth/tests/test_credentials.py +++ b/packages/google-auth/tests/test_credentials.py @@ -17,8 +17,7 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import credentials +from google.auth import _helpers, credentials class CredentialsImpl(credentials.CredentialsWithRegionalAccessBoundary): diff --git a/packages/google-auth/tests/test_downscoped.py b/packages/google-auth/tests/test_downscoped.py index 1982f3d8e53db..0607f65b028a0 100644 --- a/packages/google-auth/tests/test_downscoped.py +++ b/packages/google-auth/tests/test_downscoped.py @@ -15,19 +15,13 @@ import datetime import http.client as http_client import json -from unittest import mock import urllib +from unittest import mock import pytest # type: ignore -from google.auth import _helpers -from google.auth import credentials -from google.auth import downscoped -from google.auth import exceptions -from google.auth import transport -from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN -from google.auth.credentials import TokenState - +from google.auth import _helpers, credentials, downscoped, exceptions, transport +from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN, TokenState EXPRESSION = ( "resource.name.startsWith('projects/_/buckets/example-bucket/objects/customer-a')" diff --git a/packages/google-auth/tests/test_external_account.py b/packages/google-auth/tests/test_external_account.py index a637a95cf1682..064fb564b4a00 100644 --- a/packages/google-auth/tests/test_external_account.py +++ b/packages/google-auth/tests/test_external_account.py @@ -15,17 +15,13 @@ import datetime import http.client as http_client import json -from unittest import mock import urllib +from unittest import mock import pytest # type: ignore -from google.auth import _helpers -from google.auth import exceptions -from google.auth import external_account -from google.auth import transport -from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN -from google.auth.credentials import TokenState +from google.auth import _helpers, exceptions, external_account, transport +from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN, TokenState IMPERSONATE_ACCESS_TOKEN_REQUEST_METRICS_HEADER_VALUE = "gl-python/ auth/ auth-request-type/at cred-type/imp" LANG_LIBRARY_METRICS_HEADER_VALUE = "gl-python/ auth/" @@ -2397,10 +2393,11 @@ def test_invalid_configuration_raises_validation_error(self): ) def test_before_request_multithreaded_lazy_initialization(self): - from google.auth import identity_pool import threading import time + from google.auth import identity_pool + creds = identity_pool.Credentials( audience=self.AUDIENCE, subject_token_type=self.SUBJECT_TOKEN_TYPE, diff --git a/packages/google-auth/tests/test_external_account_authorized_user.py b/packages/google-auth/tests/test_external_account_authorized_user.py index 69a085e65df55..774485bec4438 100644 --- a/packages/google-auth/tests/test_external_account_authorized_user.py +++ b/packages/google-auth/tests/test_external_account_authorized_user.py @@ -19,9 +19,7 @@ import pytest # type: ignore -from google.auth import exceptions -from google.auth import external_account_authorized_user -from google.auth import transport +from google.auth import exceptions, external_account_authorized_user, transport from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN TOKEN_URL = "https://sts.googleapis.com/v1/token" @@ -57,7 +55,7 @@ def make_credentials( token_info_url=TOKEN_INFO_URL, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, - **kwargs + **kwargs, ): return external_account_authorized_user.Credentials( audience=audience, @@ -66,7 +64,7 @@ def make_credentials( token_info_url=token_info_url, client_id=client_id, client_secret=client_secret, - **kwargs + **kwargs, ) @classmethod diff --git a/packages/google-auth/tests/test_iam.py b/packages/google-auth/tests/test_iam.py index 26a4c825a7b35..51e045e9864d5 100644 --- a/packages/google-auth/tests/test_iam.py +++ b/packages/google-auth/tests/test_iam.py @@ -20,11 +20,8 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import exceptions -from google.auth import iam -from google.auth import transport import google.auth.credentials +from google.auth import _helpers, exceptions, iam, transport def make_request(status, data=None): diff --git a/packages/google-auth/tests/test_identity_pool.py b/packages/google-auth/tests/test_identity_pool.py index 1138db284db7f..7d6a7c1b68d8e 100644 --- a/packages/google-auth/tests/test_identity_pool.py +++ b/packages/google-auth/tests/test_identity_pool.py @@ -17,18 +17,21 @@ import http.client as http_client import json import os -from unittest import mock import urllib +from unittest import mock +import pytest # type: ignore from cryptography import x509 from cryptography.hazmat.primitives import serialization -import pytest # type: ignore -from google.auth import _helpers, external_account -from google.auth import exceptions -from google.auth import identity_pool -from google.auth import metrics -from google.auth import transport +from google.auth import ( + _helpers, + exceptions, + external_account, + identity_pool, + metrics, + transport, +) from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN CLIENT_ID = "username" diff --git a/packages/google-auth/tests/test_impersonated_credentials.py b/packages/google-auth/tests/test_impersonated_credentials.py index 215c68620aa05..2cfcbf150f860 100644 --- a/packages/google-auth/tests/test_impersonated_credentials.py +++ b/packages/google-auth/tests/test_impersonated_credentials.py @@ -21,14 +21,9 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import crypt -from google.auth import exceptions -from google.auth import impersonated_credentials -from google.auth import transport +from google.auth import _helpers, crypt, exceptions, impersonated_credentials, transport from google.auth.impersonated_credentials import Credentials -from google.oauth2 import credentials -from google.oauth2 import service_account +from google.oauth2 import credentials, service_account DATA_DIR = os.path.join(os.path.dirname(__file__), "", "data") @@ -967,7 +962,7 @@ def test_id_token_invalid_cred( with pytest.raises(exceptions.GoogleAuthError) as excinfo: impersonated_credentials.IDTokenCredentials(credentials) - assert excinfo.match("Provided Credential must be" " impersonated_credentials") + assert excinfo.match("Provided Credential must be impersonated_credentials") def test_id_token_with_include_email( self, mock_donor_credentials, mock_authorizedsession_idtoken diff --git a/packages/google-auth/tests/test_jwt.py b/packages/google-auth/tests/test_jwt.py index 8b90b0ecd093e..207648aa06fa5 100644 --- a/packages/google-auth/tests/test_jwt.py +++ b/packages/google-auth/tests/test_jwt.py @@ -20,11 +20,7 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import crypt -from google.auth import exceptions -from google.auth import jwt - +from google.auth import _helpers, crypt, exceptions, jwt DATA_DIR = os.path.join(os.path.dirname(__file__), "data") diff --git a/packages/google-auth/tests/test_metrics.py b/packages/google-auth/tests/test_metrics.py index dc8789b2fe346..31d4fd12f7d08 100644 --- a/packages/google-auth/tests/test_metrics.py +++ b/packages/google-auth/tests/test_metrics.py @@ -17,8 +17,7 @@ import pytest -from google.auth import metrics -from google.auth import version +from google.auth import metrics, version def test_add_metric_header(): diff --git a/packages/google-auth/tests/test_pluggable.py b/packages/google-auth/tests/test_pluggable.py index b2764361bd5df..f02b48dc71d2f 100644 --- a/packages/google-auth/tests/test_pluggable.py +++ b/packages/google-auth/tests/test_pluggable.py @@ -19,8 +19,7 @@ import pytest # type: ignore -from google.auth import exceptions -from google.auth import pluggable +from google.auth import exceptions, pluggable from google.auth.credentials import DEFAULT_UNIVERSE_DOMAIN from tests.test__default import WORKFORCE_AUDIENCE diff --git a/packages/google-auth/tests/transport/aio/test_aiohttp.py b/packages/google-auth/tests/transport/aio/test_aiohttp.py index 49a98d669ae31..94dcc2473bedf 100644 --- a/packages/google-auth/tests/transport/aio/test_aiohttp.py +++ b/packages/google-auth/tests/transport/aio/test_aiohttp.py @@ -15,13 +15,13 @@ import asyncio from unittest.mock import AsyncMock, Mock, patch -from aioresponses import aioresponses # type: ignore import pytest # type: ignore import pytest_asyncio # type: ignore +from aioresponses import aioresponses # type: ignore +import google.auth.aio.transport.aiohttp as auth_aiohttp from google.auth import exceptions from google.auth.aio import _helpers as _helpers_async -import google.auth.aio.transport.aiohttp as auth_aiohttp try: import aiohttp # type: ignore @@ -198,6 +198,7 @@ async def test_request_clone_closed_session_raises(self): async def test_request_clone_with_active_session(self): import ssl + from aiohttp import BasicAuth, ClientTimeout, TCPConnector custom_ssl = ssl.create_default_context() diff --git a/packages/google-auth/tests/transport/aio/test_sessions.py b/packages/google-auth/tests/transport/aio/test_sessions.py index de283b7b2e7f6..250a31c852c45 100644 --- a/packages/google-auth/tests/transport/aio/test_sessions.py +++ b/packages/google-auth/tests/transport/aio/test_sessions.py @@ -16,8 +16,8 @@ from typing import AsyncGenerator from unittest.mock import Mock, patch -from aioresponses import aioresponses # type: ignore import pytest # type: ignore +from aioresponses import aioresponses # type: ignore from google.auth.aio.credentials import AnonymousCredentials from google.auth.aio.transport import ( diff --git a/packages/google-auth/tests/transport/aio/test_sessions_mtls.py b/packages/google-auth/tests/transport/aio/test_sessions_mtls.py index b68766ca5b5d5..f3d7bd4ff7363 100644 --- a/packages/google-auth/tests/transport/aio/test_sessions_mtls.py +++ b/packages/google-auth/tests/transport/aio/test_sessions_mtls.py @@ -20,8 +20,7 @@ import pytest from google.auth import exceptions -from google.auth.aio import credentials -from google.auth.aio import transport +from google.auth.aio import credentials, transport from google.auth.aio.transport import sessions # This is the valid "workload" format the library expects @@ -40,19 +39,22 @@ async def test_configure_mtls_channel(self): Tests that the mTLS channel configures correctly when a valid workload config is mocked. """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)) - ), mock.patch( - "google.auth.aio.transport.mtls.get_client_cert_and_key" - ) as mock_helper, mock.patch( - "google.auth.aio.transport.mtls.make_client_cert_ssl_context" - ) as mock_make_context, mock.patch( - "aiohttp.TCPConnector" - ) as mock_connector, mock.patch( - "aiohttp.ClientSession" - ) as mock_session: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", + mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)), + ), + mock.patch( + "google.auth.aio.transport.mtls.get_client_cert_and_key" + ) as mock_helper, + mock.patch( + "google.auth.aio.transport.mtls.make_client_cert_ssl_context" + ) as mock_make_context, + mock.patch("aiohttp.TCPConnector") as mock_connector, + mock.patch("aiohttp.ClientSession") as mock_session, + ): mock_session.return_value.close = mock.AsyncMock() mock_exists.return_value = True mock_helper.return_value = (True, b"fake_cert_data", b"fake_key_data") @@ -78,9 +80,10 @@ async def test_configure_mtls_channel_disabled(self): """ Tests behavior when the config file does not exist. """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + ): mock_exists.return_value = False mock_creds = mock.AsyncMock(spec=credentials.Credentials) session = sessions.AsyncAuthorizedSession(mock_creds) @@ -93,10 +96,12 @@ async def test_configure_mtls_channel_invalid_format(self): """ Verifies that the MutualTLSChannelError is raised for bad formats. """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data='{"invalid": "format"}') + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", mock.mock_open(read_data='{"invalid": "format"}') + ), ): mock_exists.return_value = True mock_creds = mock.AsyncMock(spec=credentials.Credentials) @@ -111,10 +116,12 @@ async def test_configure_mtls_channel_invalud_fields(self): """ If cert is missing expected keys, it should fail gracefully """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data='{"cert_configs": {}}') + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", mock.mock_open(read_data='{"cert_configs": {}}') + ), ): mock_exists.return_value = True mock_creds = mock.AsyncMock(spec=credentials.Credentials) @@ -132,18 +139,18 @@ async def test_configure_mtls_channel_mock_callback(self): def mock_callback(): return (b"fake_cert_bytes", b"fake_key_bytes") - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch( - "google.auth.transport.mtls.has_default_client_cert_source", - return_value=True, - ), mock.patch( - "google.auth.aio.transport.mtls.make_client_cert_ssl_context" - ) as mock_make_context, mock.patch( - "aiohttp.TCPConnector" - ) as mock_connector, mock.patch( - "aiohttp.ClientSession" - ) as mock_session: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch( + "google.auth.transport.mtls.has_default_client_cert_source", + return_value=True, + ), + mock.patch( + "google.auth.aio.transport.mtls.make_client_cert_ssl_context" + ) as mock_make_context, + mock.patch("aiohttp.TCPConnector") as mock_connector, + mock.patch("aiohttp.ClientSession") as mock_session, + ): mock_session.return_value.close = mock.AsyncMock() mock_context = mock.Mock(spec=ssl.SSLContext) mock_make_context.return_value = mock_context @@ -166,15 +173,20 @@ async def test_configure_mtls_channel_custom_request(self): """Tests that if _auth_request is not an AiohttpRequest, _is_mtls is set to False because we can't configure the custom request with mTLS. """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)) - ), mock.patch( - "google.auth.aio.transport.mtls.get_client_cert_and_key" - ) as mock_helper, mock.patch( - "google.auth.aio.transport.mtls.make_client_cert_ssl_context" - ) as mock_make_context: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", + mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)), + ), + mock.patch( + "google.auth.aio.transport.mtls.get_client_cert_and_key" + ) as mock_helper, + mock.patch( + "google.auth.aio.transport.mtls.make_client_cert_ssl_context" + ) as mock_make_context, + ): mock_exists.return_value = True mock_helper.return_value = (True, b"fake_cert_data", b"fake_key_data") @@ -202,15 +214,20 @@ async def test_configure_mtls_channel_exception_resets_flag(self): Tests that self._is_mtls is reset to False if an exception is raised during configuration. """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)) - ), mock.patch( - "google.auth.aio.transport.mtls.get_client_cert_and_key" - ) as mock_helper, mock.patch( - "google.auth.aio.transport.mtls.make_client_cert_ssl_context" - ) as mock_make_context: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", + mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)), + ), + mock.patch( + "google.auth.aio.transport.mtls.get_client_cert_and_key" + ) as mock_helper, + mock.patch( + "google.auth.aio.transport.mtls.make_client_cert_ssl_context" + ) as mock_make_context, + ): mock_exists.return_value = True mock_helper.return_value = (True, b"fake_cert_data", b"fake_key_data") mock_make_context.side_effect = exceptions.ClientCertError("Mock error") @@ -230,15 +247,20 @@ async def test_configure_mtls_channel_transport_error_resets_flag(self): Tests that self._is_mtls is reset to False if a TransportError is raised during configuration. """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)) - ), mock.patch( - "google.auth.aio.transport.mtls.get_client_cert_and_key" - ) as mock_helper, mock.patch( - "google.auth.aio.transport.mtls.make_client_cert_ssl_context" - ) as mock_make_context: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", + mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)), + ), + mock.patch( + "google.auth.aio.transport.mtls.get_client_cert_and_key" + ) as mock_helper, + mock.patch( + "google.auth.aio.transport.mtls.make_client_cert_ssl_context" + ) as mock_make_context, + ): mock_exists.return_value = True mock_helper.return_value = (True, b"fake_cert_data", b"fake_key_data") mock_make_context.side_effect = exceptions.TransportError("Mock error") @@ -259,19 +281,22 @@ async def test_configure_mtls_channel_atomic_on_exception(self): a subsequent attempt that raises an exception will preserve the original mTLS state. """ # Step 1: Successful configuration - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)) - ), mock.patch( - "google.auth.aio.transport.mtls.get_client_cert_and_key" - ) as mock_helper, mock.patch( - "google.auth.aio.transport.mtls.make_client_cert_ssl_context" - ) as mock_make_context, mock.patch( - "aiohttp.TCPConnector" - ), mock.patch( - "aiohttp.ClientSession" - ) as mock_session: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", + mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)), + ), + mock.patch( + "google.auth.aio.transport.mtls.get_client_cert_and_key" + ) as mock_helper, + mock.patch( + "google.auth.aio.transport.mtls.make_client_cert_ssl_context" + ) as mock_make_context, + mock.patch("aiohttp.TCPConnector"), + mock.patch("aiohttp.ClientSession") as mock_session, + ): mock_session.return_value.close = mock.AsyncMock() mock_exists.return_value = True mock_helper.return_value = (True, b"fake_cert_data_1", b"fake_key_data_1") @@ -310,19 +335,22 @@ async def test_configure_mtls_channel_close_exception_does_not_abort(self): configuration is still considered successful, and is_mtls remains True without raising MutualTLSChannelError. """ - with mock.patch.dict( - os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"} - ), mock.patch("os.path.exists") as mock_exists, mock.patch( - "builtins.open", mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)) - ), mock.patch( - "google.auth.aio.transport.mtls.get_client_cert_and_key" - ) as mock_helper, mock.patch( - "google.auth.aio.transport.mtls.make_client_cert_ssl_context" - ) as mock_make_context, mock.patch( - "aiohttp.TCPConnector" - ), mock.patch( - "aiohttp.ClientSession" - ) as mock_session: + with ( + mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}), + mock.patch("os.path.exists") as mock_exists, + mock.patch( + "builtins.open", + mock.mock_open(read_data=json.dumps(VALID_WORKLOAD_CONFIG)), + ), + mock.patch( + "google.auth.aio.transport.mtls.get_client_cert_and_key" + ) as mock_helper, + mock.patch( + "google.auth.aio.transport.mtls.make_client_cert_ssl_context" + ) as mock_make_context, + mock.patch("aiohttp.TCPConnector"), + mock.patch("aiohttp.ClientSession") as mock_session, + ): mock_session.return_value.close = mock.AsyncMock() mock_exists.return_value = True mock_helper.return_value = (True, b"fake_cert_data", b"fake_key_data") diff --git a/packages/google-auth/tests/transport/test__custom_tls_signer.py b/packages/google-auth/tests/transport/test__custom_tls_signer.py index fa210ee0b8d7a..ab0f36298fe8e 100644 --- a/packages/google-auth/tests/transport/test__custom_tls_signer.py +++ b/packages/google-auth/tests/transport/test__custom_tls_signer.py @@ -24,7 +24,6 @@ from google.auth import exceptions from google.auth.transport import _custom_tls_signer - FAKE_ENTERPRISE_CERT_FILE_PATH = "/path/to/enterprise/cert/file" ENTERPRISE_CERT_FILE = os.path.join( os.path.dirname(__file__), "../data/enterprise_cert_valid.json" @@ -299,8 +298,9 @@ def test_cast_ssl_ctx_to_void_p_stdlib_unsupported_runtime_trace_refs(): fake_impl = mock.Mock() fake_impl.name = "cpython" - with mock.patch("sys.implementation", fake_impl), mock.patch( - "sys.getobjects", create=True + with ( + mock.patch("sys.implementation", fake_impl), + mock.patch("sys.getobjects", create=True), ): with pytest.raises( exceptions.MutualTLSChannelError, @@ -320,8 +320,9 @@ def test_cast_ssl_ctx_to_void_p_stdlib_unsupported_runtime_debug_flag(): context = ssl.SSLContext() fake_impl = mock.Mock() fake_impl.name = "cpython" - with mock.patch("sys.implementation", fake_impl), mock.patch( - "sysconfig.get_config_var", return_value=1 + with ( + mock.patch("sys.implementation", fake_impl), + mock.patch("sysconfig.get_config_var", return_value=1), ): with pytest.raises( exceptions.MutualTLSChannelError, @@ -340,8 +341,9 @@ def mock_get_config_var(var): fake_impl = mock.Mock() fake_impl.name = "cpython" - with mock.patch("sys.implementation", fake_impl), mock.patch( - "sysconfig.get_config_var", side_effect=mock_get_config_var + with ( + mock.patch("sys.implementation", fake_impl), + mock.patch("sysconfig.get_config_var", side_effect=mock_get_config_var), ): with pytest.raises( exceptions.MutualTLSChannelError, diff --git a/packages/google-auth/tests/transport/test__http_client.py b/packages/google-auth/tests/transport/test__http_client.py index 202276323c668..282cc0c280012 100644 --- a/packages/google-auth/tests/transport/test__http_client.py +++ b/packages/google-auth/tests/transport/test__http_client.py @@ -14,8 +14,8 @@ import pytest # type: ignore -from google.auth import exceptions import google.auth.transport._http_client +from google.auth import exceptions from tests.transport import compliance diff --git a/packages/google-auth/tests/transport/test__mtls_helper.py b/packages/google-auth/tests/transport/test__mtls_helper.py index e9bb62db21332..d1b035e889337 100644 --- a/packages/google-auth/tests/transport/test__mtls_helper.py +++ b/packages/google-auth/tests/transport/test__mtls_helper.py @@ -18,9 +18,9 @@ import tempfile from unittest import mock +import pytest # type: ignore from cryptography.hazmat.primitives import hashes, serialization from cryptography.hazmat.primitives.asymmetric import ec -import pytest # type: ignore from google.auth import environment_vars, exceptions from google.auth.transport import _mtls_helper @@ -548,9 +548,7 @@ def test_no_workload(self, mock_get_cert_config_path, mock_load_json_file): assert actual_cert is None assert actual_key is None - @mock.patch( - "google.auth.transport._mtls_helper._load_json_file", autospec=True - ) # noqa: E501 + @mock.patch("google.auth.transport._mtls_helper._load_json_file", autospec=True) # noqa: E501 @mock.patch( "google.auth.transport._mtls_helper._get_cert_config_path", autospec=True, @@ -559,9 +557,7 @@ def test_no_workload(self, mock_get_cert_config_path, mock_load_json_file): "google.auth.transport._mtls_helper._read_cert_and_key_files", autospec=True, ) # noqa: E501 - @mock.patch( - "google.auth.transport._mtls_helper.path.exists", autospec=True - ) # noqa: E501 + @mock.patch("google.auth.transport._mtls_helper.path.exists", autospec=True) # noqa: E501 def test_no_workload_fallback_to_home( self, mock_path_exists, @@ -611,13 +607,9 @@ def load_json_side_effect(path): mock_load_json_file.assert_has_calls( [mock.call(ecp_path), mock.call(home_path)] ) - mock_read_cert_and_key_files.assert_called_once_with( - "cert/path", "key/path" - ) # noqa: E501 + mock_read_cert_and_key_files.assert_called_once_with("cert/path", "key/path") # noqa: E501 - @mock.patch( - "google.auth.transport._mtls_helper._load_json_file", autospec=True - ) # noqa: E501 + @mock.patch("google.auth.transport._mtls_helper._load_json_file", autospec=True) # noqa: E501 @mock.patch( "google.auth.transport._mtls_helper._get_cert_config_path", autospec=True, @@ -626,9 +618,7 @@ def load_json_side_effect(path): "google.auth.transport._mtls_helper._read_cert_and_key_files", autospec=True, ) # noqa: E501 - @mock.patch( - "google.auth.transport._mtls_helper.path.exists", autospec=True - ) # noqa: E501 + @mock.patch("google.auth.transport._mtls_helper.path.exists", autospec=True) # noqa: E501 def test_no_workload_fallback_to_home_error( self, mock_path_exists, @@ -669,16 +659,12 @@ def load_json_side_effect(path): ) mock_read_cert_and_key_files.assert_not_called() - @mock.patch( - "google.auth.transport._mtls_helper._load_json_file", autospec=True - ) # noqa: E501 + @mock.patch("google.auth.transport._mtls_helper._load_json_file", autospec=True) # noqa: E501 @mock.patch( "google.auth.transport._mtls_helper._get_cert_config_path", autospec=True, ) - @mock.patch( - "google.auth.transport._mtls_helper.path.exists", autospec=True - ) # noqa: E501 + @mock.patch("google.auth.transport._mtls_helper.path.exists", autospec=True) # noqa: E501 @mock.patch("os.path.normpath", autospec=True) def test_no_workload_fallback_avoided_same_path_normalization( self, @@ -712,9 +698,7 @@ def normpath_side_effect(path): "google.auth._cloud_sdk.get_config_path", return_value="C:\\Users\\User\\.config\\gcloud", ): - actual_cert, actual_key = _mtls_helper._get_workload_cert_and_key( - None - ) # noqa: E501 + actual_cert, actual_key = _mtls_helper._get_workload_cert_and_key(None) # noqa: E501 assert actual_cert is None assert actual_key is None @@ -1302,8 +1286,9 @@ def test_memfd_success(self, mock_memfd_cm, mock_memfd_create): ) mock_memfd_cm.return_value = mock_memfd_ctx - with mock.patch.object(os.path, "exists", return_value=True), mock.patch( - "builtins.open", mock.mock_open() + with ( + mock.patch.object(os.path, "exists", return_value=True), + mock.patch("builtins.open", mock.mock_open()), ): with _mtls_helper.secure_cert_key_paths( pytest.public_cert_bytes, @@ -1368,9 +1353,10 @@ def test_falls_back_to_tempfile_when_filesystem_unreadable( ) mock_tempfile_cm.return_value = mock_tempfile_ctx - with mock.patch.object(os.path, "exists", return_value=True), mock.patch( - "builtins.open", mock.mock_open() - ) as mock_open: + with ( + mock.patch.object(os.path, "exists", return_value=True), + mock.patch("builtins.open", mock.mock_open()) as mock_open, + ): mock_open.side_effect = PermissionError("Permission denied") with _mtls_helper.secure_cert_key_paths( diff --git a/packages/google-auth/tests/transport/test_grpc.py b/packages/google-auth/tests/transport/test_grpc.py index 7979df7abb4dd..577bd3d179589 100644 --- a/packages/google-auth/tests/transport/test_grpc.py +++ b/packages/google-auth/tests/transport/test_grpc.py @@ -16,21 +16,18 @@ import importlib import os import time -from unittest import mock import warnings +from unittest import mock import pytest # type: ignore -from google.auth import _helpers -from google.auth import credentials -from google.auth import environment_vars -from google.auth import exceptions -from google.auth import transport +from google.auth import _helpers, credentials, environment_vars, exceptions, transport from google.oauth2 import service_account try: # pylint: disable=ungrouped-imports import grpc # type: ignore + import google.auth.transport.grpc HAS_GRPC = True @@ -657,15 +654,15 @@ def test_get_client_ssl_credentials_auto_enablement( None, ) - with mock.patch.dict( - os.environ, - { - environment_vars.GOOGLE_API_CERTIFICATE_CONFIG: "fake_config_path.json", - }, - ), mock.patch( - "builtins.open", mock.mock_open(read_data=fake_config_content) - ), mock.patch( - "os.path.exists", return_value=True + with ( + mock.patch.dict( + os.environ, + { + environment_vars.GOOGLE_API_CERTIFICATE_CONFIG: "fake_config_path.json", + }, + ), + mock.patch("builtins.open", mock.mock_open(read_data=fake_config_content)), + mock.patch("os.path.exists", return_value=True), ): # Ensure mTLS explicit flags are not present in the environment os.environ.pop(environment_vars.GOOGLE_API_USE_CLIENT_CERTIFICATE, None) diff --git a/packages/google-auth/tests/transport/test_mtls.py b/packages/google-auth/tests/transport/test_mtls.py index ee108448fa726..7caf63861751f 100644 --- a/packages/google-auth/tests/transport/test_mtls.py +++ b/packages/google-auth/tests/transport/test_mtls.py @@ -19,8 +19,7 @@ import pytest # type: ignore from google.auth import exceptions -from google.auth.transport import _mtls_helper -from google.auth.transport import mtls +from google.auth.transport import _mtls_helper, mtls @mock.patch("google.auth.transport._mtls_helper._get_cert_config_path") diff --git a/packages/google-auth/tests/transport/test_requests.py b/packages/google-auth/tests/transport/test_requests.py index 2ca1922494efd..bc1db48715f6a 100644 --- a/packages/google-auth/tests/transport/test_requests.py +++ b/packages/google-auth/tests/transport/test_requests.py @@ -23,12 +23,11 @@ import requests import requests.adapters -from google.auth import environment_vars -from google.auth import exceptions import google.auth.credentials import google.auth.transport._custom_tls_signer import google.auth.transport._mtls_helper import google.auth.transport.requests +from google.auth import environment_vars, exceptions from google.oauth2 import service_account from tests.transport import compliance diff --git a/packages/google-auth/tests/transport/test_urllib3.py b/packages/google-auth/tests/transport/test_urllib3.py index e1c92dbebc2cb..11e13837b02cd 100644 --- a/packages/google-auth/tests/transport/test_urllib3.py +++ b/packages/google-auth/tests/transport/test_urllib3.py @@ -19,11 +19,10 @@ import pytest # type: ignore import urllib3 # type: ignore -from google.auth import environment_vars -from google.auth import exceptions import google.auth.credentials import google.auth.transport._mtls_helper import google.auth.transport.urllib3 +from google.auth import environment_vars, exceptions from google.oauth2 import service_account from tests.transport import compliance diff --git a/packages/google-auth/tests_async/oauth2/test__client_async.py b/packages/google-auth/tests_async/oauth2/test__client_async.py index a3abd9067186d..c572c3e791add 100644 --- a/packages/google-auth/tests_async/oauth2/test__client_async.py +++ b/packages/google-auth/tests_async/oauth2/test__client_async.py @@ -16,14 +16,13 @@ import datetime import http.client as http_client import json -from unittest import mock import urllib +from unittest import mock import pytest # type: ignore -from google.auth import _helpers +from google.auth import _helpers, exceptions from google.auth import _jwt_async as jwt -from google.auth import exceptions from google.auth.aio import transport as aio_transport from google.oauth2 import _client as sync_client from google.oauth2 import _client_async as _client diff --git a/packages/google-auth/tests_async/oauth2/test_credentials_async.py b/packages/google-auth/tests_async/oauth2/test_credentials_async.py index d8bf82a0b59f6..7fbe76c028670 100644 --- a/packages/google-auth/tests_async/oauth2/test_credentials_async.py +++ b/packages/google-auth/tests_async/oauth2/test_credentials_async.py @@ -21,9 +21,7 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import exceptions -from google.auth import transport +from google.auth import _helpers, exceptions, transport from google.oauth2 import _credentials_async as _credentials_async from google.oauth2 import credentials from tests.oauth2 import test_credentials diff --git a/packages/google-auth/tests_async/oauth2/test_id_token.py b/packages/google-auth/tests_async/oauth2/test_id_token.py index 51d85daf2285d..22456c6bdaced 100644 --- a/packages/google-auth/tests_async/oauth2/test_id_token.py +++ b/packages/google-auth/tests_async/oauth2/test_id_token.py @@ -18,9 +18,8 @@ import pytest # type: ignore -from google.auth import environment_vars -from google.auth import exceptions import google.auth.compute_engine._metadata +from google.auth import environment_vars, exceptions from google.oauth2 import _id_token_async as id_token from google.oauth2 import _service_account_async from google.oauth2 import id_token as sync_id_token diff --git a/packages/google-auth/tests_async/oauth2/test_reauth_async.py b/packages/google-auth/tests_async/oauth2/test_reauth_async.py index 4874a3728e2b9..9af5019dc0fc4 100644 --- a/packages/google-auth/tests_async/oauth2/test_reauth_async.py +++ b/packages/google-auth/tests_async/oauth2/test_reauth_async.py @@ -18,9 +18,7 @@ import pytest # type: ignore from google.auth import exceptions -from google.oauth2 import _reauth_async -from google.oauth2 import reauth - +from google.oauth2 import _reauth_async, reauth MOCK_REQUEST = mock.AsyncMock(spec=["transport.Request"]) CHALLENGES_RESPONSE_TEMPLATE = { diff --git a/packages/google-auth/tests_async/oauth2/test_service_account_async.py b/packages/google-auth/tests_async/oauth2/test_service_account_async.py index 0539ecc80e13c..95b3d36bacc08 100644 --- a/packages/google-auth/tests_async/oauth2/test_service_account_async.py +++ b/packages/google-auth/tests_async/oauth2/test_service_account_async.py @@ -17,10 +17,7 @@ import pytest # type: ignore -from google.auth import _helpers -from google.auth import crypt -from google.auth import jwt -from google.auth import transport +from google.auth import _helpers, crypt, jwt, transport from google.oauth2 import _service_account_async as service_account from tests.oauth2 import test_service_account @@ -242,14 +239,17 @@ async def test_before_request_triggers_rab_refresh(self): request = mock.AsyncMock(spec=["transport.Request"]) headers1 = {} - with mock.patch.object( - credentials, - "_lookup_regional_access_boundary", - new_callable=mock.AsyncMock, - ) as mock_lookup, mock.patch.object( - credentials, - "_is_regional_access_boundary_lookup_required", - return_value=True, + with ( + mock.patch.object( + credentials, + "_lookup_regional_access_boundary", + new_callable=mock.AsyncMock, + ) as mock_lookup, + mock.patch.object( + credentials, + "_is_regional_access_boundary_lookup_required", + return_value=True, + ), ): mock_lookup.return_value = { "locations": ["us-central1", "europe-west1"], @@ -281,15 +281,18 @@ async def test_before_request_rab_refresh_failure_ignored(self): request = mock.AsyncMock(spec=["transport.Request"]) headers = {} - with mock.patch.object( - credentials, - "_lookup_regional_access_boundary", - new_callable=mock.AsyncMock, - side_effect=Exception("Transport failed"), - ) as mock_lookup, mock.patch.object( - credentials, - "_is_regional_access_boundary_lookup_required", - return_value=True, + with ( + mock.patch.object( + credentials, + "_lookup_regional_access_boundary", + new_callable=mock.AsyncMock, + side_effect=Exception("Transport failed"), + ) as mock_lookup, + mock.patch.object( + credentials, + "_is_regional_access_boundary_lookup_required", + return_value=True, + ), ): # Any transport/lookup failure must be caught gracefully during refresh. await credentials.before_request( @@ -311,14 +314,17 @@ async def test_before_request_triggers_blocking_rab_refresh(self): request = mock.AsyncMock(spec=["transport.Request"]) headers = {} - with mock.patch.object( - credentials, - "_lookup_regional_access_boundary", - new_callable=mock.AsyncMock, - ) as mock_lookup, mock.patch.object( - credentials, - "_is_regional_access_boundary_lookup_required", - return_value=True, + with ( + mock.patch.object( + credentials, + "_lookup_regional_access_boundary", + new_callable=mock.AsyncMock, + ) as mock_lookup, + mock.patch.object( + credentials, + "_is_regional_access_boundary_lookup_required", + return_value=True, + ), ): mock_lookup.return_value = { "locations": ["us-central1", "europe-west1"], diff --git a/packages/google-auth/tests_async/test__default_async.py b/packages/google-auth/tests_async/test__default_async.py index ebdd2c1b0184f..062b6fd160ec8 100644 --- a/packages/google-auth/tests_async/test__default_async.py +++ b/packages/google-auth/tests_async/test__default_async.py @@ -19,14 +19,11 @@ import pytest # type: ignore +import google.oauth2.credentials from google.auth import _credentials_async as credentials from google.auth import _default_async as _default -from google.auth import app_engine -from google.auth import compute_engine -from google.auth import environment_vars -from google.auth import exceptions +from google.auth import app_engine, compute_engine, environment_vars, exceptions from google.oauth2 import _service_account_async as service_account -import google.oauth2.credentials from tests import test__default as test_default MOCK_CREDENTIALS = mock.Mock(spec=credentials.CredentialsWithQuotaProject) @@ -307,9 +304,9 @@ def test__get_gae_credentials_gen1(app_identity): @mock.patch.dict(os.environ) def test__get_gae_credentials_gen2(): - os.environ[ - "GAE_RUNTIME" - ] = f"python{sys.version_info.major}{sys.version_info.minor}" + os.environ["GAE_RUNTIME"] = ( + f"python{sys.version_info.major}{sys.version_info.minor}" + ) credentials, project_id = _default._get_gae_credentials() assert credentials is None assert project_id is None diff --git a/packages/google-auth/tests_async/test__regional_access_boundary_utils.py b/packages/google-auth/tests_async/test__regional_access_boundary_utils.py index af8e17c6403c4..499bd16c4d4e9 100644 --- a/packages/google-auth/tests_async/test__regional_access_boundary_utils.py +++ b/packages/google-auth/tests_async/test__regional_access_boundary_utils.py @@ -56,7 +56,9 @@ async def test_async_refresh_manager_duplicate_refresh_prevented(): async def controlled_lookup(*args, **kwargs): lookup_started.set() # Signal that the background lookup has started. - await lookup_finish.wait() # Block until the test allows the lookup to complete. + await ( + lookup_finish.wait() + ) # Block until the test allows the lookup to complete. return {"encodedLocations": "0xA30"} credentials._lookup_regional_access_boundary.side_effect = controlled_lookup diff --git a/packages/google-auth/tests_async/test_jwt_async.py b/packages/google-auth/tests_async/test_jwt_async.py index 9e6054fa93efe..22ee7c26ff135 100644 --- a/packages/google-auth/tests_async/test_jwt_async.py +++ b/packages/google-auth/tests_async/test_jwt_async.py @@ -19,8 +19,7 @@ import pytest # type: ignore from google.auth import _jwt_async as jwt_async -from google.auth import crypt -from google.auth import exceptions +from google.auth import crypt, exceptions from tests import test_jwt diff --git a/packages/google-auth/tests_async/transport/test_aiohttp_requests.py b/packages/google-auth/tests_async/transport/test_aiohttp_requests.py index 1dc5b0025edc5..2071bc589f12e 100644 --- a/packages/google-auth/tests_async/transport/test_aiohttp_requests.py +++ b/packages/google-auth/tests_async/transport/test_aiohttp_requests.py @@ -15,13 +15,13 @@ from unittest import mock import aiohttp # type: ignore -from aioresponses import aioresponses, core # type: ignore import pytest # type: ignore -from tests_async.transport import async_compliance +from aioresponses import aioresponses, core # type: ignore import google.auth._credentials_async -from google.auth.transport import _aiohttp_requests as aiohttp_requests import google.auth.transport._mtls_helper +from google.auth.transport import _aiohttp_requests as aiohttp_requests +from tests_async.transport import async_compliance class TestCombinedResponse: diff --git a/packages/google-cloud-dns/google/cloud/dns/zone.py b/packages/google-cloud-dns/google/cloud/dns/zone.py index 01bc201f10150..e044ee0442d99 100644 --- a/packages/google-cloud-dns/google/cloud/dns/zone.py +++ b/packages/google-cloud-dns/google/cloud/dns/zone.py @@ -73,7 +73,7 @@ def from_api_repr(cls, resource, client): dns_name = resource.get("dnsName") if name is None or dns_name is None: raise KeyError( - "Resource lacks required identity information:" '["name"]["dnsName"]' + 'Resource lacks required identity information:["name"]["dnsName"]' ) zone = cls(name, dns_name, client=client) zone._set_properties(resource) diff --git a/packages/google-cloud-dns/noxfile.py b/packages/google-cloud-dns/noxfile.py index c7e04c73bc5a8..0c6507c42a5d4 100644 --- a/packages/google-cloud-dns/noxfile.py +++ b/packages/google-cloud-dns/noxfile.py @@ -107,10 +107,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", "google", "tests") diff --git a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/config/block.py b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/config/block.py index 2fbf97237a0c2..c6398d98cdb7a 100644 --- a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/config/block.py +++ b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/config/block.py @@ -72,6 +72,7 @@ class Block: page_number: Optional. """ + type_: Any = dataclasses.field(init=True, repr=False) text: str = dataclasses.field(init=True, repr=False) bounding_box: Optional[Union[SimpleNamespace, List[Any]]] = dataclasses.field( diff --git a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/converter.py b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/converter.py index e87b6fb8bca6d..8de74c1080015 100644 --- a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/converter.py +++ b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/converter.py @@ -15,9 +15,9 @@ # """Document.proto converters.""" -from concurrent import futures import os import time +from concurrent import futures from typing import Dict, List, Optional, Set, Tuple from google.api_core.client_options import ClientOptions diff --git a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/vision_helpers.py b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/vision_helpers.py index 77f1c08e7fac9..a312ca3fbf0d5 100644 --- a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/vision_helpers.py +++ b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/converters/vision_helpers.py @@ -18,6 +18,7 @@ import dataclasses from typing import List +import immutabledict from google.cloud.documentai import Document from google.cloud.vision import ( AnnotateImageResponse, @@ -30,7 +31,6 @@ TextAnnotation, Word, ) -import immutabledict from google.cloud import vision from google.cloud.documentai_toolbox.constants import ElementWithLayout diff --git a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/utilities/gcs_utilities.py b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/utilities/gcs_utilities.py index 3f2c8a487c22a..080ded94e66b6 100644 --- a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/utilities/gcs_utilities.py +++ b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/utilities/gcs_utilities.py @@ -14,15 +14,18 @@ # limitations under the License. # """Google Cloud Storage utilities.""" + import os import re from typing import Dict, List, Optional, Tuple from google.api_core.gapic_v1 import client_info -from google.cloud import documentai # type: ignore[attr-defined] -from google.cloud import documentai_toolbox -from google.cloud import storage # type: ignore[attr-defined] +from google.cloud import ( + documentai, # type: ignore[attr-defined] + documentai_toolbox, + storage, # type: ignore[attr-defined] +) from google.cloud.documentai_toolbox import constants diff --git a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/document.py b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/document.py index 6ef5200e7c710..d03e633597e27 100644 --- a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/document.py +++ b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/document.py @@ -18,10 +18,10 @@ import collections import copy import dataclasses -from functools import cached_property import glob import os import re +from functools import cached_property from typing import Any, Dict, Iterable, Iterator, List, Optional, Type, Union from google.api_core.client_options import ClientOptions diff --git a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/page.py b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/page.py index 3dd9b9d68876b..fa2579087f804 100644 --- a/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/page.py +++ b/packages/google-cloud-documentai-toolbox/google/cloud/documentai_toolbox/wrappers/page.py @@ -15,8 +15,8 @@ # """Wrappers for Document AI Page type.""" -from abc import ABC import dataclasses +from abc import ABC from functools import cached_property from typing import Iterable, List, Optional, Type, TypeVar diff --git a/packages/google-cloud-documentai-toolbox/noxfile.py b/packages/google-cloud-documentai-toolbox/noxfile.py index 18a537cb9bdae..b6a7ae89ebcca 100644 --- a/packages/google-cloud-documentai-toolbox/noxfile.py +++ b/packages/google-cloud-documentai-toolbox/noxfile.py @@ -109,10 +109,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", "google", "tests") diff --git a/packages/google-cloud-documentai-toolbox/tests/unit/test_document.py b/packages/google-cloud-documentai-toolbox/tests/unit/test_document.py index b9c7d448df1c1..9b7b77c0512a1 100644 --- a/packages/google-cloud-documentai-toolbox/tests/unit/test_document.py +++ b/packages/google-cloud-documentai-toolbox/tests/unit/test_document.py @@ -21,8 +21,8 @@ from unittest import mock from xml.etree import ElementTree -from google.cloud.vision import AnnotateFileResponse import pytest +from google.cloud.vision import AnnotateFileResponse from google.cloud import documentai from google.cloud.documentai_toolbox import document, gcs_utilities diff --git a/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/__init__.py b/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/__init__.py index 3772e8f623e34..7eb65a1056315 100644 --- a/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/__init__.py +++ b/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/__init__.py @@ -14,7 +14,6 @@ """Google Cloud Runtime Configurator API package.""" - from google.cloud.runtimeconfig import version as runtimeconfig_version __version__ = runtimeconfig_version.__version__ diff --git a/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/_http.py b/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/_http.py index acd7188e31a54..dc77833bc5457 100644 --- a/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/_http.py +++ b/packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/_http.py @@ -15,7 +15,6 @@ """Create / interact with Google Cloud RuntimeConfig connections.""" - from google.cloud import _http from google.cloud.runtimeconfig import __version__ diff --git a/packages/google-cloud-runtimeconfig/noxfile.py b/packages/google-cloud-runtimeconfig/noxfile.py index 75f3c0a98e3f9..c8ddaaa120ef3 100644 --- a/packages/google-cloud-runtimeconfig/noxfile.py +++ b/packages/google-cloud-runtimeconfig/noxfile.py @@ -92,10 +92,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", "google", "tests") diff --git a/packages/google-cloud-testutils/noxfile.py b/packages/google-cloud-testutils/noxfile.py index c7bde14d4e510..5a5809c9d7607 100644 --- a/packages/google-cloud-testutils/noxfile.py +++ b/packages/google-cloud-testutils/noxfile.py @@ -58,10 +58,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install("flake8", BLACK_VERSION) + session.install("flake8", RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", + *BLACK_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", *BLACK_PATHS, ) session.run("flake8", *BLACK_PATHS) diff --git a/packages/google-cloud-testutils/setup.py b/packages/google-cloud-testutils/setup.py index f0def265cb199..c11ccac6c4f2c 100644 --- a/packages/google-cloud-testutils/setup.py +++ b/packages/google-cloud-testutils/setup.py @@ -15,6 +15,7 @@ import io import os import re + import setuptools # type: ignore version = None diff --git a/packages/google-cloud-testutils/test_utils/lower_bound_checker/lower_bound_checker.py b/packages/google-cloud-testutils/test_utils/lower_bound_checker/lower_bound_checker.py index abd69196c1b6f..2294995f443c9 100644 --- a/packages/google-cloud-testutils/test_utils/lower_bound_checker/lower_bound_checker.py +++ b/packages/google-cloud-testutils/test_utils/lower_bound_checker/lower_bound_checker.py @@ -12,15 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import click +import importlib.metadata as metadata from pathlib import Path -from typing import List, Tuple, Set +from typing import List, Set, Tuple +import click from packaging.requirements import Requirement from packaging.version import Version -import importlib.metadata as metadata - def _get_package_requirements(package_name: str) -> List[Requirement]: """ diff --git a/packages/google-cloud-testutils/test_utils/orchestrate.py b/packages/google-cloud-testutils/test_utils/orchestrate.py index 65e5464040a07..177780f8f6b4b 100644 --- a/packages/google-cloud-testutils/test_utils/orchestrate.py +++ b/packages/google-cloud-testutils/test_utils/orchestrate.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from __future__ import annotations + import itertools import math import queue diff --git a/packages/google-cloud-testutils/test_utils/prefixer.py b/packages/google-cloud-testutils/test_utils/prefixer.py index 89d1e8e4423a4..ba8b211718198 100644 --- a/packages/google-cloud-testutils/test_utils/prefixer.py +++ b/packages/google-cloud-testutils/test_utils/prefixer.py @@ -15,7 +15,6 @@ import datetime import random import re - from typing import Union _RESOURCE_DATE_FORMAT = "%Y%m%d%H%M%S" diff --git a/packages/google-cloud-testutils/test_utils/system.py b/packages/google-cloud-testutils/test_utils/system.py index ed513be422bd4..f99b81bc964c5 100644 --- a/packages/google-cloud-testutils/test_utils/system.py +++ b/packages/google-cloud-testutils/test_utils/system.py @@ -13,6 +13,7 @@ # limitations under the License. from __future__ import print_function + import os import sys import time @@ -20,7 +21,6 @@ import google.auth.credentials # type: ignore from google.auth.environment_vars import CREDENTIALS as TEST_CREDENTIALS # type: ignore - # From shell environ. May be None. CREDENTIALS = os.getenv(TEST_CREDENTIALS) diff --git a/packages/google-cloud-testutils/test_utils/vpcsc_config.py b/packages/google-cloud-testutils/test_utils/vpcsc_config.py index c5e36e767ce45..eaad96be85436 100644 --- a/packages/google-cloud-testutils/test_utils/vpcsc_config.py +++ b/packages/google-cloud-testutils/test_utils/vpcsc_config.py @@ -18,7 +18,6 @@ import pytest # type: ignore - INSIDE_VPCSC_ENVVAR = "GOOGLE_CLOUD_TESTS_IN_VPCSC" PROJECT_INSIDE_ENVVAR = "PROJECT_ID" PROJECT_OUTSIDE_ENVVAR = "GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT" diff --git a/packages/google-crc32c/noxfile.py b/packages/google-crc32c/noxfile.py index 2a268734c5b59..70f4ccdf44c97 100644 --- a/packages/google-crc32c/noxfile.py +++ b/packages/google-crc32c/noxfile.py @@ -94,10 +94,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{ALL_PYTHON[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", *LINT_PATHS) diff --git a/packages/google-crc32c/src/google_crc32c/cext.py b/packages/google-crc32c/src/google_crc32c/cext.py index 1ace01c481069..31895740fe751 100644 --- a/packages/google-crc32c/src/google_crc32c/cext.py +++ b/packages/google-crc32c/src/google_crc32c/cext.py @@ -16,8 +16,10 @@ # modify the search path used to locate shared libraries. import google_crc32c.__config__ # noqa: F401 from google_crc32c._checksum import CommonChecksum -from google_crc32c._crc32c import extend # type: ignore -from google_crc32c._crc32c import value # type: ignore +from google_crc32c._crc32c import ( + extend, # type: ignore + value, # type: ignore +) class Checksum(CommonChecksum): diff --git a/packages/google-resumable-media/google/_async_resumable_media/__init__.py b/packages/google-resumable-media/google/_async_resumable_media/__init__.py index eaade3e8792fe..f319f8ea6953e 100644 --- a/packages/google-resumable-media/google/_async_resumable_media/__init__.py +++ b/packages/google-resumable-media/google/_async_resumable_media/__init__.py @@ -42,13 +42,14 @@ .. _pip: https://pip.pypa.io/ """ -from google.resumable_media.common import DataCorruption -from google.resumable_media.common import InvalidResponse -from google.resumable_media.common import PERMANENT_REDIRECT -from google.resumable_media.common import RetryStrategy -from google.resumable_media.common import TOO_MANY_REQUESTS -from google.resumable_media.common import UPLOAD_CHUNK_SIZE - +from google.resumable_media.common import ( + PERMANENT_REDIRECT, + TOO_MANY_REQUESTS, + UPLOAD_CHUNK_SIZE, + DataCorruption, + InvalidResponse, + RetryStrategy, +) __all__ = [ "DataCorruption", diff --git a/packages/google-resumable-media/google/_async_resumable_media/_download.py b/packages/google-resumable-media/google/_async_resumable_media/_download.py index 1966f339c81c0..66eb34ae497bd 100644 --- a/packages/google-resumable-media/google/_async_resumable_media/_download.py +++ b/packages/google-resumable-media/google/_async_resumable_media/_download.py @@ -20,7 +20,6 @@ from google._async_resumable_media import _helpers from google.resumable_media import common - _CONTENT_RANGE_RE = re.compile( r"bytes (?P\d+)-(?P\d+)/(?P\d+)", flags=re.IGNORECASE, diff --git a/packages/google-resumable-media/google/_async_resumable_media/_helpers.py b/packages/google-resumable-media/google/_async_resumable_media/_helpers.py index 8cf0f2e96acb4..2cd6f93db739e 100644 --- a/packages/google-resumable-media/google/_async_resumable_media/_helpers.py +++ b/packages/google-resumable-media/google/_async_resumable_media/_helpers.py @@ -18,10 +18,8 @@ import random import time - from google.resumable_media import common - RANGE_HEADER = "range" CONTENT_RANGE_HEADER = "content-range" diff --git a/packages/google-resumable-media/google/_async_resumable_media/_upload.py b/packages/google-resumable-media/google/_async_resumable_media/_upload.py index 9f5b0de1b5392..65f261203917e 100644 --- a/packages/google-resumable-media/google/_async_resumable_media/_upload.py +++ b/packages/google-resumable-media/google/_async_resumable_media/_upload.py @@ -32,22 +32,20 @@ from google.resumable_media import _helpers as sync_helpers from google.resumable_media import _upload as sync_upload from google.resumable_media import common - - from google.resumable_media._upload import ( - _CONTENT_TYPE_HEADER, - _CONTENT_RANGE_TEMPLATE, - _RANGE_UNKNOWN_TEMPLATE, - _EMPTY_RANGE_TEMPLATE, _BOUNDARY_FORMAT, - _MULTIPART_SEP, + _BYTES_RANGE_RE, + _CONTENT_RANGE_TEMPLATE, + _CONTENT_TYPE_HEADER, _CRLF, + _EMPTY_RANGE_TEMPLATE, _MULTIPART_BEGIN, - _RELATED_HEADER, - _BYTES_RANGE_RE, - _STREAM_ERROR_TEMPLATE, + _MULTIPART_SEP, _POST, _PUT, + _RANGE_UNKNOWN_TEMPLATE, + _RELATED_HEADER, + _STREAM_ERROR_TEMPLATE, _UPLOAD_CHECKSUM_MISMATCH_MESSAGE, _UPLOAD_METADATA_NO_APPROPRIATE_CHECKSUM_MESSAGE, ) diff --git a/packages/google-resumable-media/google/_async_resumable_media/requests/__init__.py b/packages/google-resumable-media/google/_async_resumable_media/requests/__init__.py index e6a6190c17f57..f028bc206f487 100644 --- a/packages/google-resumable-media/google/_async_resumable_media/requests/__init__.py +++ b/packages/google-resumable-media/google/_async_resumable_media/requests/__init__.py @@ -663,14 +663,17 @@ def SimpleUpload(*args, **kwargs): True """ -from google._async_resumable_media.requests.download import ChunkedDownload -from google._async_resumable_media.requests.download import Download -from google._async_resumable_media.requests.upload import MultipartUpload -from google._async_resumable_media.requests.download import RawChunkedDownload -from google._async_resumable_media.requests.download import RawDownload -from google._async_resumable_media.requests.upload import ResumableUpload -from google._async_resumable_media.requests.upload import SimpleUpload - +from google._async_resumable_media.requests.download import ( + ChunkedDownload, + Download, + RawChunkedDownload, + RawDownload, +) +from google._async_resumable_media.requests.upload import ( + MultipartUpload, + ResumableUpload, + SimpleUpload, +) __all__ = [ "ChunkedDownload", diff --git a/packages/google-resumable-media/google/_async_resumable_media/requests/_request_helpers.py b/packages/google-resumable-media/google/_async_resumable_media/requests/_request_helpers.py index cd9b9b85b3ef6..93c345c491ace 100644 --- a/packages/google-resumable-media/google/_async_resumable_media/requests/_request_helpers.py +++ b/packages/google-resumable-media/google/_async_resumable_media/requests/_request_helpers.py @@ -19,12 +19,12 @@ import functools +import aiohttp # type: ignore +from google.auth.transport import _aiohttp_requests as aiohttp_requests # type: ignore + from google._async_resumable_media import _helpers from google.resumable_media import common -from google.auth.transport import _aiohttp_requests as aiohttp_requests # type: ignore -import aiohttp # type: ignore - _DEFAULT_RETRY_STRATEGY = common.RetryStrategy() _SINGLE_GET_CHUNK_SIZE = 8192 diff --git a/packages/google-resumable-media/google/_async_resumable_media/requests/download.py b/packages/google-resumable-media/google/_async_resumable_media/requests/download.py index 490017cf2880e..4017804678e6f 100644 --- a/packages/google-resumable-media/google/_async_resumable_media/requests/download.py +++ b/packages/google-resumable-media/google/_async_resumable_media/requests/download.py @@ -14,14 +14,14 @@ """Support for downloading media from Google APIs.""" -import urllib3.response # type: ignore import http -from google._async_resumable_media import _download -from google._async_resumable_media import _helpers +import urllib3.response # type: ignore + +from google._async_resumable_media import _download, _helpers from google._async_resumable_media.requests import _request_helpers -from google.resumable_media import common from google.resumable_media import _helpers as sync_helpers +from google.resumable_media import common from google.resumable_media.requests import download _CHECKSUM_MISMATCH = download._CHECKSUM_MISMATCH diff --git a/packages/google-resumable-media/google/resumable_media/__init__.py b/packages/google-resumable-media/google/resumable_media/__init__.py index eaade3e8792fe..f319f8ea6953e 100644 --- a/packages/google-resumable-media/google/resumable_media/__init__.py +++ b/packages/google-resumable-media/google/resumable_media/__init__.py @@ -42,13 +42,14 @@ .. _pip: https://pip.pypa.io/ """ -from google.resumable_media.common import DataCorruption -from google.resumable_media.common import InvalidResponse -from google.resumable_media.common import PERMANENT_REDIRECT -from google.resumable_media.common import RetryStrategy -from google.resumable_media.common import TOO_MANY_REQUESTS -from google.resumable_media.common import UPLOAD_CHUNK_SIZE - +from google.resumable_media.common import ( + PERMANENT_REDIRECT, + TOO_MANY_REQUESTS, + UPLOAD_CHUNK_SIZE, + DataCorruption, + InvalidResponse, + RetryStrategy, +) __all__ = [ "DataCorruption", diff --git a/packages/google-resumable-media/google/resumable_media/_download.py b/packages/google-resumable-media/google/resumable_media/_download.py index 5ca41ba5422e5..5e9a8ad2226ba 100644 --- a/packages/google-resumable-media/google/resumable_media/_download.py +++ b/packages/google-resumable-media/google/resumable_media/_download.py @@ -17,9 +17,7 @@ import http.client import re -from google.resumable_media import _helpers -from google.resumable_media import common - +from google.resumable_media import _helpers, common _CONTENT_RANGE_RE = re.compile( r"bytes (?P\d+)-(?P\d+)/(?P\d+)", diff --git a/packages/google-resumable-media/google/resumable_media/_helpers.py b/packages/google-resumable-media/google/resumable_media/_helpers.py index e80d9909b6bfc..04cd6cc80b428 100644 --- a/packages/google-resumable-media/google/resumable_media/_helpers.py +++ b/packages/google-resumable-media/google/resumable_media/_helpers.py @@ -21,15 +21,10 @@ import logging import random import warnings - -from urllib.parse import parse_qs -from urllib.parse import urlencode -from urllib.parse import urlsplit -from urllib.parse import urlunsplit +from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit from google.resumable_media import common - RANGE_HEADER = "range" CONTENT_RANGE_HEADER = "content-range" CONTENT_ENCODING_HEADER = "content-encoding" diff --git a/packages/google-resumable-media/google/resumable_media/_upload.py b/packages/google-resumable-media/google/resumable_media/_upload.py index e176fb391d8c9..76509c0ded10c 100644 --- a/packages/google-resumable-media/google/resumable_media/_upload.py +++ b/packages/google-resumable-media/google/resumable_media/_upload.py @@ -28,13 +28,10 @@ import re import sys import urllib.parse - -from google import resumable_media -from google.resumable_media import _helpers -from google.resumable_media import common - from xml.etree import ElementTree +from google import resumable_media +from google.resumable_media import _helpers, common _CONTENT_TYPE_HEADER = "content-type" _CONTENT_RANGE_TEMPLATE = "bytes {:d}-{:d}/{:d}" diff --git a/packages/google-resumable-media/google/resumable_media/requests/__init__.py b/packages/google-resumable-media/google/resumable_media/requests/__init__.py index c46a02c4543c8..cbd1aede36ff4 100644 --- a/packages/google-resumable-media/google/resumable_media/requests/__init__.py +++ b/packages/google-resumable-media/google/resumable_media/requests/__init__.py @@ -663,15 +663,19 @@ def SimpleUpload(*args, **kwargs): True """ -from google.resumable_media.requests.download import ChunkedDownload -from google.resumable_media.requests.download import Download -from google.resumable_media.requests.upload import MultipartUpload -from google.resumable_media.requests.download import RawChunkedDownload -from google.resumable_media.requests.download import RawDownload -from google.resumable_media.requests.upload import ResumableUpload -from google.resumable_media.requests.upload import SimpleUpload -from google.resumable_media.requests.upload import XMLMPUContainer -from google.resumable_media.requests.upload import XMLMPUPart +from google.resumable_media.requests.download import ( + ChunkedDownload, + Download, + RawChunkedDownload, + RawDownload, +) +from google.resumable_media.requests.upload import ( + MultipartUpload, + ResumableUpload, + SimpleUpload, + XMLMPUContainer, + XMLMPUPart, +) __all__ = [ "ChunkedDownload", diff --git a/packages/google-resumable-media/google/resumable_media/requests/_request_helpers.py b/packages/google-resumable-media/google/resumable_media/requests/_request_helpers.py index 051f0bae075d1..04b02ae895ae3 100644 --- a/packages/google-resumable-media/google/resumable_media/requests/_request_helpers.py +++ b/packages/google-resumable-media/google/resumable_media/requests/_request_helpers.py @@ -18,13 +18,12 @@ """ import http.client +import time + import requests.exceptions import urllib3.exceptions # type: ignore -import time - -from google.resumable_media import common -from google.resumable_media import _helpers +from google.resumable_media import _helpers, common _DEFAULT_RETRY_STRATEGY = common.RetryStrategy() _SINGLE_GET_CHUNK_SIZE = 8192 diff --git a/packages/google-resumable-media/google/resumable_media/requests/download.py b/packages/google-resumable-media/google/resumable_media/requests/download.py index be017f549c277..990f369a02fb8 100644 --- a/packages/google-resumable-media/google/resumable_media/requests/download.py +++ b/packages/google-resumable-media/google/resumable_media/requests/download.py @@ -14,14 +14,12 @@ """Support for downloading media from Google APIs.""" -import urllib3.response # type: ignore import http -from google.resumable_media import _download -from google.resumable_media import common -from google.resumable_media import _helpers -from google.resumable_media.requests import _request_helpers +import urllib3.response # type: ignore +from google.resumable_media import _download, _helpers, common +from google.resumable_media.requests import _request_helpers _CHECKSUM_MISMATCH = """\ Checksum mismatch while downloading: diff --git a/packages/google-resumable-media/noxfile.py b/packages/google-resumable-media/noxfile.py index 3dc0eb9222315..2f075f4a91c98 100644 --- a/packages/google-resumable-media/noxfile.py +++ b/packages/google-resumable-media/noxfile.py @@ -46,6 +46,7 @@ nox.options.sessions = [ "system", "blacken", + "format", "mypy", "doctest", ] @@ -211,6 +212,20 @@ def lint(session): "tests_async", ) + # 1. Check imports + session.run( + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + os.path.join("google", "resumable_media"), + "tests", + os.path.join("google", "_async_resumable_media"), + "tests_async", + ) + # 2. Check formatting session.run( "ruff", @@ -253,6 +268,44 @@ def blacken(session): ) +@nox.session(python=DEFAULT_PYTHON_VERSION) +def format(session): + """ + Run ruff to sort imports and format code. + """ + # 1. Install ruff (skipped automatically if you run with --no-venv) + session.install(RUFF_VERSION) + + # 2. Run Ruff to fix imports + # check --select I: Enables strict import sorting + # --fix: Applies the changes automatically + session.run( + "ruff", + "check", + "--select", + "I", + "--fix", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", # Standard Black line length + os.path.join("google", "resumable_media"), + "tests", + os.path.join("google", "_async_resumable_media"), + "tests_async", + ) + + # 3. Run Ruff to format code + session.run( + "ruff", + "format", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", # Standard Black line length + os.path.join("google", "resumable_media"), + "tests", + os.path.join("google", "_async_resumable_media"), + "tests_async", + ) + + @nox.session(python=DEFAULT_PYTHON_VERSION) def mypy(session): """Verify type hints are mypy compatible.""" diff --git a/packages/google-resumable-media/tests/system/requests/test_download.py b/packages/google-resumable-media/tests/system/requests/test_download.py index f0bd6c7e30e1d..5b9c3fcb9b72c 100644 --- a/packages/google-resumable-media/tests/system/requests/test_download.py +++ b/packages/google-resumable-media/tests/system/requests/test_download.py @@ -23,14 +23,12 @@ import google.auth.transport.requests as tr_requests # type: ignore import pytest # type: ignore -from google.resumable_media import common import google.resumable_media.requests as resumable_requests -from google.resumable_media import _helpers -from google.resumable_media.requests import _request_helpers import google.resumable_media.requests.download as download_mod +from google.resumable_media import _helpers, common +from google.resumable_media.requests import _request_helpers from tests.system import utils - CURR_DIR = os.path.dirname(os.path.realpath(__file__)) DATA_DIR = os.path.join(CURR_DIR, "..", "..", "data") PLAIN_TEXT = "text/plain" diff --git a/packages/google-resumable-media/tests/system/requests/test_upload.py b/packages/google-resumable-media/tests/system/requests/test_upload.py index 3f961bc4e7547..ecc2e6ecdcce0 100644 --- a/packages/google-resumable-media/tests/system/requests/test_upload.py +++ b/packages/google-resumable-media/tests/system/requests/test_upload.py @@ -18,17 +18,14 @@ import io import os import urllib.parse +from unittest import mock import pytest # type: ignore -from unittest import mock -from google.resumable_media import common -from google import resumable_media import google.resumable_media.requests as resumable_requests -from google.resumable_media import _helpers +from google import resumable_media +from google.resumable_media import _helpers, _upload, common from tests.system import utils -from google.resumable_media import _upload - CURR_DIR = os.path.dirname(os.path.realpath(__file__)) DATA_DIR = os.path.join(CURR_DIR, "..", "..", "data") diff --git a/packages/google-resumable-media/tests/system/utils.py b/packages/google-resumable-media/tests/system/utils.py index 7b679095dac65..1448059b72365 100644 --- a/packages/google-resumable-media/tests/system/utils.py +++ b/packages/google-resumable-media/tests/system/utils.py @@ -18,7 +18,6 @@ from test_utils.retry import RetryResult # type: ignore - BUCKET_NAME = "grpm-systest-{}".format(int(1000 * time.time())) BUCKET_POST_URL = "https://www.googleapis.com/storage/v1/b/" BUCKET_URL = "https://www.googleapis.com/storage/v1/b/{}".format(BUCKET_NAME) diff --git a/packages/google-resumable-media/tests/unit/requests/test__helpers.py b/packages/google-resumable-media/tests/unit/requests/test__helpers.py index de85991ac9e40..36cbac34abda4 100644 --- a/packages/google-resumable-media/tests/unit/requests/test__helpers.py +++ b/packages/google-resumable-media/tests/unit/requests/test__helpers.py @@ -13,10 +13,9 @@ # limitations under the License. import http.client - from unittest import mock -import pytest # type: ignore +import pytest # type: ignore import requests.exceptions import urllib3.exceptions # type: ignore diff --git a/packages/google-resumable-media/tests/unit/requests/test_download.py b/packages/google-resumable-media/tests/unit/requests/test_download.py index 57cb4ed29d7ce..6ec80c660818a 100644 --- a/packages/google-resumable-media/tests/unit/requests/test_download.py +++ b/packages/google-resumable-media/tests/unit/requests/test_download.py @@ -14,15 +14,13 @@ import http.client import io - from unittest import mock + import pytest # type: ignore -from google.resumable_media import common -from google.resumable_media import _helpers -from google.resumable_media.requests import download as download_mod +from google.resumable_media import _helpers, common from google.resumable_media.requests import _request_helpers - +from google.resumable_media.requests import download as download_mod URL_PREFIX = "https://www.googleapis.com/download/storage/v1/b/{BUCKET}/o/" EXAMPLE_URL = URL_PREFIX + "{OBJECT}?alt=media" diff --git a/packages/google-resumable-media/tests/unit/requests/test_upload.py b/packages/google-resumable-media/tests/unit/requests/test_upload.py index 18bc06d91d4f5..d890103760093 100644 --- a/packages/google-resumable-media/tests/unit/requests/test_upload.py +++ b/packages/google-resumable-media/tests/unit/requests/test_upload.py @@ -15,12 +15,12 @@ import http.client import io import json -import pytest # type: ignore import tempfile from unittest import mock -import google.resumable_media.requests.upload as upload_mod +import pytest # type: ignore +import google.resumable_media.requests.upload as upload_mod URL_PREFIX = "https://www.googleapis.com/upload/storage/v1/b/{BUCKET}/o" SIMPLE_URL = URL_PREFIX + "?uploadType=media&name={OBJECT}" diff --git a/packages/google-resumable-media/tests/unit/test__download.py b/packages/google-resumable-media/tests/unit/test__download.py index 21a232eb04cd7..85909c0582f8b 100644 --- a/packages/google-resumable-media/tests/unit/test__download.py +++ b/packages/google-resumable-media/tests/unit/test__download.py @@ -14,13 +14,11 @@ import http.client import io - from unittest import mock -import pytest # type: ignore -from google.resumable_media import _download -from google.resumable_media import common +import pytest # type: ignore +from google.resumable_media import _download, common EXAMPLE_URL = ( "https://www.googleapis.com/download/storage/v1/b/{BUCKET}/o/{OBJECT}?alt=media" diff --git a/packages/google-resumable-media/tests/unit/test__helpers.py b/packages/google-resumable-media/tests/unit/test__helpers.py index 98cbc1f99684a..856088fc5f48e 100644 --- a/packages/google-resumable-media/tests/unit/test__helpers.py +++ b/packages/google-resumable-media/tests/unit/test__helpers.py @@ -16,12 +16,11 @@ import hashlib import http.client - from unittest import mock + import pytest # type: ignore -from google.resumable_media import _helpers -from google.resumable_media import common +from google.resumable_media import _helpers, common def test_do_nothing(): diff --git a/packages/google-resumable-media/tests/unit/test__upload.py b/packages/google-resumable-media/tests/unit/test__upload.py index 5e1da37d86aa4..7ee743a2c3229 100644 --- a/packages/google-resumable-media/tests/unit/test__upload.py +++ b/packages/google-resumable-media/tests/unit/test__upload.py @@ -16,14 +16,11 @@ import io import sys import tempfile - from unittest import mock -import pytest # type: ignore -from google.resumable_media import _helpers -from google.resumable_media import _upload -from google.resumable_media import common +import pytest # type: ignore +from google.resumable_media import _helpers, _upload, common URL_PREFIX = "https://www.googleapis.com/upload/storage/v1/b/{BUCKET}/o" SIMPLE_URL = URL_PREFIX + "?uploadType=media&name={OBJECT}" diff --git a/packages/google-resumable-media/tests/unit/test_common.py b/packages/google-resumable-media/tests/unit/test_common.py index d96840c17243d..8ca192ee1f3f2 100644 --- a/packages/google-resumable-media/tests/unit/test_common.py +++ b/packages/google-resumable-media/tests/unit/test_common.py @@ -13,6 +13,7 @@ # limitations under the License. from unittest import mock + import pytest # type: ignore from google.resumable_media import common diff --git a/packages/google-resumable-media/tests_async/system/requests/conftest.py b/packages/google-resumable-media/tests_async/system/requests/conftest.py index 81c39df2ecfe6..f9acc483223e0 100644 --- a/packages/google-resumable-media/tests_async/system/requests/conftest.py +++ b/packages/google-resumable-media/tests_async/system/requests/conftest.py @@ -13,11 +13,11 @@ # limitations under the License. """py.test fixtures to be shared across multiple system test modules.""" -from tests.system import utils - -from google.auth._default_async import default_async # type: ignore import google.auth.transport._aiohttp_requests as tr_requests # type: ignore import pytest # type: ignore +from google.auth._default_async import default_async # type: ignore + +from tests.system import utils async def ensure_bucket(transport): diff --git a/packages/google-resumable-media/tests_async/system/requests/test_download.py b/packages/google-resumable-media/tests_async/system/requests/test_download.py index 483d8598698dc..5c5ac7d8582e8 100644 --- a/packages/google-resumable-media/tests_async/system/requests/test_download.py +++ b/packages/google-resumable-media/tests_async/system/requests/test_download.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import asyncio import base64 import copy import hashlib @@ -19,19 +20,16 @@ import io import os -import asyncio -from google.auth._default_async import default_async # type: ignore import google.auth.transport._aiohttp_requests as tr_requests # type: ignore import multidict # type: ignore import pytest # type: ignore +from google.auth._default_async import default_async # type: ignore import google._async_resumable_media.requests as resumable_requests -from google.resumable_media import _helpers import google._async_resumable_media.requests.download as download_mod -from google.resumable_media import common +from google.resumable_media import _helpers, common from tests.system import utils - CURR_DIR = os.path.dirname(os.path.realpath(__file__)) DATA_DIR = os.path.join(CURR_DIR, "..", "..", "data") PLAIN_TEXT = "text/plain" diff --git a/packages/google-resumable-media/tests_async/system/requests/test_upload.py b/packages/google-resumable-media/tests_async/system/requests/test_upload.py index fb8ba51a6aa7b..d9acf412f33d2 100644 --- a/packages/google-resumable-media/tests_async/system/requests/test_upload.py +++ b/packages/google-resumable-media/tests_async/system/requests/test_upload.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import asyncio import base64 import hashlib import http.client @@ -19,17 +20,14 @@ import os import urllib.parse -import asyncio import mock import pytest # type: ignore -from google.resumable_media import common -from google import _async_resumable_media import google._async_resumable_media.requests as resumable_requests -from google.resumable_media import _helpers +from google import _async_resumable_media +from google.resumable_media import _helpers, common from tests.system import utils - CURR_DIR = os.path.dirname(os.path.realpath(__file__)) DATA_DIR = os.path.join(CURR_DIR, "..", "..", "data") ICO_FILE = os.path.realpath(os.path.join(DATA_DIR, "favicon.ico")) diff --git a/packages/google-resumable-media/tests_async/system/utils.py b/packages/google-resumable-media/tests_async/system/utils.py index 620b2c99c1d38..25362c8a3965d 100644 --- a/packages/google-resumable-media/tests_async/system/utils.py +++ b/packages/google-resumable-media/tests_async/system/utils.py @@ -16,7 +16,6 @@ import hashlib import time - BUCKET_NAME = "grpm-systest-{}".format(int(1000 * time.time())) BUCKET_POST_URL = "https://www.googleapis.com/storage/v1/b/" BUCKET_URL = "https://www.googleapis.com/storage/v1/b/{}".format(BUCKET_NAME) diff --git a/packages/google-resumable-media/tests_async/unit/requests/test_download.py b/packages/google-resumable-media/tests_async/unit/requests/test_download.py index 16c23a49327ad..08e51109372b3 100644 --- a/packages/google-resumable-media/tests_async/unit/requests/test_download.py +++ b/packages/google-resumable-media/tests_async/unit/requests/test_download.py @@ -19,10 +19,9 @@ import mock import pytest # type: ignore - -from google.resumable_media import common from google._async_resumable_media import _helpers from google._async_resumable_media.requests import download as download_mod +from google.resumable_media import common from tests.unit.requests import test_download as sync_test EXPECTED_TIMEOUT = aiohttp.ClientTimeout( diff --git a/packages/google-resumable-media/tests_async/unit/test__download.py b/packages/google-resumable-media/tests_async/unit/test__download.py index 8dfd13040a8e9..364e2069f3fe3 100644 --- a/packages/google-resumable-media/tests_async/unit/test__download.py +++ b/packages/google-resumable-media/tests_async/unit/test__download.py @@ -23,7 +23,6 @@ from google.resumable_media import common from tests.unit import test__download as sync_test - EXAMPLE_URL = sync_test.EXAMPLE_URL diff --git a/packages/google-resumable-media/tests_async/unit/test__upload.py b/packages/google-resumable-media/tests_async/unit/test__upload.py index 2f7d0e987c6eb..dbb113b0e887a 100644 --- a/packages/google-resumable-media/tests_async/unit/test__upload.py +++ b/packages/google-resumable-media/tests_async/unit/test__upload.py @@ -20,8 +20,8 @@ import pytest # type: ignore from google._async_resumable_media import _upload -from google.resumable_media import common from google.resumable_media import _helpers as sync_helpers +from google.resumable_media import common from tests.unit import test__upload as sync_test diff --git a/packages/pandas-gbq/noxfile.py b/packages/pandas-gbq/noxfile.py index 0021226416cd9..ca831333a5b25 100644 --- a/packages/pandas-gbq/noxfile.py +++ b/packages/pandas-gbq/noxfile.py @@ -136,10 +136,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install("flake8", BLACK_VERSION) + session.install("flake8", RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) session.run("flake8", "pandas_gbq", "tests") diff --git a/packages/pandas-gbq/pandas_gbq/core/biglake.py b/packages/pandas-gbq/pandas_gbq/core/biglake.py index 63d868ee6c38c..339e4dd27fb18 100644 --- a/packages/pandas-gbq/pandas_gbq/core/biglake.py +++ b/packages/pandas-gbq/pandas_gbq/core/biglake.py @@ -69,9 +69,9 @@ def get_table_metadata( ) ) ) - assert ( - len(count_rows) == 1 - ), "got unexpected query response when determining number of rows" + assert len(count_rows) == 1, ( + "got unexpected query response when determining number of rows" + ) total_rows = count_rows[0].total_rows return BigLakeTableMetadata( diff --git a/packages/pandas-gbq/pandas_gbq/core/pandas.py b/packages/pandas-gbq/pandas_gbq/core/pandas.py index aceaa29b5b919..f656e7a807187 100644 --- a/packages/pandas-gbq/pandas_gbq/core/pandas.py +++ b/packages/pandas-gbq/pandas_gbq/core/pandas.py @@ -3,9 +3,9 @@ # license that can be found in the LICENSE file. import itertools +import typing import pandas -import typing def list_columns_and_indexes(dataframe, index=True): diff --git a/packages/pandas-gbq/pandas_gbq/gbq.py b/packages/pandas-gbq/pandas_gbq/gbq.py index ec6a7e9308aaf..09b40341860e2 100644 --- a/packages/pandas-gbq/pandas_gbq/gbq.py +++ b/packages/pandas-gbq/pandas_gbq/gbq.py @@ -66,7 +66,9 @@ def _test_google_api_imports(): # This import is solely to test if the package is installed, so we ignore the "unused import" warning. # Remove this comment and the ignore pragma upon completing: # https://github.com/googleapis/google-cloud-python/issues/17045 - from google_auth_oauthlib.flow import InstalledAppFlow # type: ignore[import-untyped] # noqa: F401 + from google_auth_oauthlib.flow import ( # type: ignore[import-untyped] # noqa: F401 + InstalledAppFlow, + ) except ImportError as ex: # pragma: NO COVER raise ImportError("pandas-gbq requires google-auth-oauthlib") from ex diff --git a/packages/pandas-gbq/pandas_gbq/gbq_connector.py b/packages/pandas-gbq/pandas_gbq/gbq_connector.py index 68d5aaad5aa78..5298a2dd58418 100644 --- a/packages/pandas-gbq/pandas_gbq/gbq_connector.py +++ b/packages/pandas-gbq/pandas_gbq/gbq_connector.py @@ -190,8 +190,7 @@ def run_query( job_config_dict = { "query": { - "useLegacySql": self.dialect - == "legacy" + "useLegacySql": self.dialect == "legacy" # 'allowLargeResults', 'createDisposition', # 'preserveNulls', destinationTable, useQueryCache } diff --git a/packages/pandas-gbq/tests/system/conftest.py b/packages/pandas-gbq/tests/system/conftest.py index e6a4ec6844ea6..29ab096e417c3 100644 --- a/packages/pandas-gbq/tests/system/conftest.py +++ b/packages/pandas-gbq/tests/system/conftest.py @@ -149,9 +149,7 @@ def tokyo_table(bigquery_client, tokyo_dataset): 2000 + CAST(18 * RAND() as INT64) as year, IF(RAND() > 0.5,"foo","bar") as token FROM UNNEST(GENERATE_ARRAY(0,5,1)) as r - """.format( - tokyo_dataset, table_id - ), + """.format(tokyo_dataset, table_id), location="asia-northeast1", ).result() return table_id diff --git a/packages/pandas-gbq/tests/unit/test_arrow.py b/packages/pandas-gbq/tests/unit/test_arrow.py index c4b55d5a12369..3b00ed7eed29c 100644 --- a/packages/pandas-gbq/tests/unit/test_arrow.py +++ b/packages/pandas-gbq/tests/unit/test_arrow.py @@ -1,9 +1,10 @@ from unittest import mock -import pandas_gbq.arrow import pyarrow as pa import pytest +import pandas_gbq.arrow + def test_from_read_rows_response_valid_message_returns_record_batch(): schema = pa.schema([("id", pa.int64()), ("name", pa.string())]) diff --git a/packages/proto-plus/docs/conf.py b/packages/proto-plus/docs/conf.py index bd3b28de7b315..5afaa8a82920e 100644 --- a/packages/proto-plus/docs/conf.py +++ b/packages/proto-plus/docs/conf.py @@ -17,7 +17,6 @@ import proto - sys.path.insert(0, os.path.abspath("..")) diff --git a/packages/proto-plus/noxfile.py b/packages/proto-plus/noxfile.py index 5f56955863c84..b506d1f042881 100644 --- a/packages/proto-plus/noxfile.py +++ b/packages/proto-plus/noxfile.py @@ -327,10 +327,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install("flake8", BLACK_VERSION) + session.install("flake8", RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) diff --git a/packages/proto-plus/proto/__init__.py b/packages/proto-plus/proto/__init__.py index 8780991c0426b..85155a8c0a7bf 100644 --- a/packages/proto-plus/proto/__init__.py +++ b/packages/proto-plus/proto/__init__.py @@ -13,16 +13,13 @@ # limitations under the License. from .enums import Enum -from .fields import Field -from .fields import MapField -from .fields import RepeatedField +from .fields import Field, MapField, RepeatedField from .marshal import Marshal from .message import Message from .modules import define_module as module from .primitives import ProtoType from .version import __version__ - DOUBLE = ProtoType.DOUBLE FLOAT = ProtoType.FLOAT INT64 = ProtoType.INT64 diff --git a/packages/proto-plus/proto/_file_info.py b/packages/proto-plus/proto/_file_info.py index 537eeaf4556aa..6337a4792204d 100644 --- a/packages/proto-plus/proto/_file_info.py +++ b/packages/proto-plus/proto/_file_info.py @@ -16,10 +16,7 @@ import inspect import logging -from google.protobuf import descriptor_pb2 -from google.protobuf import descriptor_pool -from google.protobuf import message -from google.protobuf import reflection +from google.protobuf import descriptor_pb2, descriptor_pool, message, reflection from proto.marshal.rules.message import MessageRule diff --git a/packages/proto-plus/proto/datetime_helpers.py b/packages/proto-plus/proto/datetime_helpers.py index d4cfaca6f83a7..2e2c561664445 100644 --- a/packages/proto-plus/proto/datetime_helpers.py +++ b/packages/proto-plus/proto/datetime_helpers.py @@ -19,7 +19,6 @@ from google.protobuf import timestamp_pb2 - _UTC_EPOCH = datetime.datetime.fromtimestamp(0, datetime.timezone.utc) _RFC3339_MICROS = "%Y-%m-%dT%H:%M:%S.%fZ" diff --git a/packages/proto-plus/proto/enums.py b/packages/proto-plus/proto/enums.py index 6ddf63a2cce54..ab7e8b6e65f21 100644 --- a/packages/proto-plus/proto/enums.py +++ b/packages/proto-plus/proto/enums.py @@ -16,8 +16,7 @@ from google.protobuf import descriptor_pb2 -from proto import _file_info -from proto import _package_info +from proto import _file_info, _package_info from proto.marshal.rules.enums import EnumRule diff --git a/packages/proto-plus/proto/marshal/__init__.py b/packages/proto-plus/proto/marshal/__init__.py index 621ea3695f931..2e9fdcd1cdfc3 100644 --- a/packages/proto-plus/proto/marshal/__init__.py +++ b/packages/proto-plus/proto/marshal/__init__.py @@ -14,5 +14,4 @@ from .marshal import Marshal - __all__ = ("Marshal",) diff --git a/packages/proto-plus/proto/marshal/collections/__init__.py b/packages/proto-plus/proto/marshal/collections/__init__.py index 4b80a546c26a5..70906b51a741b 100644 --- a/packages/proto-plus/proto/marshal/collections/__init__.py +++ b/packages/proto-plus/proto/marshal/collections/__init__.py @@ -13,9 +13,7 @@ # limitations under the License. from .maps import MapComposite -from .repeated import Repeated -from .repeated import RepeatedComposite - +from .repeated import Repeated, RepeatedComposite __all__ = ( "MapComposite", diff --git a/packages/proto-plus/proto/marshal/rules/dates.py b/packages/proto-plus/proto/marshal/rules/dates.py index a4b43043de08d..78abaab2e1dad 100644 --- a/packages/proto-plus/proto/marshal/rules/dates.py +++ b/packages/proto-plus/proto/marshal/rules/dates.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from datetime import datetime -from datetime import timedelta +from datetime import datetime, timedelta + +from google.protobuf import duration_pb2, timestamp_pb2 -from google.protobuf import duration_pb2 -from google.protobuf import timestamp_pb2 from proto import datetime_helpers diff --git a/packages/proto-plus/proto/marshal/rules/enums.py b/packages/proto-plus/proto/marshal/rules/enums.py index 9cfc312764b6a..cedec9dbf84a8 100644 --- a/packages/proto-plus/proto/marshal/rules/enums.py +++ b/packages/proto-plus/proto/marshal/rules/enums.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Type import enum import warnings +from typing import Type class EnumRule: diff --git a/packages/proto-plus/proto/marshal/rules/struct.py b/packages/proto-plus/proto/marshal/rules/struct.py index 0e34587b26b19..802702cd4da80 100644 --- a/packages/proto-plus/proto/marshal/rules/struct.py +++ b/packages/proto-plus/proto/marshal/rules/struct.py @@ -16,8 +16,7 @@ from google.protobuf import struct_pb2 -from proto.marshal.collections import maps -from proto.marshal.collections import repeated +from proto.marshal.collections import maps, repeated class ValueRule: diff --git a/packages/proto-plus/proto/message.py b/packages/proto-plus/proto/message.py index 1eb36a977faa0..1e5cd9cca6230 100644 --- a/packages/proto-plus/proto/message.py +++ b/packages/proto-plus/proto/message.py @@ -16,24 +16,19 @@ import collections.abc import copy import re -from typing import Any, Dict, List, Optional, Type import warnings +from typing import Any, Dict, List, Optional, Type import google.protobuf -from google.protobuf import descriptor_pb2 -from google.protobuf import message +from google.protobuf import descriptor_pb2, message from google.protobuf.json_format import MessageToDict, MessageToJson, Parse -from proto import _file_info -from proto import _package_info -from proto.fields import Field -from proto.fields import MapField -from proto.fields import RepeatedField +from proto import _file_info, _package_info +from proto.fields import Field, MapField, RepeatedField from proto.marshal import Marshal from proto.primitives import ProtoType from proto.utils import has_upb - PROTOBUF_VERSION = google.protobuf.__version__ # extract the major version code diff --git a/packages/proto-plus/proto/modules.py b/packages/proto-plus/proto/modules.py index 45864a937c589..4c5a3fa0e8280 100644 --- a/packages/proto-plus/proto/modules.py +++ b/packages/proto-plus/proto/modules.py @@ -12,9 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Set import collections - +from typing import Set _ProtoModule = collections.namedtuple( "ProtoModule", diff --git a/packages/proto-plus/setup.py b/packages/proto-plus/setup.py index 12e9ed26dcdf8..402eaafd62a7a 100644 --- a/packages/proto-plus/setup.py +++ b/packages/proto-plus/setup.py @@ -15,5 +15,4 @@ from setuptools import setup - setup() diff --git a/packages/proto-plus/tests/conftest.py b/packages/proto-plus/tests/conftest.py index 6caeb50d75c5d..14b8fe3b7d681 100644 --- a/packages/proto-plus/tests/conftest.py +++ b/packages/proto-plus/tests/conftest.py @@ -15,14 +15,10 @@ import importlib from unittest import mock -from google.protobuf import descriptor_pool -from google.protobuf import message -from google.protobuf import reflection -from google.protobuf import symbol_database +from google.protobuf import descriptor_pool, message, reflection, symbol_database from proto._file_info import _FileInfo -from proto.marshal import Marshal -from proto.marshal import rules +from proto.marshal import Marshal, rules from proto.utils import has_upb diff --git a/packages/proto-plus/tests/mollusc.py b/packages/proto-plus/tests/mollusc.py index c92f161a406dc..88237ffe7f202 100644 --- a/packages/proto-plus/tests/mollusc.py +++ b/packages/proto-plus/tests/mollusc.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import proto import zone +import proto + __protobuf__ = proto.module( package="ocean.mollusc.v1", manifest={ diff --git a/packages/proto-plus/tests/test_datetime_helpers.py b/packages/proto-plus/tests/test_datetime_helpers.py index 264b5296980a9..d7ec98136a26c 100644 --- a/packages/proto-plus/tests/test_datetime_helpers.py +++ b/packages/proto-plus/tests/test_datetime_helpers.py @@ -17,10 +17,9 @@ import pytest import pytz - -from proto import datetime_helpers from google.protobuf import timestamp_pb2 +from proto import datetime_helpers ONE_MINUTE_IN_MICROSECONDS = 60 * 1e6 diff --git a/packages/proto-plus/tests/test_enum_total_ordering.py b/packages/proto-plus/tests/test_enum_total_ordering.py index 584a1831c58ad..dd9aa87df4a42 100644 --- a/packages/proto-plus/tests/test_enum_total_ordering.py +++ b/packages/proto-plus/tests/test_enum_total_ordering.py @@ -12,9 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest - import enums_test +import pytest def test_total_ordering_w_same_enum_type(): diff --git a/packages/proto-plus/tests/test_fields_enum.py b/packages/proto-plus/tests/test_fields_enum.py index aa66dcfca4448..d5c994d1a7350 100644 --- a/packages/proto-plus/tests/test_fields_enum.py +++ b/packages/proto-plus/tests/test_fields_enum.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import proto import sys +import proto + def test_outer_enum_init(): class Foo(proto.Message): diff --git a/packages/proto-plus/tests/test_fields_mitigate_collision.py b/packages/proto-plus/tests/test_fields_mitigate_collision.py index 07eac5ff7ce4c..d61c313d7af30 100644 --- a/packages/proto-plus/tests/test_fields_mitigate_collision.py +++ b/packages/proto-plus/tests/test_fields_mitigate_collision.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import proto import pytest +import proto + # Underscores may be appended to field names # that collide with python or proto-plus keywords. diff --git a/packages/proto-plus/tests/test_fields_repeated_composite.py b/packages/proto-plus/tests/test_fields_repeated_composite.py index bdb7e62d0f1a2..966fa88efa3ce 100644 --- a/packages/proto-plus/tests/test_fields_repeated_composite.py +++ b/packages/proto-plus/tests/test_fields_repeated_composite.py @@ -16,7 +16,6 @@ from enum import Enum import pytest - from google.protobuf import timestamp_pb2 import proto diff --git a/packages/proto-plus/tests/test_file_info_salting.py b/packages/proto-plus/tests/test_file_info_salting.py index 4fce9105270ee..bdce86a189f67 100644 --- a/packages/proto-plus/tests/test_file_info_salting.py +++ b/packages/proto-plus/tests/test_file_info_salting.py @@ -15,9 +15,9 @@ import collections -import proto from google.protobuf import descriptor_pb2 +import proto from proto import _file_info, _package_info diff --git a/packages/proto-plus/tests/test_file_info_salting_with_manifest.py b/packages/proto-plus/tests/test_file_info_salting_with_manifest.py index 2d8f75eb48c7c..f61e18290e117 100644 --- a/packages/proto-plus/tests/test_file_info_salting_with_manifest.py +++ b/packages/proto-plus/tests/test_file_info_salting_with_manifest.py @@ -15,9 +15,9 @@ import collections -import proto from google.protobuf import descriptor_pb2 +import proto from proto import _file_info, _package_info PACKAGE = "a.test.package.salting.with.manifest" diff --git a/packages/proto-plus/tests/test_json.py b/packages/proto-plus/tests/test_json.py index b8c40ccff7cc6..e397195b045c4 100644 --- a/packages/proto-plus/tests/test_json.py +++ b/packages/proto-plus/tests/test_json.py @@ -12,12 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import pytest import re -import proto +import pytest from google.protobuf.json_format import ParseError +import proto + def test_message_to_json(): class Squid(proto.Message): @@ -257,8 +258,8 @@ class Squid(proto.Message): "expect_proto_7_plus", [True, False], ids=["proto >= 7", "proto <= 6"] ) def test_json_float_precision(expect_proto_7_plus): - if ((expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) < 7)) or ( - (not expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) >= 7) + if (expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) < 7) or ( + not expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) >= 7 ): pytest.skip("installed proto version does not match test") diff --git a/packages/proto-plus/tests/test_marshal_register.py b/packages/proto-plus/tests/test_marshal_register.py index 3ca1a2a88d73a..73c8e81a7f15d 100644 --- a/packages/proto-plus/tests/test_marshal_register.py +++ b/packages/proto-plus/tests/test_marshal_register.py @@ -13,7 +13,6 @@ # limitations under the License. import pytest - from google.protobuf import empty_pb2 from proto.marshal.marshal import BaseMarshal diff --git a/packages/proto-plus/tests/test_marshal_strict.py b/packages/proto-plus/tests/test_marshal_strict.py index 6ff3187ae7b58..fa6c379329a40 100644 --- a/packages/proto-plus/tests/test_marshal_strict.py +++ b/packages/proto-plus/tests/test_marshal_strict.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from proto.marshal.marshal import BaseMarshal import pytest +from proto.marshal.marshal import BaseMarshal + def test_strict_to_proto(): m = BaseMarshal() diff --git a/packages/proto-plus/tests/test_marshal_types_dates.py b/packages/proto-plus/tests/test_marshal_types_dates.py index d3fad95fbb4b9..7cf9ffd776d50 100644 --- a/packages/proto-plus/tests/test_marshal_types_dates.py +++ b/packages/proto-plus/tests/test_marshal_types_dates.py @@ -12,16 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from datetime import datetime -from datetime import timedelta -from datetime import timezone +from datetime import datetime, timedelta, timezone -from google.protobuf import duration_pb2 -from google.protobuf import timestamp_pb2 +from google.protobuf import duration_pb2, timestamp_pb2 import proto -from proto.marshal.marshal import BaseMarshal from proto.datetime_helpers import DatetimeWithNanoseconds +from proto.marshal.marshal import BaseMarshal def test_timestamp_read(): diff --git a/packages/proto-plus/tests/test_marshal_types_enum.py b/packages/proto-plus/tests/test_marshal_types_enum.py index acb554618dfaf..becafb105831f 100644 --- a/packages/proto-plus/tests/test_marshal_types_enum.py +++ b/packages/proto-plus/tests/test_marshal_types_enum.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from unittest import mock import warnings +from unittest import mock import proto from proto.marshal.rules.enums import EnumRule diff --git a/packages/proto-plus/tests/test_marshal_types_struct.py b/packages/proto-plus/tests/test_marshal_types_struct.py index 8ca2cde8a3c14..bbc5afae9c970 100644 --- a/packages/proto-plus/tests/test_marshal_types_struct.py +++ b/packages/proto-plus/tests/test_marshal_types_struct.py @@ -13,7 +13,6 @@ # limitations under the License. import pytest - from google.protobuf import struct_pb2 import proto diff --git a/packages/proto-plus/tests/test_message.py b/packages/proto-plus/tests/test_message.py index 0649eae5ee9b6..836b4596dc694 100644 --- a/packages/proto-plus/tests/test_message.py +++ b/packages/proto-plus/tests/test_message.py @@ -13,6 +13,7 @@ # limitations under the License. import itertools + import pytest import proto @@ -330,8 +331,8 @@ class Color(proto.Enum): "expect_proto_7_plus", [True, False], ids=["proto >= 7", "proto <= 6"] ) def test_serialize_to_dict_float_precision(expect_proto_7_plus): - if ((expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) < 7)) or ( - (not expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) >= 7) + if (expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) < 7) or ( + not expect_proto_7_plus and int(proto.message._PROTOBUF_MAJOR_VERSION) >= 7 ): pytest.skip("installed proto version does not match test") diff --git a/packages/proto-plus/tests/test_message_filename_with_and_without_manifest.py b/packages/proto-plus/tests/test_message_filename_with_and_without_manifest.py index 8e1b34766dfa3..cdd5ea532a7bc 100644 --- a/packages/proto-plus/tests/test_message_filename_with_and_without_manifest.py +++ b/packages/proto-plus/tests/test_message_filename_with_and_without_manifest.py @@ -14,7 +14,6 @@ import proto - PACKAGE = "a.test.package.with.and.without.manifest" __protobuf__ = proto.module( package=PACKAGE, diff --git a/packages/proto-plus/tests/test_modules.py b/packages/proto-plus/tests/test_modules.py index 5021aab16483d..4c8eb6b773479 100644 --- a/packages/proto-plus/tests/test_modules.py +++ b/packages/proto-plus/tests/test_modules.py @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from unittest import mock import inspect import sys +from unittest import mock from google.protobuf import wrappers_pb2 diff --git a/packages/proto-plus/tests/zone.py b/packages/proto-plus/tests/zone.py index 90bea6a878c0c..d0c4d3cbe8dc1 100644 --- a/packages/proto-plus/tests/zone.py +++ b/packages/proto-plus/tests/zone.py @@ -15,7 +15,6 @@ import proto - __protobuf__ = proto.module( package="ocean.zone.v1", manifest={ diff --git a/packages/sqlalchemy-bigquery/noxfile.py b/packages/sqlalchemy-bigquery/noxfile.py index 506a5bfcfb8fd..aa69ee0bc74b3 100644 --- a/packages/sqlalchemy-bigquery/noxfile.py +++ b/packages/sqlalchemy-bigquery/noxfile.py @@ -179,10 +179,24 @@ def lint(session): Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ - session.install(FLAKE8_VERSION, BLACK_VERSION) + session.install(FLAKE8_VERSION, RUFF_VERSION) + # 1. Check imports session.run( - "black", + "ruff", + "check", + "--select", + "I", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", + *LINT_PATHS, + ) + # 2. Check formatting + session.run( + "ruff", + "format", "--check", + f"--target-version=py{UNIT_TEST_PYTHON_VERSIONS[0].replace('.', '')}", + "--line-length=88", *LINT_PATHS, ) diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/__init__.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/__init__.py index 7473f3326a460..1d3bdb51e0820 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/__init__.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/__init__.py @@ -20,6 +20,7 @@ SQLAlchemy dialect for Google BigQuery """ +import sys import warnings from ._types import ( @@ -43,7 +44,6 @@ ) from .base import BigQueryDialect, dialect from .version import __version__ -import sys # Now that support for Python 3.7, 3.8 and 3.9 has been removed, we don't expect the # following check to succeed. The warning is only included for robustness. diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py index 41513a57fe8f4..2977860d122ff 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_helpers.py @@ -10,11 +10,11 @@ import re from typing import Optional -from google.api_core import client_info import google.auth +import sqlalchemy +from google.api_core import client_info from google.cloud import bigquery from google.oauth2 import service_account -import sqlalchemy USER_AGENT_TEMPLATE = "sqlalchemy/{}" SCOPES = ( diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_struct.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_struct.py index 5fe4ccd51f901..5b9678d635025 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_struct.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_struct.py @@ -17,12 +17,12 @@ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -from sqlalchemy.sql import operators import sqlalchemy.sql.coercions import sqlalchemy.sql.default_comparator import sqlalchemy.sql.roles import sqlalchemy.sql.sqltypes import sqlalchemy.types +from sqlalchemy.sql import operators # from . import base # Moved to _get_subtype_col_spec to break circular import diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_types.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_types.py index 1f16b8fca5c8e..dc0fcd01f2e57 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_types.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_types.py @@ -17,9 +17,9 @@ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -from google.cloud.bigquery.schema import SchemaField import sqlalchemy.types import sqlalchemy.util +from google.cloud.bigquery.schema import SchemaField try: from .geography import GEOGRAPHY diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py index 2b63363182676..e2922890cea0a 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py @@ -20,14 +20,21 @@ """Integration between SQLAlchemy and BigQuery.""" import datetime -from decimal import Decimal import operator import random import re import uuid +from decimal import Decimal -from google import auth import google.api_core.exceptions +import packaging.version +import sqlalchemy +import sqlalchemy.sql.expression +import sqlalchemy.sql.functions +import sqlalchemy.sql.sqltypes +import sqlalchemy.sql.type_api +import sqlalchemy_bigquery_vendored.sqlalchemy.postgresql.base as vendored_postgresql +from google import auth from google.api_core.exceptions import NotFound from google.cloud.bigquery import ConnectionProperty, QueryJobConfig, dbapi from google.cloud.bigquery.table import ( @@ -35,8 +42,6 @@ TableReference, TimePartitioning, ) -import packaging.version -import sqlalchemy from sqlalchemy import util from sqlalchemy.engine.base import Engine from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext @@ -49,14 +54,9 @@ IdentifierPreparer, SQLCompiler, ) -import sqlalchemy.sql.expression -import sqlalchemy.sql.functions from sqlalchemy.sql.schema import Column, Table from sqlalchemy.sql.selectable import CTE -import sqlalchemy.sql.sqltypes from sqlalchemy.sql.sqltypes import Integer, NullType, Numeric, String -import sqlalchemy.sql.type_api -import sqlalchemy_bigquery_vendored.sqlalchemy.postgresql.base as vendored_postgresql from . import _helpers, _types from .parse_url import parse_url diff --git a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/geography.py b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/geography.py index 744bfc803c703..2c1b20d559aa9 100644 --- a/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/geography.py +++ b/packages/sqlalchemy-bigquery/sqlalchemy_bigquery/geography.py @@ -19,9 +19,9 @@ import geoalchemy2 import geoalchemy2.functions +import sqlalchemy.ext.compiler from geoalchemy2.shape import to_shape from shapely import wkb, wkt -import sqlalchemy.ext.compiler from sqlalchemy.sql.elements import BindParameter SRID = 4326 # WGS84, https://spatialreference.org/ref/epsg/wgs-84/ diff --git a/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/conftest.py b/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/conftest.py index b55d08758fceb..c5d69ced03dd6 100644 --- a/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/conftest.py +++ b/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/conftest.py @@ -22,6 +22,7 @@ import traceback import google.cloud.bigquery.dbapi.connection +import test_utils.prefixer from sqlalchemy.testing import config from sqlalchemy.testing.plugin.pytestplugin import * # noqa from sqlalchemy.testing.plugin.pytestplugin import ( @@ -30,7 +31,6 @@ from sqlalchemy.testing.plugin.pytestplugin import ( pytest_sessionstart as _pytest_sessionstart, ) -import test_utils.prefixer import sqlalchemy_bigquery.base diff --git a/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/test_dialect_compliance.py b/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/test_dialect_compliance.py index 18983cef6e73e..dde343c624290 100644 --- a/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/test_dialect_compliance.py +++ b/packages/sqlalchemy-bigquery/tests/sqlalchemy_dialect_compliance/test_dialect_compliance.py @@ -25,33 +25,34 @@ import pytest import pytz import sqlalchemy -from sqlalchemy import and_ import sqlalchemy.sql.sqltypes +import sqlalchemy.testing.suite.test_types +from sqlalchemy import and_ from sqlalchemy.testing import config, util from sqlalchemy.testing.assertions import eq_ +from sqlalchemy.testing.suite import * # noqa from sqlalchemy.testing.suite import ( + Column, + DistinctOnTest, + HasIndexTest, + IdentityAutoincrementTest, Integer, LongNameBlowoutTest, PostCompileParamsTest, QuotedNameArgumentTest, -) -from sqlalchemy.testing.suite import ( + String, + Table, WindowFunctionTest, bindparam, exists, select, testing, ) -from sqlalchemy.testing.suite import * # noqa from sqlalchemy.testing.suite import CTETest as _CTETest -from sqlalchemy.testing.suite import Column -from sqlalchemy.testing.suite import DistinctOnTest -from sqlalchemy.testing.suite import ExistsTest as _ExistsTest -from sqlalchemy.testing.suite import HasIndexTest, IdentityAutoincrementTest -from sqlalchemy.testing.suite import InsertBehaviorTest as _InsertBehaviorTest -from sqlalchemy.testing.suite import String, Table from sqlalchemy.testing.suite import DifficultParametersTest as _DifficultParametersTest +from sqlalchemy.testing.suite import ExistsTest as _ExistsTest from sqlalchemy.testing.suite import FetchLimitOffsetTest as _FetchLimitOffsetTest +from sqlalchemy.testing.suite import InsertBehaviorTest as _InsertBehaviorTest from sqlalchemy.testing.suite import SimpleUpdateDeleteTest as _SimpleUpdateDeleteTest from sqlalchemy.testing.suite import ( TimestampMicrosecondsTest as _TimestampMicrosecondsTest, @@ -62,7 +63,6 @@ ComponentReflectionTestExtra, HasTableTest, ) -import sqlalchemy.testing.suite.test_types from sqlalchemy.testing.suite.test_types import ArrayTest if packaging.version.parse(sqlalchemy.__version__) >= packaging.version.parse("2.0"): @@ -637,8 +637,12 @@ def test_no_results_for_non_returning_insert(cls): del DistinctOnTest # expects unquoted table names. del HasIndexTest # BQ doesn't do the indexes that SQLA is loooking for. del IdentityAutoincrementTest # BQ doesn't do autoincrement -del LongNameBlowoutTest # Requires features (indexes, primary keys, etc., that BigQuery doesn't have. -del PostCompileParamsTest # BQ adds backticks to bind parameters, causing failure of tests TODO: fix this? +del ( + LongNameBlowoutTest +) # Requires features (indexes, primary keys, etc., that BigQuery doesn't have. +del ( + PostCompileParamsTest +) # BQ adds backticks to bind parameters, causing failure of tests TODO: fix this? del QuotedNameArgumentTest # Quotes aren't allowed in BigQuery table names. del ( WindowFunctionTest.test_window_rows_between diff --git a/packages/sqlalchemy-bigquery/tests/system/conftest.py b/packages/sqlalchemy-bigquery/tests/system/conftest.py index 966fde7cdb357..5b6d27698bf6c 100644 --- a/packages/sqlalchemy-bigquery/tests/system/conftest.py +++ b/packages/sqlalchemy-bigquery/tests/system/conftest.py @@ -20,12 +20,12 @@ import pathlib from typing import List -from google.api_core import exceptions -from google.cloud import bigquery import pytest import sqlalchemy import test_utils.prefixer import test_utils.retry +from google.api_core import exceptions +from google.cloud import bigquery from sqlalchemy_bigquery import BigQueryDialect diff --git a/packages/sqlalchemy-bigquery/tests/system/test_alembic.py b/packages/sqlalchemy-bigquery/tests/system/test_alembic.py index 76da691bea979..a35b369625bf8 100644 --- a/packages/sqlalchemy-bigquery/tests/system/test_alembic.py +++ b/packages/sqlalchemy-bigquery/tests/system/test_alembic.py @@ -20,8 +20,8 @@ import contextlib import google.api_core.exceptions -from google.cloud.bigquery import SchemaField, TimePartitioning import pytest +from google.cloud.bigquery import SchemaField, TimePartitioning from sqlalchemy import Column, DateTime, Integer, Numeric, String alembic = pytest.importorskip("alembic") diff --git a/packages/sqlalchemy-bigquery/tests/system/test_sqlalchemy_bigquery.py b/packages/sqlalchemy-bigquery/tests/system/test_sqlalchemy_bigquery.py index 757863f9637b3..6947e61707ae0 100644 --- a/packages/sqlalchemy-bigquery/tests/system/test_sqlalchemy_bigquery.py +++ b/packages/sqlalchemy-bigquery/tests/system/test_sqlalchemy_bigquery.py @@ -21,10 +21,10 @@ import datetime import decimal -from google.cloud.bigquery import TimePartitioning import packaging.version import pytest import sqlalchemy +from google.cloud.bigquery import TimePartitioning from sqlalchemy import ( Column, MetaData, diff --git a/packages/sqlalchemy-bigquery/tests/unit/conftest.py b/packages/sqlalchemy-bigquery/tests/unit/conftest.py index fa186379bf3f5..64691da333909 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/conftest.py +++ b/packages/sqlalchemy-bigquery/tests/unit/conftest.py @@ -68,7 +68,7 @@ def ex(sql, *args, **kw): conn.ex = ex - ex("create table comments" " (key string primary key, comment string)") + ex("create table comments (key string primary key, comment string)") # Modernize faux_conn for SQLAlchemy 2.0+ by allowing it to execute # Compiled objects (common in this test suite) diff --git a/packages/sqlalchemy-bigquery/tests/unit/fauxdbi.py b/packages/sqlalchemy-bigquery/tests/unit/fauxdbi.py index 1d1bccdd31c45..ec8980aa9946d 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/fauxdbi.py +++ b/packages/sqlalchemy-bigquery/tests/unit/fauxdbi.py @@ -432,8 +432,7 @@ def _get_field( def __get_comments(self, cursor, table_name): cursor.execute( - f"select key, comment" - f" from comments where key like {repr(table_name + '%')}" + f"select key, comment from comments where key like {repr(table_name + '%')}" ) return {key.split(",")[1]: comment for key, comment in cursor} diff --git a/packages/sqlalchemy-bigquery/tests/unit/test__types.py b/packages/sqlalchemy-bigquery/tests/unit/test__types.py index 5ece6a1db6d6c..3c702e613d6ff 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test__types.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test__types.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from google.cloud.bigquery.schema import SchemaField import pytest +from google.cloud.bigquery.schema import SchemaField from sqlalchemy_bigquery._types import _get_transitive_schema_fields diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_comments.py b/packages/sqlalchemy-bigquery/tests/unit/test_comments.py index 6feba866bde47..6e361d6cb5509 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_comments.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_comments.py @@ -79,7 +79,7 @@ def test_table_friendly_name_dialect_option(faux_conn): ) assert " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) == ( - "CREATE TABLE `some_table` ( `id` INT64 )" " OPTIONS(friendly_name='bob')" + "CREATE TABLE `some_table` ( `id` INT64 ) OPTIONS(friendly_name='bob')" ) diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_compiler.py b/packages/sqlalchemy-bigquery/tests/unit/test_compiler.py index 242fe390b8838..77f55dd7bf80d 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_compiler.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_compiler.py @@ -58,7 +58,7 @@ def test_constraints_are_ignored(faux_conn, metadata): ) metadata.create_all(faux_conn.engine) assert " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) == ( - "CREATE TABLE `some_table`" " ( `id` INT64 NOT NULL, `ref_id` INT64 )" + "CREATE TABLE `some_table` ( `id` INT64 NOT NULL, `ref_id` INT64 )" ) @@ -111,8 +111,7 @@ def test_no_alias_for_known_tables_cte(faux_conn, metadata): q = sqlalchemy.select(table.c.foo, F.unnest(table.c.bars).column_valued("bar")) expected_initial_sql = ( - "SELECT `table1`.`foo`, `bar` \n" - "FROM `table1`, unnest(`table1`.`bars`) AS `bar`" + "SELECT `table1`.`foo`, `bar` \nFROM `table1`, unnest(`table1`.`bars`) AS `bar`" ) found_initial_sql = q.compile(faux_conn).string assert found_initial_sql == expected_initial_sql @@ -338,8 +337,7 @@ def test_grouping_ops_vs_single_column(faux_conn, table, grouping_op, grouping_o found_sql = q.compile(faux_conn).string expected_sql = ( - f"SELECT `table1`.`foo` \n" - f"FROM `table1` GROUP BY {grouping_op}(`table1`.`foo`)" + f"SELECT `table1`.`foo` \nFROM `table1` GROUP BY {grouping_op}(`table1`.`foo`)" ) assert found_sql == expected_sql diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_compliance.py b/packages/sqlalchemy-bigquery/tests/unit/test_compliance.py index d3a2e717c5684..e926628390fb8 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_compliance.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_compliance.py @@ -166,12 +166,12 @@ def test_likish(faux_conn, meth, arg, expected): ], ) expr = getattr(table.c.data, meth)(arg) - rows = {value for value, in faux_conn.execute(select(table.c.id).where(expr))} + rows = {value for (value,) in faux_conn.execute(select(table.c.id).where(expr))} eq_(rows, expected) all = {i for i in range(1, 11)} expr = sqlalchemy.not_(expr) - rows = {value for value, in faux_conn.execute(select(table.c.id).where(expr))} + rows = {value for (value,) in faux_conn.execute(select(table.c.id).where(expr))} eq_(rows, all - expected) diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_helpers.py b/packages/sqlalchemy-bigquery/tests/unit/test_helpers.py index c5b7a5aa9e896..9017e0ba800d6 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_helpers.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_helpers.py @@ -10,8 +10,8 @@ import google.auth import google.auth.credentials -from google.oauth2 import service_account import pytest +from google.oauth2 import service_account from sqlalchemy_bigquery import _helpers diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_parse_url.py b/packages/sqlalchemy-bigquery/tests/unit/test_parse_url.py index fde82bf62fe6b..343346dc3e5d9 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_parse_url.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_parse_url.py @@ -17,10 +17,10 @@ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +import pytest from google.cloud.bigquery import QueryJobConfig from google.cloud.bigquery.dataset import DatasetReference from google.cloud.bigquery.table import EncryptionConfiguration, TableReference -import pytest from sqlalchemy.engine.url import make_url from sqlalchemy_bigquery.parse_url import parse_url diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_select.py b/packages/sqlalchemy-bigquery/tests/unit/test_select.py index 332f8b070c97d..5ad296076fe77 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_select.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_select.py @@ -118,7 +118,7 @@ def test_typed_parameters(faux_conn, type_, val, btype, vrep): table = setup_table(faux_conn, "t", sqlalchemy.Column(col_name, type_)) assert faux_conn.test_data["execute"].pop()[0].strip() == ( - f"CREATE TABLE `t` (\n" f"\t`{col_name}` {btype}\n" f")" + f"CREATE TABLE `t` (\n\t`{col_name}` {btype}\n)" ) faux_conn.execute(table.insert().values(**{col_name: val})) diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_sqlalchemy_bigquery.py b/packages/sqlalchemy-bigquery/tests/unit/test_sqlalchemy_bigquery.py index 85408aefd3280..c185d54e0e8b4 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_sqlalchemy_bigquery.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_sqlalchemy_bigquery.py @@ -7,11 +7,11 @@ from unittest import mock import google.api_core.exceptions +import pytest +import sqlalchemy from google.cloud import bigquery from google.cloud.bigquery.dataset import DatasetListItem from google.cloud.bigquery.table import TableListItem -import pytest -import sqlalchemy from .conftest import setup_table diff --git a/packages/sqlalchemy-bigquery/tests/unit/test_table_options.py b/packages/sqlalchemy-bigquery/tests/unit/test_table_options.py index cbdf51e347a59..507ee33b84fda 100644 --- a/packages/sqlalchemy-bigquery/tests/unit/test_table_options.py +++ b/packages/sqlalchemy-bigquery/tests/unit/test_table_options.py @@ -20,14 +20,14 @@ import datetime import sqlite3 +import pytest +import sqlalchemy from google.cloud.bigquery import ( PartitionRange, RangePartitioning, TimePartitioning, TimePartitioningType, ) -import pytest -import sqlalchemy from .conftest import setup_table