From 2dd2fe780f684baadf63be03b71510a7009ca05d Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Fri, 17 Nov 2023 21:33:07 +0000 Subject: [PATCH 1/4] fix: use native namespace to avoid pkg_resources warnings --- google/__init__.py | 21 ------------ google/cloud/__init__.py | 21 ------------ google/cloud/sql/__init__.py | 0 google/cloud/sql/connector/__init__.py | 15 +++------ requirements-test.txt | 1 - setup.py | 45 +++++++++++++------------- tests/unit/test_packaging.py | 30 +++++++++++++++++ 7 files changed, 56 insertions(+), 77 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/google/__init__.py b/google/__init__.py deleted file mode 100644 index 868db78d7..000000000 --- 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 868db78d7..000000000 --- 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 e69de29bb..000000000 diff --git a/google/cloud/sql/connector/__init__.py b/google/cloud/sql/connector/__init__.py index 527e177c2..9569d67ff 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.version import __version__ +from google.cloud.sql.connector.connector import Connector, create_async_connector +from google.cloud.sql.connector.instance import IPTypes -__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/requirements-test.txt b/requirements-test.txt index 1029c0029..183546371 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 7154901a8..8a700256b 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 000000000..f11718da8 --- /dev/null +++ b/tests/unit/test_packaging.py @@ -0,0 +1,30 @@ +# 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 this package. + """ + 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) From fe248fdd2c3b3478360127ea67d2c599e7b90ff2 Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Fri, 17 Nov 2023 22:02:07 +0000 Subject: [PATCH 2/4] chore: lint --- google/cloud/sql/connector/__init__.py | 2 +- tests/unit/test_packaging.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/google/cloud/sql/connector/__init__.py b/google/cloud/sql/connector/__init__.py index 9569d67ff..36007ba2d 100644 --- a/google/cloud/sql/connector/__init__.py +++ b/google/cloud/sql/connector/__init__.py @@ -14,9 +14,9 @@ limitations under the License. """ -from google.cloud.sql.connector.version import __version__ 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__ = [__version__, create_async_connector, Connector, IPTypes] diff --git a/tests/unit/test_packaging.py b/tests/unit/test_packaging.py index f11718da8..30c70b6b4 100644 --- a/tests/unit/test_packaging.py +++ b/tests/unit/test_packaging.py @@ -13,11 +13,12 @@ # limitations under the License. import os +import pathlib import subprocess import sys -def test_namespace_package_compat(tmp_path): +def test_namespace_package_compat(tmp_path: pathlib.PosixPath) -> None: """ The ``google`` namespace package should not be masked by the presence of this package. From 2df89fd154cc681ea0fa1c4cba070a751e4f951f Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Mon, 20 Nov 2023 15:05:22 +0000 Subject: [PATCH 3/4] chore: lint --- .mypy.ini | 1 + google/cloud/sql/connector/__init__.py | 2 +- noxfile.py | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.mypy.ini b/.mypy.ini index ae5530053..9556bcdb2 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/cloud/sql/connector/__init__.py b/google/cloud/sql/connector/__init__.py index 36007ba2d..fe2e40d7a 100644 --- a/google/cloud/sql/connector/__init__.py +++ b/google/cloud/sql/connector/__init__.py @@ -19,4 +19,4 @@ from google.cloud.sql.connector.version import __version__ -__all__ = [__version__, create_async_connector, Connector, IPTypes] +__all__ = ["__version__", "create_async_connector", "Connector", "IPTypes"] diff --git a/noxfile.py b/noxfile.py index dcffa6798..05c25c518 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/*") From b6b69a24e3e58f452bbff758b24c819c2970669b Mon Sep 17 00:00:00 2001 From: jackwotherspoon Date: Mon, 20 Nov 2023 18:08:20 +0000 Subject: [PATCH 4/4] chore: expand compat test --- tests/unit/test_packaging.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_packaging.py b/tests/unit/test_packaging.py index 30c70b6b4..d0611f163 100644 --- a/tests/unit/test_packaging.py +++ b/tests/unit/test_packaging.py @@ -19,13 +19,29 @@ def test_namespace_package_compat(tmp_path: pathlib.PosixPath) -> None: - """ - The ``google`` namespace package should not be masked - by the presence of this package. - """ + # 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)