Skip to content

Commit e718522

Browse files
authored
feat: Introduce compatibility with native namespace packages (#454)
* feat: Introduce compatibility with native namespace packages * use setuptools.find_namespace_packages() in setup.py
1 parent ca73f72 commit e718522

File tree

8 files changed

+47
-54
lines changed

8 files changed

+47
-54
lines changed

google/__init__.py

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

google/cloud/__init__.py

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

google/cloud/pubsublite/internal/wire/pubsub_context.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from typing import Mapping, Optional, NamedTuple
1717

1818
import logging
19-
import pkg_resources
19+
2020
from google.protobuf import struct_pb2 # pytype: disable=pyi-error
2121

22+
from google.cloud.pubsublite import gapic_version
23+
2224

2325
_LOGGER = logging.getLogger(__name__)
2426

@@ -29,13 +31,7 @@ class _Semver(NamedTuple):
2931

3032

3133
def _version() -> _Semver:
32-
try:
33-
version = pkg_resources.get_distribution("google-cloud-pubsublite").version
34-
except pkg_resources.DistributionNotFound:
35-
_LOGGER.info(
36-
"Failed to extract the google-cloud-pubsublite semver version. DistributionNotFound."
37-
)
38-
return _Semver(0, 0)
34+
version = gapic_version.__version__
3935
splits = version.split(".")
4036
if len(splits) != 3:
4137
_LOGGER.info(f"Failed to extract semver from {version}.")

noxfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ def pytype(session):
311311
"""Run type checks."""
312312
install_test_deps(session)
313313
session.install(PYTYPE_VERSION)
314-
session.run("pytype", "google/cloud/pubsublite")
314+
# See https://github.com/google/pytype/issues/464
315+
session.run("pytype", "-P", ".", "google/cloud/pubsublite")
315316

316317

317318
@nox.session(python="3.10")

owlbot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def pytype(session):
132132
\"\"\"Run type checks.\"\"\"
133133
install_test_deps(session)
134134
session.install(PYTYPE_VERSION)
135-
session.run("pytype", "google/cloud/pubsublite")
135+
# See https://github.com/google/pytype/issues/464
136+
session.run("pytype", "-P", ".", "google/cloud/pubsublite")
136137
137138
@nox.session(python="3.10")
138139
def docfx(session):""",

release-please-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"release-type": "python",
66
"extra-files": [
77
"google/cloud/pubsublite/gapic_version.py",
8+
"google/cloud/pubsublite_v1/gapic_version.py",
89
{
910
"type": "json",
1011
"path": "samples/generated_samples/snippet_metadata_google.cloud.pubsublite.v1.json",

setup.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,10 @@
5252

5353
packages = [
5454
package
55-
for package in setuptools.PEP420PackageFinder.find()
55+
for package in setuptools.find_namespace_packages()
5656
if package.startswith("google")
5757
]
5858

59-
namespaces = ["google"]
60-
if "google.cloud" in packages:
61-
namespaces.append("google.cloud")
62-
6359
setuptools.setup(
6460
name=name,
6561
version=version,
@@ -85,7 +81,6 @@
8581
platforms="Posix; MacOS X; Windows",
8682
packages=packages,
8783
python_requires=">=3.8",
88-
namespace_packages=namespaces,
8984
install_requires=dependencies,
9085
include_package_data=True,
9186
zip_safe=False,

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-pubsublite``.
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-pubsublite``.
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)