Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat: Introduce compatibility with native namespace packages #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions 22 google/__init__.py

This file was deleted.

22 changes: 0 additions & 22 deletions 22 google/cloud/__init__.py

This file was deleted.

5 changes: 4 additions & 1 deletion 5 google/cloud/sqlalchemy_spanner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@

from .sqlalchemy_spanner import SpannerDialect

__all__ = (SpannerDialect,)
from .version import __version__


__all__ = (SpannerDialect, __version__)
6 changes: 3 additions & 3 deletions 6 google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
import base64

import pkg_resources
import re

from alembic.ddl.base import (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
51 changes: 21 additions & 30 deletions 51 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import io
import os
import warnings

import setuptools

Expand All @@ -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)
Expand All @@ -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,
)
8 changes: 3 additions & 5 deletions 8 test/test_suite_13.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import decimal
import operator
import os
import pkg_resources
import pytest
import random
import time
Expand Down Expand Up @@ -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 = ""


Expand Down Expand Up @@ -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__}"
)


Expand Down Expand Up @@ -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())

Expand Down
9 changes: 4 additions & 5 deletions 9 test/test_suite_14.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import decimal
import operator
import os
import pkg_resources
import pytest
import random
import time
Expand Down Expand Up @@ -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 = ""


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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__}"
)


Expand Down
11 changes: 4 additions & 7 deletions 11 test/test_suite_20.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import decimal
import operator
import os
import pkg_resources
import pytest
import random
import time
Expand Down Expand Up @@ -157,6 +156,9 @@
get_project,
)

from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version


config.test_schema = ""


Expand Down Expand Up @@ -884,7 +886,6 @@ def test_get_multi_foreign_keys(
scope=ObjectScope.DEFAULT,
kind=ObjectKind.TABLE,
):

"""
SPANNER OVERRIDE:

Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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__}"
)


Expand Down
37 changes: 37 additions & 0 deletions 37 test/unit/test_packaging.py
Original file line number Diff line number Diff line change
@@ -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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.