diff --git a/google/__init__.py b/google/__init__.py deleted file mode 100644 index e0bdef06..00000000 --- a/google/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2021 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -try: - import pkg_resources - - pkg_resources.declare_namespace(__name__) -except ImportError: - import pkgutil - - __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/google/cloud/__init__.py b/google/cloud/__init__.py deleted file mode 100644 index e0bdef06..00000000 --- a/google/cloud/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2021 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -try: - import pkg_resources - - pkg_resources.declare_namespace(__name__) -except ImportError: - import pkgutil - - __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/google/cloud/sqlalchemy_spanner/__init__.py b/google/cloud/sqlalchemy_spanner/__init__.py index 6cfd02bb..a5a387d1 100644 --- a/google/cloud/sqlalchemy_spanner/__init__.py +++ b/google/cloud/sqlalchemy_spanner/__init__.py @@ -14,4 +14,7 @@ from .sqlalchemy_spanner import SpannerDialect -__all__ = (SpannerDialect,) +from .version import __version__ + + +__all__ = (SpannerDialect, __version__) diff --git a/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py b/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py index 9db46b65..8e935fb2 100644 --- a/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py +++ b/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py @@ -13,7 +13,6 @@ # limitations under the License. import base64 -import pkg_resources import re from alembic.ddl.base import ( @@ -50,6 +49,7 @@ from google.cloud.spanner_v1.data_types import JsonObject from google.cloud import spanner_dbapi from google.cloud.sqlalchemy_spanner._opentelemetry_tracing import trace_call +from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version import sqlalchemy USING_SQLACLCHEMY_20 = False @@ -928,8 +928,8 @@ def create_connect_args(self, url): ), url.database, ) - dist = pkg_resources.get_distribution("sqlalchemy-spanner") - options = {"user_agent": f"gl-{dist.project_name}/{dist.version}"} + dist_version = sqlalchemy_spanner_version.__version__ + options = {"user_agent": f"gl-sqlalchemy-spanner/{dist_version}"} connect_opts = url.translate_connect_args() if ( "host" in connect_opts diff --git a/version.py b/google/cloud/sqlalchemy_spanner/version.py similarity index 100% rename from version.py rename to google/cloud/sqlalchemy_spanner/version.py diff --git a/setup.py b/setup.py index 74ad4214..2c039fe7 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,6 @@ import io import os -import warnings import setuptools @@ -37,7 +36,7 @@ } BASE_DIR = os.path.dirname(__file__) -VERSION_FILENAME = os.path.join(BASE_DIR, "version.py") +VERSION_FILENAME = os.path.join(BASE_DIR, "google/cloud/sqlalchemy_spanner/version.py") PACKAGE_INFO = {} with open(VERSION_FILENAME) as f: exec(f.read(), PACKAGE_INFO) @@ -56,31 +55,23 @@ if package.startswith("google") ] -# Determine which namespaces are needed. -namespaces = ["google"] -if "google.cloud" in packages: - namespaces.append("google.cloud") - -with warnings.catch_warnings(): - warnings.simplefilter("ignore") - setuptools.setup( - author="Google LLC", - author_email="cloud-spanner-developers@googlegroups.com", - classifiers=["Intended Audience :: Developers"], - description=description, - long_description=readme, - entry_points={ - "sqlalchemy.dialects": [ - "spanner.spanner = google.cloud.sqlalchemy_spanner:SpannerDialect" - ] - }, - install_requires=dependencies, - extras_require=extras, - name=name, - namespace_packages=namespaces, - packages=packages, - url="https://github.com/cloudspannerecosystem/python-spanner-sqlalchemy", - version=version, - include_package_data=True, - zip_safe=False, - ) +setuptools.setup( + author="Google LLC", + author_email="googleapis-packages@google.com", + classifiers=["Intended Audience :: Developers"], + description=description, + long_description=readme, + entry_points={ + "sqlalchemy.dialects": [ + "spanner.spanner = google.cloud.sqlalchemy_spanner:SpannerDialect" + ] + }, + install_requires=dependencies, + extras_require=extras, + name=name, + packages=packages, + url="https://github.com/cloudspannerecosystem/python-spanner-sqlalchemy", + version=version, + include_package_data=True, + zip_safe=False, +) diff --git a/test/test_suite_13.py b/test/test_suite_13.py index 9a0c0545..be8b1f78 100644 --- a/test/test_suite_13.py +++ b/test/test_suite_13.py @@ -18,7 +18,6 @@ import decimal import operator import os -import pkg_resources import pytest import random import time @@ -126,6 +125,8 @@ ) from test._helpers import get_db_url, get_project +from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version + config.test_schema = "" @@ -1632,12 +1633,10 @@ class UserAgentTest(SpannerSpecificTestBase): """Check that SQLAlchemy dialect uses correct user agent.""" def test_user_agent(self): - dist = pkg_resources.get_distribution("sqlalchemy-spanner") - with self._engine.connect() as connection: assert ( connection.connection.instance._client._client_info.user_agent - == f"gl-{dist.project_name}/{dist.version}" + == f"gl-sqlalchemy-spanner/{sqlalchemy_spanner_version.__version__}" ) @@ -1689,7 +1688,6 @@ def setUp(self): def test_offset_only(self): for offset in [1, 7, 10, 100, 1000, 10000]: - with self._engine.connect().execution_options(read_only=True) as connection: list(connection.execute(self._table.select().offset(offset)).fetchall()) diff --git a/test/test_suite_14.py b/test/test_suite_14.py index e12267fa..5a083ce2 100644 --- a/test/test_suite_14.py +++ b/test/test_suite_14.py @@ -18,7 +18,6 @@ import decimal import operator import os -import pkg_resources import pytest import random import time @@ -143,6 +142,9 @@ ) from test._helpers import get_db_url, get_project +from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version + + config.test_schema = "" @@ -557,7 +559,6 @@ def test_get_foreign_keys(self, connection, use_schema): def test_get_table_names( self, connection, order_by, include_plain, include_views, use_schema ): - if use_schema: schema = config.test_schema else: @@ -1905,12 +1906,10 @@ def setUp(self): self._metadata = MetaData(bind=self._engine) def test_user_agent(self): - dist = pkg_resources.get_distribution("sqlalchemy-spanner") - with self._engine.connect() as connection: assert ( connection.connection.instance._client._client_info.user_agent - == "gl-" + dist.project_name + "/" + dist.version + == f"gl-sqlalchemy-spanner/{sqlalchemy_spanner_version.__version__}" ) diff --git a/test/test_suite_20.py b/test/test_suite_20.py index 2b40d762..3cb252d2 100644 --- a/test/test_suite_20.py +++ b/test/test_suite_20.py @@ -18,7 +18,6 @@ import decimal import operator import os -import pkg_resources import pytest import random import time @@ -157,6 +156,9 @@ get_project, ) +from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version + + config.test_schema = "" @@ -884,7 +886,6 @@ def test_get_multi_foreign_keys( scope=ObjectScope.DEFAULT, kind=ObjectKind.TABLE, ): - """ SPANNER OVERRIDE: @@ -1106,7 +1107,6 @@ def test_get_foreign_keys(self, connection, use_schema): (True, testing.requires.schemas), False, argnames="use_schema" ) def test_get_table_names(self, connection, order_by, use_schema): - schema = None _ignore_tables = [ @@ -1981,7 +1981,6 @@ def _round_trip(self, datatype, data): assert isinstance(row[0], (long, int)) # noqa def _huge_ints(): - return testing.combinations( 2147483649, # 32 bits 2147483648, # 32 bits @@ -2721,12 +2720,10 @@ def setUp(self): self._metadata = MetaData() def test_user_agent(self): - dist = pkg_resources.get_distribution("sqlalchemy-spanner") - with self._engine.connect() as connection: assert ( connection.connection.instance._client._client_info.user_agent - == "gl-" + dist.project_name + "/" + dist.version + == f"gl-sqlalchemy-spanner/{sqlalchemy_spanner_version.__version__}" ) diff --git a/test/unit/test_packaging.py b/test/unit/test_packaging.py new file mode 100644 index 00000000..c05f6042 --- /dev/null +++ b/test/unit/test_packaging.py @@ -0,0 +1,37 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import subprocess +import sys + + +def test_namespace_package_compat(tmp_path): + # The ``google`` namespace package should not be masked + # by the presence of ``sqlalchemy-spanner``. + google = tmp_path / "google" + google.mkdir() + google.joinpath("othermod.py").write_text("") + env = dict(os.environ, PYTHONPATH=str(tmp_path)) + cmd = [sys.executable, "-m", "google.othermod"] + subprocess.check_call(cmd, env=env) + + # The ``google.cloud`` namespace package should not be masked + # by the presence of ``sqlalchemy-spanner``. + google_cloud = tmp_path / "google" / "cloud" + google_cloud.mkdir() + google_cloud.joinpath("othermod.py").write_text("") + env = dict(os.environ, PYTHONPATH=str(tmp_path)) + cmd = [sys.executable, "-m", "google.cloud.othermod"] + subprocess.check_call(cmd, env=env)