Skip to content

Commit 6e19a4f

Browse files
committed
feat: Introduce compatibility with native namespace packages
1 parent 5077ddb commit 6e19a4f

File tree

10 files changed

+76
-95
lines changed

10 files changed

+76
-95
lines changed

google/__init__.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

google/cloud/__init__.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

google/cloud/sqlalchemy_spanner/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414

1515
from .sqlalchemy_spanner import SpannerDialect
1616

17-
__all__ = (SpannerDialect,)
17+
from .version import __version__
18+
19+
20+
__all__ = (SpannerDialect, __version__)

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414
import base64
1515

16-
import pkg_resources
1716
import re
1817

1918
from alembic.ddl.base import (
@@ -50,6 +49,7 @@
5049
from google.cloud.spanner_v1.data_types import JsonObject
5150
from google.cloud import spanner_dbapi
5251
from google.cloud.sqlalchemy_spanner._opentelemetry_tracing import trace_call
52+
from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version
5353
import sqlalchemy
5454

5555
USING_SQLACLCHEMY_20 = False
@@ -928,8 +928,8 @@ def create_connect_args(self, url):
928928
),
929929
url.database,
930930
)
931-
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
932-
options = {"user_agent": f"gl-{dist.project_name}/{dist.version}"}
931+
932+
options = {"user_agent": f"gl-sqlalchemy-spanner/{sqlalchemy_spanner_version.__version__}"}
933933
connect_opts = url.translate_connect_args()
934934
if (
935935
"host" in connect_opts
File renamed without changes.

setup.py

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import io
1616
import os
17-
import warnings
1817

1918
import setuptools
2019

@@ -37,7 +36,7 @@
3736
}
3837

3938
BASE_DIR = os.path.dirname(__file__)
40-
VERSION_FILENAME = os.path.join(BASE_DIR, "version.py")
39+
VERSION_FILENAME = os.path.join(BASE_DIR, "google/cloud/sqlalchemy_spanner/version.py")
4140
PACKAGE_INFO = {}
4241
with open(VERSION_FILENAME) as f:
4342
exec(f.read(), PACKAGE_INFO)
@@ -56,31 +55,23 @@
5655
if package.startswith("google")
5756
]
5857

59-
# Determine which namespaces are needed.
60-
namespaces = ["google"]
61-
if "google.cloud" in packages:
62-
namespaces.append("google.cloud")
63-
64-
with warnings.catch_warnings():
65-
warnings.simplefilter("ignore")
66-
setuptools.setup(
67-
author="Google LLC",
68-
author_email="cloud-spanner-developers@googlegroups.com",
69-
classifiers=["Intended Audience :: Developers"],
70-
description=description,
71-
long_description=readme,
72-
entry_points={
73-
"sqlalchemy.dialects": [
74-
"spanner.spanner = google.cloud.sqlalchemy_spanner:SpannerDialect"
75-
]
76-
},
77-
install_requires=dependencies,
78-
extras_require=extras,
79-
name=name,
80-
namespace_packages=namespaces,
81-
packages=packages,
82-
url="https://github.com/cloudspannerecosystem/python-spanner-sqlalchemy",
83-
version=version,
84-
include_package_data=True,
85-
zip_safe=False,
86-
)
58+
setuptools.setup(
59+
author="Google LLC",
60+
author_email="googleapis-packages@google.com",
61+
classifiers=["Intended Audience :: Developers"],
62+
description=description,
63+
long_description=readme,
64+
entry_points={
65+
"sqlalchemy.dialects": [
66+
"spanner.spanner = google.cloud.sqlalchemy_spanner:SpannerDialect"
67+
]
68+
},
69+
install_requires=dependencies,
70+
extras_require=extras,
71+
name=name,
72+
packages=packages,
73+
url="https://github.com/cloudspannerecosystem/python-spanner-sqlalchemy",
74+
version=version,
75+
include_package_data=True,
76+
zip_safe=False,
77+
)

test/test_suite_13.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import decimal
1919
import operator
2020
import os
21-
import pkg_resources
2221
import pytest
2322
import random
2423
import time
@@ -126,6 +125,8 @@
126125
)
127126
from test._helpers import get_db_url, get_project
128127

128+
from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version
129+
129130
config.test_schema = ""
130131

131132

@@ -1632,12 +1633,10 @@ class UserAgentTest(SpannerSpecificTestBase):
16321633
"""Check that SQLAlchemy dialect uses correct user agent."""
16331634

