Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 59f9564

Browse files
kiraksiparthea
andauthored
feat: introduce compatibility with native namespace packages (#706)
* feat: introduce compatibility with native namespace packages * reformatted with black * removed pkg_resources * Moved importlib to extras and resolved issues with using it in tests * reformatted with black * Fixed mistake of not checking only first 3 indexes of PANDAS_VERSION * Fixed another error in comparison strings * use setuptools.find_namespace_packages() --------- Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 743cd42 commit 59f9564

5 files changed

Lines changed: 49 additions & 65 deletions

File tree

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.

setup.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"protobuf>=3.19.5,<5.0.0dev,!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5",
4545
]
4646
extras = {
47-
"pandas": ["pandas>=0.21.1"],
47+
"pandas": ["pandas>=0.21.1", "importlib_metadata>=1.0.0; python_version<'3.8'"],
4848
"fastavro": ["fastavro>=0.21.2"],
4949
"pyarrow": ["pyarrow>=0.15.0"],
5050
}
@@ -58,14 +58,10 @@
5858

5959
packages = [
6060
package
61-
for package in setuptools.PEP420PackageFinder.find()
61+
for package in setuptools.find_namespace_packages()
6262
if package.startswith("google")
6363
]
6464

65-
namespaces = ["google"]
66-
if "google.cloud" in packages:
67-
namespaces.append("google.cloud")
68-
6965
setuptools.setup(
7066
name=name,
7167
version=version,
@@ -92,7 +88,6 @@
9288
platforms="Posix; MacOS X; Windows",
9389
packages=packages,
9490
python_requires=">=3.7",
95-
namespace_packages=namespaces,
9691
install_requires=dependencies,
9792
extras_require=extras,
9893
scripts=["scripts/fixup_bigquery_storage_v1_keywords.py"],

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 2022 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-bigquery-storage``.
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, shell=True)
29+
30+
# The ``google.cloud`` namespace package should not be masked
31+
# by the presence of ``google-cloud-bigquery-storage``.
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, shell=True)

tests/unit/test_reader_v1_arrow.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
import pandas
2121
import pandas.testing
2222
import pytest
23-
import pkg_resources
23+
24+
try:
25+
import importlib.metadata as metadata
26+
except ImportError:
27+
import importlib_metadata as metadata
2428

2529
import google.api_core.exceptions
2630
from google.cloud.bigquery_storage import types
@@ -30,9 +34,9 @@
3034
pyarrow = pytest.importorskip("pyarrow")
3135

3236
if pandas is not None: # pragma: NO COVER
33-
PANDAS_INSTALLED_VERSION = pkg_resources.get_distribution("pandas").parsed_version
34-
else: # pragma: NO COVER
35-
PANDAS_INSTALLED_VERSION = pkg_resources.parse_version("0.0.0")
37+
PANDAS_INSTALLED_VERSION = metadata.version("pandas")
38+
else:
39+
PANDAS_INSTALLED_VERSION = "0.0.0"
3640

3741

3842
# This dictionary is duplicated in bigquery/google/cloud/bigquery/_pandas_helpers.py
@@ -178,9 +182,7 @@ def test_to_arrow_w_scalars_arrow(class_under_test, mock_gapic_client):
178182
assert actual_table == expected_table
179183

180184

181-
@pytest.mark.skipif(
182-
PANDAS_INSTALLED_VERSION >= pkg_resources.parse_version("2.0.0"), reason=""
183-
)
185+
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
184186
def test_to_dataframe_w_scalars_arrow(class_under_test, mock_gapic_client):
185187
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
186188
arrow_batches = _bq_to_arrow_batches(SCALAR_BLOCKS, arrow_schema)
@@ -248,9 +250,7 @@ def test_to_dataframe_w_dtypes_arrow(class_under_test, mock_gapic_client):
248250
)
249251

250252

251-
@pytest.mark.skipif(
252-
PANDAS_INSTALLED_VERSION >= pkg_resources.parse_version("2.0.0"), reason=""
253-
)
253+
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
254254
def test_to_dataframe_empty_w_scalars_arrow(class_under_test, mock_gapic_client):
255255
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
256256
read_session = _generate_arrow_read_session(arrow_schema)

0 commit comments

Comments
 (0)