Skip to content

Commit

Permalink
feat: Introduce compatibility with native namespace packages
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea committed Nov 15, 2023
1 parent d3f44ff commit 4a19b0b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
15 changes: 0 additions & 15 deletions google/iam/__init__.py

This file was deleted.

8 changes: 1 addition & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@

packages = [
package
for package in setuptools.PEP420PackageFinder.find()
for package in setuptools.find_namespace_packages()
if package.startswith("google")
]

namespaces = ["google"]
if "google.iam" in packages:
namespaces.append("google.iam")


setuptools.setup(
name=name,
version=version,
Expand All @@ -74,7 +69,6 @@
platforms="Posix; MacOS X; Windows",
packages=packages,
python_requires=">=3.7",
namespace_packages=namespaces,
install_requires=dependencies,
include_package_data=True,
zip_safe=False,
Expand Down
11 changes: 8 additions & 3 deletions google/__init__.py → tests/unit/test_google_iam.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Copyright 2022 Google LLC
# 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
# 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__("pkg_resources").declare_namespace(__name__)
from google.iam.v1 import policy_pb2

def test_create_policy():
# Check that the import works
# and that an Policy instance can be instantiated
policy_pb2.Policy()
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 ``grpc-google-iam-v1``.
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.iam`` namespace package should not be masked
# by the presence of ``grpc-google-iam-v1``.
google_iam = tmp_path / "google" / "iam"
google_iam.mkdir()
google_iam.joinpath("othermod.py").write_text("")
env = dict(os.environ, PYTHONPATH=str(tmp_path))
cmd = [sys.executable, "-m", "google.iam.othermod"]
subprocess.check_call(cmd, env=env)

0 comments on commit 4a19b0b

Please sign in to comment.