diff --git a/.flake8 b/.flake8 index 3da787c..02330e9 100644 --- a/.flake8 +++ b/.flake8 @@ -3,7 +3,7 @@ import-order-style=google # Note: this forces all google imports to be in the third group. See # https://github.com/PyCQA/flake8-import-order/issues/111 application-import-names=google -ignore = E203, E266, E501, W503 +ignore = E203, E266, E501, W503, I202 exclude = __pycache__, .git, diff --git a/docs/conf.py b/docs/conf.py index b9e8d03..744193a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -313,13 +313,7 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ( - master_doc, - "google-cloud-core", - u"google-cloud-core Documentation", - [author], - 1, - ) + (master_doc, "google-cloud-core", u"google-cloud-core Documentation", [author], 1,) ] # If true, show URL addresses after external links. @@ -360,14 +354,10 @@ intersphinx_mapping = { "python": ("https://python.readthedocs.org/en/latest/", None), "google-auth": ("https://googleapis.dev/python/google-auth/latest/", None), - "google.api_core": ( - "https://googleapis.dev/python/google-api-core/latest/", - None, - ), + "google.api_core": ("https://googleapis.dev/python/google-api-core/latest/", None,), "grpc": ("https://grpc.github.io/grpc/python/", None), "proto-plus": ("https://proto-plus-python.readthedocs.io/en/latest/", None), "protobuf": ("https://googleapis.dev/python/protobuf/latest/", None), - } diff --git a/google/cloud/_http.py b/google/cloud/_http.py index 719e986..6627a9b 100644 --- a/google/cloud/_http.py +++ b/google/cloud/_http.py @@ -15,9 +15,10 @@ """Shared implementation of connections to API servers.""" import collections + try: import collections.abc as collections_abc -except ImportError: +except ImportError: # Python2 import collections as collections_abc import json import os @@ -34,9 +35,7 @@ API_BASE_URL = "https://www.googleapis.com" """The base of the API call URL.""" -DEFAULT_USER_AGENT = "gcloud-python/{0}".format( - version.__version__ -) +DEFAULT_USER_AGENT = "gcloud-python/{0}".format(version.__version__) """The user agent for google-cloud-python requests.""" CLIENT_INFO_HEADER = "X-Goog-API-Client" @@ -83,14 +82,12 @@ def USER_AGENT(self): :rtype: str :returns: user agent """ - warnings.warn( - _USER_AGENT_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2) + warnings.warn(_USER_AGENT_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2) return self.user_agent @USER_AGENT.setter def USER_AGENT(self, value): - warnings.warn( - _USER_AGENT_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2) + warnings.warn(_USER_AGENT_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2) self.user_agent = value @property @@ -114,13 +111,15 @@ def _EXTRA_HEADERS(self): :returns: header keys / values """ warnings.warn( - _EXTRA_HEADERS_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2) + _EXTRA_HEADERS_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2 + ) return self.extra_headers @_EXTRA_HEADERS.setter def _EXTRA_HEADERS(self, value): warnings.warn( - _EXTRA_HEADERS_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2) + _EXTRA_HEADERS_ALL_CAPS_DEPRECATED, DeprecationWarning, stacklevel=2 + ) self.extra_headers = value @property @@ -222,7 +221,9 @@ def get_api_base_url_for_mtls(self, api_base_url=None): url_to_use = self.API_BASE_URL else: if self.ALLOW_AUTO_SWITCH_TO_MTLS_URL: - url_to_use = self.API_BASE_MTLS_URL if self.http.is_mtls else self.API_BASE_URL + url_to_use = ( + self.API_BASE_MTLS_URL if self.http.is_mtls else self.API_BASE_URL + ) else: url_to_use = self.API_BASE_URL return url_to_use diff --git a/google/cloud/client.py b/google/cloud/client.py index 0cfffdb..0697741 100644 --- a/google/cloud/client.py +++ b/google/cloud/client.py @@ -158,9 +158,12 @@ def __init__(self, credentials=None, _http=None, client_options=None): if credentials and client_options.credentials_file: raise google.api_core.exceptions.DuplicateCredentialArgs( - "'credentials' and 'client_options.credentials_file' are mutually exclusive.") + "'credentials' and 'client_options.credentials_file' are mutually exclusive." + ) - if credentials and not isinstance(credentials, google.auth.credentials.Credentials): + if credentials and not isinstance( + credentials, google.auth.credentials.Credentials + ): raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP) scopes = client_options.scopes or self.SCOPE @@ -169,15 +172,19 @@ def __init__(self, credentials=None, _http=None, client_options=None): if not _http and credentials is None: if client_options.credentials_file: credentials, _ = google.auth.load_credentials_from_file( - client_options.credentials_file, scopes=scopes) + client_options.credentials_file, scopes=scopes + ) else: credentials, _ = google.auth.default(scopes=scopes) self._credentials = google.auth.credentials.with_scopes_if_required( - credentials, scopes=scopes) + credentials, scopes=scopes + ) if client_options.quota_project_id: - self._credentials = self._credentials.with_quota_project(client_options.quota_project_id) + self._credentials = self._credentials.with_quota_project( + client_options.quota_project_id + ) self._http_internal = _http self._client_cert_source = client_options.client_cert_source @@ -202,8 +209,7 @@ def _http(self): """ if self._http_internal is None: self._http_internal = google.auth.transport.requests.AuthorizedSession( - self._credentials, - refresh_timeout=_CREDENTIALS_REFRESH_TIMEOUT, + self._credentials, refresh_timeout=_CREDENTIALS_REFRESH_TIMEOUT, ) self._http_internal.configure_mtls_channel(self._client_cert_source) return self._http_internal @@ -233,8 +239,7 @@ def __init__(self, project=None, credentials=None): # https://github.com/googleapis/python-cloud-core/issues/27 if project is None: project = os.getenv( - environment_vars.PROJECT, - os.getenv(environment_vars.LEGACY_PROJECT), + environment_vars.PROJECT, os.getenv(environment_vars.LEGACY_PROJECT), ) # Project set on explicit credentials overrides discovery from @@ -296,4 +301,6 @@ class ClientWithProject(Client, _ClientProjectMixin): def __init__(self, project=None, credentials=None, client_options=None, _http=None): _ClientProjectMixin.__init__(self, project=project, credentials=credentials) - Client.__init__(self, credentials=credentials, client_options=client_options, _http=_http) + Client.__init__( + self, credentials=credentials, client_options=client_options, _http=_http + ) diff --git a/noxfile.py b/noxfile.py index 5c16a25..5eb9229 100644 --- a/noxfile.py +++ b/noxfile.py @@ -19,10 +19,35 @@ import nox +BLACK_VERSION = "black==19.10b0" +BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"] + DEFAULT_PYTHON_VERSION = "3.7" CURRENT_DIRECTORY = os.path.abspath(os.path.dirname(__file__)) +@nox.session(python=DEFAULT_PYTHON_VERSION) +def lint(session): + """Run linters. + + Returns a failure if the linters find linting errors or sufficiently + serious code quality issues. + """ + session.install("flake8", "flake8-import-order", BLACK_VERSION) + session.install(".") + session.run("flake8", "google", "tests") + + +@nox.session(python=DEFAULT_PYTHON_VERSION) +def blacken(session): + """Run black. + + Format code to uniform standard. + """ + session.install(BLACK_VERSION) + session.run("black", *BLACK_PATHS) + + def default(session): """Default unit test session. This is intended to be run **without** an interpreter set, so @@ -51,7 +76,7 @@ def default(session): "--cov-report=", "--cov-fail-under=0", os.path.join("tests", "unit"), - *session.posargs + *session.posargs, ) @@ -61,18 +86,6 @@ def unit(session): default(session) -@nox.session(python=DEFAULT_PYTHON_VERSION) -def lint(session): - """Run linters. - - Returns a failure if the linters find linting errors or sufficiently - serious code quality issues. - """ - session.install("flake8", "flake8-import-order") - session.install(".") - session.run("flake8", "google", "tests") - - @nox.session(python=DEFAULT_PYTHON_VERSION) def lint_setup_py(session): """Verify that setup.py is valid (including RST check).""" diff --git a/owlbot.py b/owlbot.py index 29a5f77..80f0c15 100644 --- a/owlbot.py +++ b/owlbot.py @@ -35,3 +35,5 @@ "setup.cfg", ], ) + +s.shell.run(["nox", "-s", "blacken"], hide_output=False) diff --git a/tests/unit/test__http.py b/tests/unit/test__http.py index 32a4965..fff2b6b 100644 --- a/tests/unit/test__http.py +++ b/tests/unit/test__http.py @@ -186,14 +186,18 @@ def test_build_api_url_no_extra_query_params(self): client = object() conn = self._make_mock_one(client) # Intended to emulate self.mock_template - URI = "/".join([conn.API_BASE_URL, "mock", conn.API_VERSION, "foo?prettyPrint=false"]) + URI = "/".join( + [conn.API_BASE_URL, "mock", conn.API_VERSION, "foo?prettyPrint=false"] + ) self.assertEqual(conn.build_api_url("/foo"), URI) def test_build_api_url_w_pretty_print_query_params(self): client = object() conn = self._make_mock_one(client) uri = conn.build_api_url("/foo", {"prettyPrint": "true"}) - URI = "/".join([conn.API_BASE_URL, "mock", conn.API_VERSION, "foo?prettyPrint=true"]) + URI = "/".join( + [conn.API_BASE_URL, "mock", conn.API_VERSION, "foo?prettyPrint=true"] + ) self.assertEqual(uri, URI) def test_build_api_url_w_extra_query_params(self): @@ -220,7 +224,9 @@ def test_build_api_url_w_extra_query_params_tuples(self): client = object() conn = self._make_mock_one(client) - uri = conn.build_api_url("/foo", [("bar", "baz"), ("qux", "quux"), ("qux", "corge")]) + uri = conn.build_api_url( + "/foo", [("bar", "baz"), ("qux", "quux"), ("qux", "corge")] + ) scheme, netloc, path, qs, _ = urlsplit(uri) self.assertEqual("%s://%s" % (scheme, netloc), conn.API_BASE_URL) diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 0e31348..9eeb757 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -80,7 +80,7 @@ def test_ctor_client_options_w_conflicting_creds(self): from google.api_core.exceptions import DuplicateCredentialArgs credentials = _make_credentials() - client_options = {'credentials_file': '/path/to/creds.json'} + client_options = {"credentials_file": "/path/to/creds.json"} with self.assertRaises(DuplicateCredentialArgs): self._make_one(credentials=credentials, client_options=client_options) @@ -92,26 +92,34 @@ def test_ctor_bad_credentials(self): def test_ctor_client_options_w_creds_file_scopes(self): credentials = _make_credentials() - credentials_file = '/path/to/creds.json' - scopes = ['SCOPE1', 'SCOPE2'] - client_options = {'credentials_file': credentials_file, 'scopes': scopes} + credentials_file = "/path/to/creds.json" + scopes = ["SCOPE1", "SCOPE2"] + client_options = {"credentials_file": credentials_file, "scopes": scopes} - patch = mock.patch("google.auth.load_credentials_from_file", return_value=(credentials, None)) + patch = mock.patch( + "google.auth.load_credentials_from_file", return_value=(credentials, None) + ) with patch as load_credentials_from_file: client_obj = self._make_one(client_options=client_options) self.assertIs(client_obj._credentials, credentials) self.assertIsNone(client_obj._http_internal) - load_credentials_from_file.assert_called_once_with(credentials_file, scopes=scopes) + load_credentials_from_file.assert_called_once_with( + credentials_file, scopes=scopes + ) def test_ctor_client_options_w_quota_project(self): credentials = _make_credentials() - quota_project_id = 'quota-project-123' - client_options = {'quota_project_id': quota_project_id} + quota_project_id = "quota-project-123" + client_options = {"quota_project_id": quota_project_id} - client_obj = self._make_one(credentials=credentials, client_options=client_options) + client_obj = self._make_one( + credentials=credentials, client_options=client_options + ) - self.assertIs(client_obj._credentials, credentials.with_quota_project.return_value) + self.assertIs( + client_obj._credentials, credentials.with_quota_project.return_value + ) credentials.with_quota_project.assert_called_once_with(quota_project_id) def test_ctor__http_property_existing(self): @@ -126,18 +134,24 @@ def test_ctor__http_property_new(self): credentials = _make_credentials() mock_client_cert_source = mock.Mock() - client_options = {'client_cert_source': mock_client_cert_source} + client_options = {"client_cert_source": mock_client_cert_source} client = self._make_one(credentials=credentials, client_options=client_options) self.assertIsNone(client._http_internal) - with mock.patch('google.auth.transport.requests.AuthorizedSession') as AuthorizedSession: + with mock.patch( + "google.auth.transport.requests.AuthorizedSession" + ) as AuthorizedSession: session = mock.Mock() session.configure_mtls_channel = mock.Mock() AuthorizedSession.return_value = session self.assertIs(client._http, session) # Check the mock. - AuthorizedSession.assert_called_once_with(credentials, refresh_timeout=_CREDENTIALS_REFRESH_TIMEOUT) - session.configure_mtls_channel.assert_called_once_with(mock_client_cert_source) + AuthorizedSession.assert_called_once_with( + credentials, refresh_timeout=_CREDENTIALS_REFRESH_TIMEOUT + ) + session.configure_mtls_channel.assert_called_once_with( + mock_client_cert_source + ) # Make sure the cached value is used on subsequent access. self.assertIs(client._http_internal, session) self.assertIs(client._http, session) @@ -206,8 +220,7 @@ def test_ctor_defaults_wo_envvar(self): environ = {} patch_env = mock.patch("os.environ", new=environ) patch_default = mock.patch( - "google.cloud.client._determine_default_project", - return_value=None, + "google.cloud.client._determine_default_project", return_value=None, ) with patch_env: with patch_default as patched: @@ -241,8 +254,7 @@ def test_ctor_defaults_w_legacy_envvar(self): def test_ctor_w_explicit_project(self): explicit_project = "explicit-project-456" patch_default = mock.patch( - "google.cloud.client._determine_default_project", - return_value=None, + "google.cloud.client._determine_default_project", return_value=None, ) with patch_default as patched: client = self._make_one(project=explicit_project) @@ -254,8 +266,7 @@ def test_ctor_w_explicit_project(self): def test_ctor_w_explicit_project_bytes(self): explicit_project = b"explicit-project-456" patch_default = mock.patch( - "google.cloud.client._determine_default_project", - return_value=None, + "google.cloud.client._determine_default_project", return_value=None, ) with patch_default as patched: client = self._make_one(project=explicit_project) @@ -267,8 +278,7 @@ def test_ctor_w_explicit_project_bytes(self): def test_ctor_w_explicit_project_invalid(self): explicit_project = object() patch_default = mock.patch( - "google.cloud.client._determine_default_project", - return_value=None, + "google.cloud.client._determine_default_project", return_value=None, ) with patch_default as patched: with self.assertRaises(ValueError): @@ -307,8 +317,7 @@ def test_ctor_w_explicit_credentials_w_project(self): project = "credentials-project-456" credentials = self._make_credentials(project_id=project) patch_default = mock.patch( - "google.cloud.client._determine_default_project", - return_value=None, + "google.cloud.client._determine_default_project", return_value=None, ) with patch_default as patched: client = self._make_one(credentials=credentials)