Skip to content

Commit b0089ce

Browse files
committed
feat: Introduce compatibility with native namespace packages
1 parent 9362395 commit b0089ce

File tree

5 files changed

+48
-59
lines changed

5 files changed

+48
-59
lines changed

google/__init__.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

google/cloud/__init__.py

Lines changed: 0 additions & 24 deletions
This file was deleted.

google/cloud/obsolete/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
"""Helpers for deprecated code and modules."""
1616

1717
import warnings
18+
import sys
1819

19-
import pkg_resources
20+
if sys.version_info < (3, 8):
21+
import importlib_metadata as metadata
22+
else:
23+
import importlib.metadata as metadata
2024

2125

2226
def complain(distribution_name):
@@ -29,7 +33,7 @@ def complain(distribution_name):
2933
distribution_name (str): The name of the obsolete distribution.
3034
"""
3135
try:
32-
pkg_resources.get_distribution(distribution_name)
36+
metadata.distribution(distribution_name)
3337
warnings.warn(
3438
"The {pkg} distribution is now obsolete. "
3539
"Please `pip uninstall {pkg}`. "
@@ -38,5 +42,5 @@ def complain(distribution_name):
3842
),
3943
DeprecationWarning,
4044
)
41-
except pkg_resources.DistributionNotFound:
45+
except metadata.PackageNotFoundError:
4246
pass

setup.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
dependencies = [
3131
"google-api-core >= 1.31.6, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0",
3232
"google-auth >= 1.25.0, < 3.0dev",
33+
"importlib-metadata > 1.0.0; python_version<'3.8'",
3334
]
3435
extras = {"grpc": "grpcio >= 1.38.0, < 2.0dev"}
3536

@@ -50,15 +51,11 @@
5051
# Only include packages under the 'google' namespace. Do not include tests,
5152
# benchmarks, etc.
5253
packages = [
53-
package for package in setuptools.find_packages() if package.startswith("google")
54+
package
55+
for package in setuptools.find_namespace_packages()
56+
if package.startswith("google")
5457
]
5558

56-
# Determine which namespaces are needed.
57-
namespaces = ["google"]
58-
if "google.cloud" in packages:
59-
namespaces.append("google.cloud")
60-
61-
6259
setuptools.setup(
6360
name=name,
6461
version=version,
@@ -83,7 +80,6 @@
8380
],
8481
platforms="Posix; MacOS X; Windows",
8582
packages=packages,
86-
namespace_packages=namespaces,
8783
install_requires=dependencies,
8884
extras_require=extras,
8985
python_requires=">=3.7",

tests/unit/test_packaging.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import subprocess
17+
import sys
18+
19+
20+
def test_namespace_package_compat(tmp_path):
21+
# The ``google`` namespace package should not be masked
22+
# by the presence of ``google-cloud-core``.
23+
google = tmp_path / "google"
24+
google.mkdir()
25+
google.joinpath("othermod.py").write_text("")
26+
env = dict(os.environ, PYTHONPATH=str(tmp_path))
27+
cmd = [sys.executable, "-m", "google.othermod"]
28+
subprocess.check_call(cmd, env=env)
29+
30+
# The ``google.cloud`` namespace package should not be masked
31+
# by the presence of ``google-cloud-core``.
32+
google_cloud = tmp_path / "google" / "cloud"
33+
google_cloud.mkdir()
34+
google_cloud.joinpath("othermod.py").write_text("")
35+
env = dict(os.environ, PYTHONPATH=str(tmp_path))
36+
cmd = [sys.executable, "-m", "google.cloud.othermod"]
37+
subprocess.check_call(cmd, env=env)

0 commit comments

Comments
 (0)