Skip to content

Commit

Permalink
feat: introduce compatibility with native namespace packages (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Nov 20, 2023
1 parent 9ecd247 commit 8b0e2ae
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 94 deletions.
21 changes: 0 additions & 21 deletions google/__init__.py

This file was deleted.

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

This file was deleted.

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

This file was deleted.

13 changes: 3 additions & 10 deletions google/cloud/alloydb/connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@
# 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.
from .connector import Connector
from google.cloud.alloydb.connector.connector import Connector
from google.cloud.alloydb.connector.version import __version__

__ALL__ = [Connector]

try:
import pkg_resources

pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil

__path__ = pkgutil.extend_path(__path__, __name__)
__all__ = ["__version__", "Connector"]
1 change: 1 addition & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[mypy]
python_version = 3.8
warn_unused_configs = True
namespace_packages = True

[mypy-google.auth.*]
ignore_missing_imports = True
Expand Down
9 changes: 8 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ def lint(session):
"google",
"tests",
)
session.run("mypy", "google", "--install-types", "--non-interactive")
session.run(
"mypy",
"-p",
"google",
"--install-types",
"--non-interactive",
"--show-traceback",
)
session.run("python", "setup.py", "sdist")
session.run("twine", "check", "dist/*")

Expand Down
42 changes: 22 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,40 @@
# limitations under the License.
import io
import os
from setuptools import setup, find_packages

from setuptools import find_namespace_packages, setup

name = "google-cloud-alloydb-connector"
description = "A Python client library for connecting securely to your Google Cloud AlloyDB instances."

release_status = "Development Status :: 4 - Beta"
dependencies = [
"aiohttp",
"cryptography>=38.0.3",
"requests",
"google-auth",
]

package_root = os.path.abspath(os.path.dirname(__file__))

readme_filename = os.path.join(package_root, "README.md")
with io.open(readme_filename, encoding="utf-8") as readme_file:
readme = readme_file.read()

packages = [package for package in find_packages() if package.startswith("google")]

# Determine which namespaces are needed.
namespaces = ["google"]
if "google.cloud" in packages:
namespaces.append("google.cloud")

name = "google-cloud-alloydb-connector"
description = "A Python client library for connecting securely to your Google Cloud AlloyDB instances."

version = {}
with open("google/cloud/alloydb/connector/version.py") as fp:
with open(
os.path.join(package_root, "google/cloud/alloydb/connector/version.py")
) as fp:
exec(fp.read(), version)
version = version["__version__"]

release_status = "Development Status :: 4 - Beta"
core_dependencies = [
"aiohttp",
"cryptography>=38.0.3",
"requests",
"google-auth",
# Only include packages under the 'google' namespace. Do not include tests,
# samples, etc.
packages = [
package for package in find_namespace_packages() if package.startswith("google")
]


setup(
name=name,
version=version,
Expand All @@ -66,8 +69,7 @@
],
platforms="Posix; MacOS X; Windows",
packages=packages,
namespace_packages=namespaces,
install_requires=core_dependencies,
install_requires=dependencies,
extras_require={
"pg8000": ["pg8000>=1.30.3"],
},
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/test_packaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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 pathlib
import subprocess
import sys


def test_namespace_package_compat(tmp_path: pathlib.PosixPath) -> None:
# The ``google`` namespace package should not be masked
# by the presence of ``google-cloud-alloydb-connector``.
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 ``google-cloud-alloydb-connector``.
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)

# The ``google.cloud.sql`` namespace package should not be masked
# by the presence of ``google-cloud-alloydb-connector``.
google_cloud_alloydb = tmp_path / "google" / "cloud" / "alloydb"
google_cloud_alloydb.mkdir()
google_cloud_alloydb.joinpath("othermod.py").write_text("")
env = dict(os.environ, PYTHONPATH=str(tmp_path))
cmd = [sys.executable, "-m", "google.cloud.alloydb.othermod"]
subprocess.check_call(cmd, env=env)

0 comments on commit 8b0e2ae

Please sign in to comment.