Skip to content

Commit fd1ef6e

Browse files
authored
feat: Introduce compatibility with native namespace packages (#260)
* feat: Introduce compatibility with native namespace packages * lint * mypy * omit /tmp/ in coverage
1 parent 9362395 commit fd1ef6e

File tree

8 files changed

+52
-61
lines changed

8 files changed

+52
-61
lines changed

.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ branch = True
33

44
[report]
55
omit =
6+
/tmp/*
67
google/cloud/__init__.py
78
google/cloud/_testing/__init__.py
89
google/cloud/environment_vars/__init__.py

google/__init__.py

-24
This file was deleted.

google/cloud/__init__.py

-24
This file was deleted.

google/cloud/obsolete/__init__.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414

1515
"""Helpers for deprecated code and modules."""
1616

17+
import sys
1718
import warnings
1819

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

2126

2227
def complain(distribution_name):
@@ -29,7 +34,7 @@ def complain(distribution_name):
2934
distribution_name (str): The name of the obsolete distribution.
3035
"""
3136
try:
32-
pkg_resources.get_distribution(distribution_name)
37+
metadata.distribution(distribution_name)
3338
warnings.warn(
3439
"The {pkg} distribution is now obsolete. "
3540
"Please `pip uninstall {pkg}`. "
@@ -38,5 +43,5 @@ def complain(distribution_name):
3843
),
3944
DeprecationWarning,
4045
)
41-
except pkg_resources.DistributionNotFound:
46+
except metadata.PackageNotFoundError:
4247
pass

noxfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def mypy(session):
4848
"types-mock",
4949
"types-protobuf",
5050
)
51-
session.run("mypy", "google", "tests")
51+
session.run("mypy", "-p", "google", "-p", "tests")
5252

5353

5454
@nox.session(python=DEFAULT_PYTHON_VERSION)

owlbot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
".flake8",
3636
".coveragerc",
3737
"setup.cfg",
38-
"README.rst",
38+
"README.rst",
3939
],
4040
)
4141

setup.py

+4-8
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

+37
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)