16341635
def test_user_agent(self):
1635-
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
1636-
16371636
with self._engine.connect() as connection:
16381637
assert (
16391638
connection.connection.instance._client._client_info.user_agent
1640-
== f"gl-{dist.project_name}/{dist.version}"
1639+
== f"gl-sqlalchemy-spanner/{sqlalchemy_spanner_version.__version__}"
16411640
)
16421641

16431642

@@ -1689,7 +1688,6 @@ def setUp(self):
16891688

16901689
def test_offset_only(self):
16911690
for offset in [1, 7, 10, 100, 1000, 10000]:
1692-
16931691
with self._engine.connect().execution_options(read_only=True) as connection:
16941692
list(connection.execute(self._table.select().offset(offset)).fetchall())
16951693

test/test_suite_14.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import decimal
1919
import operator
2020
import os
21-
import pkg_resources
2221
import pytest
2322
import random
2423
import time
@@ -143,6 +142,9 @@
143142
)
144143
from test._helpers import get_db_url, get_project
145144

145+
from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version
146+
147+
146148
config.test_schema = ""
147149

148150

@@ -557,7 +559,6 @@ def test_get_foreign_keys(self, connection, use_schema):
557559
def test_get_table_names(
558560
self, connection, order_by, include_plain, include_views, use_schema
559561
):
560-
561562
if use_schema:
562563
schema = config.test_schema
563564
else:
@@ -1905,12 +1906,10 @@ def setUp(self):
19051906
self._metadata = MetaData(bind=self._engine)
19061907

19071908
def test_user_agent(self):
1908-
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
1909-
19101909
with self._engine.connect() as connection:
19111910
assert (
19121911
connection.connection.instance._client._client_info.user_agent
1913-
== "gl-" + dist.project_name + "/" + dist.version
1912+
== f"gl-sqlalchemy-spanner/{sqlalchemy_spanner_version.__version__}"
19141913
)
19151914

19161915

test/test_suite_20.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import decimal
1919
import operator
2020
import os
21-
import pkg_resources
2221
import pytest
2322
import random
2423
import time
@@ -157,6 +156,9 @@
157156
get_project,
158157
)
159158

159+
from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version
160+
161+
160162
config.test_schema = ""
161163

162164

@@ -884,7 +886,6 @@ def test_get_multi_foreign_keys(
884886
scope=ObjectScope.DEFAULT,
885887
kind=ObjectKind.TABLE,
886888
):
887-
888889
"""
889890
SPANNER OVERRIDE:
890891
@@ -1106,7 +1107,6 @@ def test_get_foreign_keys(self, connection, use_schema):
11061107
(True, testing.requires.schemas), False, argnames="use_schema"
11071108
)
11081109
def test_get_table_names(self, connection, order_by, use_schema):
1109-
11101110
schema = None
11111111

11121112
_ignore_tables = [
@@ -1981,7 +1981,6 @@ def _round_trip(self, datatype, data):
19811981
assert isinstance(row[0], (long, int)) # noqa
19821982

19831983
def _huge_ints():
1984-
19851984
return testing.combinations(
19861985
2147483649, # 32 bits
19871986
2147483648, # 32 bits
@@ -2721,12 +2720,10 @@ def setUp(self):
27212720
self._metadata = MetaData()
27222721

27232722
def test_user_agent(self):
2724-
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
2725-
27262723
with self._engine.connect() as connection:
27272724
assert (
27282725
connection.connection.instance._client._client_info.user_agent
2729-
== "gl-" + dist.project_name + "/" + dist.version
2726+
== f"gl-sqlalchemy-spanner/{sqlalchemy_spanner_version.__version__}"
27302727
)
27312728

27322729

test/unit/test_packaging.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import subprocess
17+
import sys
18+
19+
20+
def test_namespace_package_compat(tmp_path):
21+
# The ``google`` namespace package should not be masked
22+
# by the presence of ``sqlalchemy-spanner``.
23+
google = tmp_path / "google"
24+
google.mkdir()
25+
google.joinpath("othermod.py").write_text("")
26+
env = dict(os.environ, PYTHONPATH=str(tmp_path))
27+
cmd = [sys.executable, "-m", "google.othermod"]
28+
subprocess.check_call(cmd, env=env)
29+
30+
# The ``google.cloud`` namespace package should not be masked
31+
# by the presence of ``sqlalchemy-spanner``.
32+
google_cloud = tmp_path / "google" / "cloud"
33+
google_cloud.mkdir()
34+
google_cloud.joinpath("othermod.py").write_text("")
35+
env = dict(os.environ, PYTHONPATH=str(tmp_path))
36+
cmd = [sys.executable, "-m", "google.cloud.othermod"]
37+
subprocess.check_call(cmd, env=env)

0 commit comments

Comments
 (0)