From ea6752936c304929d8424c5e86a4107b8283ef68 Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Mon, 20 Nov 2023 20:00:09 +0000 Subject: [PATCH] feat: introduce compatibility with native namespace packages --- google/__init__.py | 21 ---------- google/cloud/__init__.py | 21 ---------- google/cloud/alloydb/__init__.py | 21 ---------- google/cloud/alloydb/connector/__init__.py | 13 ++---- mypy.ini | 1 + noxfile.py | 9 ++++- setup.py | 42 ++++++++++--------- tests/unit/test_packaging.py | 47 ++++++++++++++++++++++ 8 files changed, 81 insertions(+), 94 deletions(-) delete mode 100644 google/__init__.py delete mode 100644 google/cloud/__init__.py delete mode 100644 google/cloud/alloydb/__init__.py create mode 100644 tests/unit/test_packaging.py diff --git a/google/__init__.py b/google/__init__.py deleted file mode 100644 index 14417c0..0000000 --- a/google/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# 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. -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 14417c0..0000000 --- a/google/cloud/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# 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. -try: - import pkg_resources - - pkg_resources.declare_namespace(__name__) -except ImportError: - import pkgutil - - __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/google/cloud/alloydb/__init__.py b/google/cloud/alloydb/__init__.py deleted file mode 100644 index 14417c0..0000000 --- a/google/cloud/alloydb/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# 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. -try: - import pkg_resources - - pkg_resources.declare_namespace(__name__) -except ImportError: - import pkgutil - - __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/google/cloud/alloydb/connector/__init__.py b/google/cloud/alloydb/connector/__init__.py index 0667b56..9647ace 100644 --- a/google/cloud/alloydb/connector/__init__.py +++ b/google/cloud/alloydb/connector/__init__.py @@ -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"] diff --git a/mypy.ini b/mypy.ini index a3964a5..c37fb80 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,6 +1,7 @@ [mypy] python_version = 3.8 warn_unused_configs = True +namespace_packages = True [mypy-google.auth.*] ignore_missing_imports = True diff --git a/noxfile.py b/noxfile.py index 19bfbfe..ad3b6c2 100644 --- a/noxfile.py +++ b/noxfile.py @@ -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/*") diff --git a/setup.py b/setup.py index 3fa52d3..84fff6d 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,19 @@ # 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__)) @@ -21,29 +33,20 @@ 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, @@ -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"], }, diff --git a/tests/unit/test_packaging.py b/tests/unit/test_packaging.py new file mode 100644 index 0000000..c82118b --- /dev/null +++ b/tests/unit/test_packaging.py @@ -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)