Skip to content

Commit

Permalink
feat: introduce compatibility with native namespace packages (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored Nov 20, 2023
1 parent 33dc2d7 commit 083da11
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 78 deletions.
1 change: 1 addition & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
python_version = 3.8
warn_unused_configs = True
plugins = sqlmypy
namespace_packages = True

[mypy-google.auth.*]
ignore_missing_imports = True
Expand Down
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.

Empty file removed google/cloud/sql/__init__.py
Empty file.
15 changes: 4 additions & 11 deletions google/cloud/sql/connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/*")

Expand Down
1 change: 0 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 22 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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,
Expand All @@ -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,
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 ``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)

0 comments on commit 083da11

Please sign in to comment.