Skip to content

feat: Introduce compatibility with native namespace packages #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ branch = True

[report]
omit =
/tmp/*
google/cloud/__init__.py
google/cloud/_testing/__init__.py
google/cloud/environment_vars/__init__.py
Expand Down
24 changes: 0 additions & 24 deletions google/__init__.py

This file was deleted.

24 changes: 0 additions & 24 deletions google/cloud/__init__.py

This file was deleted.

11 changes: 8 additions & 3 deletions google/cloud/obsolete/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@

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

import sys
import warnings

import pkg_resources

if sys.version_info < (3, 8):
import importlib_metadata as metadata
else:
import importlib.metadata as metadata


def complain(distribution_name):
Expand All @@ -29,7 +34,7 @@ def complain(distribution_name):
distribution_name (str): The name of the obsolete distribution.
"""
try:
pkg_resources.get_distribution(distribution_name)
metadata.distribution(distribution_name)
warnings.warn(
"The {pkg} distribution is now obsolete. "
"Please `pip uninstall {pkg}`. "
Expand All @@ -38,5 +43,5 @@ def complain(distribution_name):
),
DeprecationWarning,
)
except pkg_resources.DistributionNotFound:
except metadata.PackageNotFoundError:
pass
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def mypy(session):
"types-mock",
"types-protobuf",
)
session.run("mypy", "google", "tests")
session.run("mypy", "-p", "google", "-p", "tests")


@nox.session(python=DEFAULT_PYTHON_VERSION)
Expand Down
2 changes: 1 addition & 1 deletion owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
".flake8",
".coveragerc",
"setup.cfg",
"README.rst",
"README.rst",
],
)

Expand Down
12 changes: 4 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
dependencies = [
"google-api-core >= 1.31.6, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0",
"google-auth >= 1.25.0, < 3.0dev",
"importlib-metadata > 1.0.0; python_version<'3.8'",
]
extras = {"grpc": "grpcio >= 1.38.0, < 2.0dev"}

Expand All @@ -50,15 +51,11 @@
# Only include packages under the 'google' namespace. Do not include tests,
# benchmarks, etc.
packages = [
package for package in setuptools.find_packages() if package.startswith("google")
package
for package in setuptools.find_namespace_packages()
if package.startswith("google")
]

# Determine which namespaces are needed.
namespaces = ["google"]
if "google.cloud" in packages:
namespaces.append("google.cloud")


setuptools.setup(
name=name,
version=version,
Expand All @@ -83,7 +80,6 @@
],
platforms="Posix; MacOS X; Windows",
packages=packages,
namespace_packages=namespaces,
install_requires=dependencies,
extras_require=extras,
python_requires=">=3.7",
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_packaging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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 ``google-cloud-core``.
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 ``google-cloud-core``.
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)