From 083da1193a838be9f16653f2d4efb4ad8bcfa73a Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Mon, 20 Nov 2023 14:35:01 -0500 Subject: [PATCH] feat: introduce compatibility with native namespace packages (#906) --- .mypy.ini | 1 + google/__init__.py | 21 ------------ google/cloud/__init__.py | 21 ------------ google/cloud/sql/__init__.py | 0 google/cloud/sql/connector/__init__.py | 15 +++----- noxfile.py | 2 +- requirements-test.txt | 1 - setup.py | 45 ++++++++++++------------ tests/unit/test_packaging.py | 47 ++++++++++++++++++++++++++ 9 files changed, 75 insertions(+), 78 deletions(-) delete mode 100644 google/__init__.py delete mode 100644 google/cloud/__init__.py delete mode 100644 google/cloud/sql/__init__.py create mode 100644 tests/unit/test_packaging.py diff --git a/.mypy.ini b/.mypy.ini index ae553005..9556bcdb 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -2,6 +2,7 @@ python_version = 3.8 warn_unused_configs = True plugins = sqlmypy +namespace_packages = True [mypy-google.auth.*] ignore_missing_imports = True diff --git a/google/__init__.py b/google/__init__.py deleted file mode 100644 index 868db78d..00000000 --- a/google/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2019 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 868db78d..00000000 --- a/google/cloud/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2019 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/sql/__init__.py b/google/cloud/sql/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/google/cloud/sql/connector/__init__.py b/google/cloud/sql/connector/__init__.py index 527e177c..fe2e40d7 100644 --- a/google/cloud/sql/connector/__init__.py +++ b/google/cloud/sql/connector/__init__.py @@ -13,17 +13,10 @@ See the License for the specific language governing permissions and limitations under the License. """ -from .connector import Connector, create_async_connector -from .instance import IPTypes +from google.cloud.sql.connector.connector import Connector, create_async_connector +from google.cloud.sql.connector.instance import IPTypes +from google.cloud.sql.connector.version import __version__ -__ALL__ = [create_async_connector, Connector, IPTypes] -try: - import pkg_resources - - pkg_resources.declare_namespace(__name__) -except ImportError: - import pkgutil - - __path__ = pkgutil.extend_path(__path__, __name__) +__all__ = ["__version__", "create_async_connector", "Connector", "IPTypes"] diff --git a/noxfile.py b/noxfile.py index dcffa679..05c25c51 100644 --- a/noxfile.py +++ b/noxfile.py @@ -42,7 +42,7 @@ def lint(session): "google", "tests", ) - session.run("mypy", "google", "tests") + session.run("mypy", "-p", "google", "--show-traceback") session.run("python", "setup.py", "sdist") session.run("twine", "check", "dist/*") diff --git a/requirements-test.txt b/requirements-test.txt index 1029c002..18354637 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -9,7 +9,6 @@ flake8-annotations==2.9.1 black==23.11.0 mypy==0.982 sqlalchemy-stubs==0.4 -types-pkg-resources==0.1.3 types-PyMySQL==1.1.0.1 types-mock==5.1.0.2 twine==4.0.2 diff --git a/setup.py b/setup.py index 7154901a..8a700256 100644 --- a/setup.py +++ b/setup.py @@ -13,20 +13,8 @@ # limitations under the License. import io import os -from setuptools import setup, find_packages -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") +from setuptools import find_namespace_packages, setup name = "cloud-sql-python-connector" description = ( @@ -35,20 +23,32 @@ " permissions to connect to a Cloud SQL database without having" " to manually allowlist IPs or manage SSL certificates." ) - -version = {} -with open("google/cloud/sql/connector/version.py") as fp: - exec(fp.read(), version) -version = version["__version__"] - release_status = "Development Status :: 5 - Production/Stable" -core_dependencies = [ +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() + +version = {} +with open(os.path.join(package_root, "google/cloud/sql/connector/version.py")) as fp: + exec(fp.read(), version) +version = version["__version__"] + +# 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, @@ -71,13 +71,12 @@ ], platforms="Posix; MacOS X; Windows", packages=packages, - namespace_packages=namespaces, - install_requires=core_dependencies, + install_requires=dependencies, extras_require={ "pymysql": ["PyMySQL>=1.1.0"], "pg8000": ["pg8000>=1.30.3"], "pytds": ["python-tds>=1.13.0"], - "asyncpg": ["asyncpg>=0.29.0"] + "asyncpg": ["asyncpg>=0.29.0"], }, python_requires=">=3.8", include_package_data=True, diff --git a/tests/unit/test_packaging.py b/tests/unit/test_packaging.py new file mode 100644 index 00000000..d0611f16 --- /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 ``cloud-sql-python-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 ``cloud-sql-python-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 ``cloud-sql-python-connector``. + google_cloud_sql = tmp_path / "google" / "cloud" / "sql" + google_cloud_sql.mkdir() + google_cloud_sql.joinpath("othermod.py").write_text("") + env = dict(os.environ, PYTHONPATH=str(tmp_path)) + cmd = [sys.executable, "-m", "google.cloud.sql.othermod"] + subprocess.check_call(cmd, env=env)