Skip to content

Commit

Permalink
feat: introduce compatibility with native namespace packages (#706)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kiraksi and parthea authored Dec 1, 2023
1 parent 743cd42 commit 59f9564
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 65 deletions.
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.

9 changes: 2 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"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",
]
extras = {
"pandas": ["pandas>=0.21.1"],
"pandas": ["pandas>=0.21.1", "importlib_metadata>=1.0.0; python_version<'3.8'"],
"fastavro": ["fastavro>=0.21.2"],
"pyarrow": ["pyarrow>=0.15.0"],
}
Expand All @@ -58,14 +58,10 @@

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

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

setuptools.setup(
name=name,
version=version,
Expand All @@ -92,7 +88,6 @@
platforms="Posix; MacOS X; Windows",
packages=packages,
python_requires=">=3.7",
namespace_packages=namespaces,
install_requires=dependencies,
extras_require=extras,
scripts=["scripts/fixup_bigquery_storage_v1_keywords.py"],
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 2022 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-bigquery-storage``.
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, shell=True)

# The ``google.cloud`` namespace package should not be masked
# by the presence of ``google-cloud-bigquery-storage``.
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, shell=True)
20 changes: 10 additions & 10 deletions tests/unit/test_reader_v1_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import pandas
import pandas.testing
import pytest
import pkg_resources

try:
import importlib.metadata as metadata
except ImportError:
import importlib_metadata as metadata

import google.api_core.exceptions
from google.cloud.bigquery_storage import types
Expand All @@ -30,9 +34,9 @@
pyarrow = pytest.importorskip("pyarrow")

if pandas is not None: # pragma: NO COVER
PANDAS_INSTALLED_VERSION = pkg_resources.get_distribution("pandas").parsed_version
else: # pragma: NO COVER
PANDAS_INSTALLED_VERSION = pkg_resources.parse_version("0.0.0")
PANDAS_INSTALLED_VERSION = metadata.version("pandas")
else:
PANDAS_INSTALLED_VERSION = "0.0.0"


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


@pytest.mark.skipif(
PANDAS_INSTALLED_VERSION >= pkg_resources.parse_version("2.0.0"), reason=""
)
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
def test_to_dataframe_w_scalars_arrow(class_under_test, mock_gapic_client):
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
arrow_batches = _bq_to_arrow_batches(SCALAR_BLOCKS, arrow_schema)
Expand Down Expand Up @@ -248,9 +250,7 @@ def test_to_dataframe_w_dtypes_arrow(class_under_test, mock_gapic_client):
)


@pytest.mark.skipif(
PANDAS_INSTALLED_VERSION >= pkg_resources.parse_version("2.0.0"), reason=""
)
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
def test_to_dataframe_empty_w_scalars_arrow(class_under_test, mock_gapic_client):
arrow_schema = _bq_to_arrow_schema(SCALAR_COLUMNS)
read_session = _generate_arrow_read_session(arrow_schema)
Expand Down

0 comments on commit 59f9564

Please sign in to comment.