Skip to content

Commit c2c5413

Browse files
authored
Revert "feat: removed pkg_resources from all test files and moved importlib into pandas extra (#1726)"
This reverts commit 1f4ebb1.
1 parent 1f4ebb1 commit c2c5413

12 files changed

+133
-96
lines changed

.coveragerc

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ exclude_lines =
1212
pragma: NO COVER
1313
# Ignore debug-only repr
1414
def __repr__
15+
# Ignore pkg_resources exceptions.
16+
# This is added at the module level as a safeguard for if someone
17+
# generates the code and tries to run it without pip installing. This
18+
# makes it virtually impossible to test properly.
19+
except pkg_resources.DistributionNotFound

google/__init__.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
try:
18+
import pkg_resources
19+
20+
pkg_resources.declare_namespace(__name__)
21+
except ImportError:
22+
import pkgutil
23+
24+
__path__ = pkgutil.extend_path(__path__, __name__) # type: ignore

google/cloud/__init__.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2019 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
try:
18+
import pkg_resources
19+
20+
pkg_resources.declare_namespace(__name__)
21+
except ImportError:
22+
import pkgutil
23+
24+
__path__ = pkgutil.extend_path(__path__, __name__) # type: ignore

noxfile.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def mypy(session):
137137
"types-requests",
138138
"types-setuptools",
139139
)
140-
session.run("mypy", "-p", "google", "--show-traceback")
140+
session.run("mypy", "google/cloud", "--show-traceback")
141141

142142

143143
@nox.session(python=DEFAULT_PYTHON_VERSION)
@@ -149,8 +149,7 @@ def pytype(session):
149149
session.install("attrs==20.3.0")
150150
session.install("-e", ".[all]")
151151
session.install(PYTYPE_VERSION)
152-
# See https://github.com/google/pytype/issues/464
153-
session.run("pytype", "-P", ".", "google/cloud/bigquery")
152+
session.run("pytype")
154153

155154

156155
@nox.session(python=SYSTEM_TEST_PYTHON_VERSIONS)

setup.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"pandas>=1.1.0",
6363
pyarrow_dependency,
6464
"db-dtypes>=0.3.0,<2.0.0dev",
65-
"importlib_metadata>=1.0.0; python_version<'3.8'",
6665
],
6766
"ipywidgets": [
6867
"ipywidgets>=7.7.0",
@@ -109,10 +108,16 @@
109108
# benchmarks, etc.
110109
packages = [
111110
package
112-
for package in setuptools.find_namespace_packages()
111+
for package in setuptools.PEP420PackageFinder.find()
113112
if package.startswith("google")
114113
]
115114

115+
# Determine which namespaces are needed.
116+
namespaces = ["google"]
117+
if "google.cloud" in packages:
118+
namespaces.append("google.cloud")
119+
120+
116121
setuptools.setup(
117122
name=name,
118123
version=version,
@@ -138,6 +143,7 @@
138143
],
139144
platforms="Posix; MacOS X; Windows",
140145
packages=packages,
146+
namespace_packages=namespaces,
141147
install_requires=dependencies,
142148
extras_require=extras,
143149
python_requires=">=3.7",

tests/system/test_pandas.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@
2323
import warnings
2424

2525
import google.api_core.retry
26+
import pkg_resources
2627
import pytest
2728

28-
try:
29-
import importlib.metadata as metadata
30-
except ImportError:
31-
import importlib_metadata as metadata
32-
3329
from google.cloud import bigquery
3430

3531
from google.cloud.bigquery import enums
@@ -46,9 +42,11 @@
4642
)
4743

4844
if pandas is not None:
49-
PANDAS_INSTALLED_VERSION = metadata.version("pandas")
45+
PANDAS_INSTALLED_VERSION = pkg_resources.get_distribution("pandas").parsed_version
5046
else:
51-
PANDAS_INSTALLED_VERSION = "0.0.0"
47+
PANDAS_INSTALLED_VERSION = pkg_resources.parse_version("0.0.0")
48+
49+
PANDAS_INT64_VERSION = pkg_resources.parse_version("1.0.0")
5250

5351

5452
class MissingDataError(Exception):
@@ -312,7 +310,10 @@ def test_load_table_from_dataframe_w_automatic_schema(bigquery_client, dataset_i
312310
]
313311

314312

315-
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
313+
@pytest.mark.skipif(
314+
PANDAS_INSTALLED_VERSION < PANDAS_INT64_VERSION,
315+
reason="Only `pandas version >=1.0.0` is supported",
316+
)
316317
def test_load_table_from_dataframe_w_nullable_int64_datatype(
317318
bigquery_client, dataset_id
318319
):
@@ -341,7 +342,7 @@ def test_load_table_from_dataframe_w_nullable_int64_datatype(
341342

342343

343344
@pytest.mark.skipif(
344-
PANDAS_INSTALLED_VERSION[0:2].startswith("0."),
345+
PANDAS_INSTALLED_VERSION < PANDAS_INT64_VERSION,
345346
reason="Only `pandas version >=1.0.0` is supported",
346347
)
347348
def test_load_table_from_dataframe_w_nullable_int64_datatype_automatic_schema(
@@ -1042,7 +1043,9 @@ def test_list_rows_max_results_w_bqstorage(bigquery_client):
10421043
assert len(dataframe.index) == 100
10431044

10441045

1045-
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
1046+
@pytest.mark.skipif(
1047+
PANDAS_INSTALLED_VERSION >= pkg_resources.parse_version("2.0.0"), reason=""
1048+
)
10461049
@pytest.mark.parametrize(
10471050
("max_results",),
10481051
(

tests/unit/job/test_query_pandas.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import json
1818

1919
import mock
20+
import pkg_resources
2021
import pytest
2122

2223

@@ -44,19 +45,14 @@
4445
except (ImportError, AttributeError): # pragma: NO COVER
4546
tqdm = None
4647

47-
try:
48-
import importlib.metadata as metadata
49-
except ImportError:
50-
import importlib_metadata as metadata
51-
5248
from ..helpers import make_connection
5349
from .helpers import _make_client
5450
from .helpers import _make_job_resource
5551

5652
if pandas is not None:
57-
PANDAS_INSTALLED_VERSION = metadata.version("pandas")
53+
PANDAS_INSTALLED_VERSION = pkg_resources.get_distribution("pandas").parsed_version
5854
else:
59-
PANDAS_INSTALLED_VERSION = "0.0.0"
55+
PANDAS_INSTALLED_VERSION = pkg_resources.parse_version("0.0.0")
6056

6157
pandas = pytest.importorskip("pandas")
6258

@@ -660,7 +656,9 @@ def test_to_dataframe_bqstorage_no_pyarrow_compression():
660656
)
661657

662658

663-
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
659+
@pytest.mark.skipif(
660+
PANDAS_INSTALLED_VERSION >= pkg_resources.parse_version("2.0.0"), reason=""
661+
)
664662
@pytest.mark.skipif(pyarrow is None, reason="Requires `pyarrow`")
665663
def test_to_dataframe_column_dtypes():
666664
from google.cloud.bigquery.job import QueryJob as target_class

tests/unit/test__pandas_helpers.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import operator
2020
import queue
2121
import warnings
22-
23-
try:
24-
import importlib.metadata as metadata
25-
except ImportError:
26-
import importlib_metadata as metadata
22+
import pkg_resources
2723

2824
import mock
2925

@@ -61,10 +57,13 @@
6157

6258
bigquery_storage = _versions_helpers.BQ_STORAGE_VERSIONS.try_import()
6359

60+
PANDAS_MINIUM_VERSION = pkg_resources.parse_version("1.0.0")
61+
6462
if pandas is not None:
65-
PANDAS_INSTALLED_VERSION = metadata.version("pandas")
63+
PANDAS_INSTALLED_VERSION = pkg_resources.get_distribution("pandas").parsed_version
6664
else:
67-
PANDAS_INSTALLED_VERSION = "0.0.0"
65+
# Set to less than MIN version.
66+
PANDAS_INSTALLED_VERSION = pkg_resources.parse_version("0.0.0")
6867

6968

7069
skip_if_no_bignumeric = pytest.mark.skipif(
@@ -543,7 +542,9 @@ def test_bq_to_arrow_array_w_nullable_scalars(module_under_test, bq_type, rows):
543542
],
544543
)
545544
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
546-
@pytest.mark.skipif(PANDAS_INSTALLED_VERSION[0:2] not in ["0.", "1."], reason="")
545+
@pytest.mark.skipif(
546+
PANDAS_INSTALLED_VERSION >= pkg_resources.parse_version("2.0.0"), reason=""
547+
)
547548
@pytest.mark.skipif(isinstance(pyarrow, mock.Mock), reason="Requires `pyarrow`")
548549
def test_bq_to_arrow_array_w_pandas_timestamp(module_under_test, bq_type, rows):
549550
rows = [pandas.Timestamp(row) for row in rows]
@@ -805,7 +806,10 @@ def test_list_columns_and_indexes_with_named_index_same_as_column_name(
805806
assert columns_and_indexes == expected
806807

807808

808-
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
809+
@pytest.mark.skipif(
810+
pandas is None or PANDAS_INSTALLED_VERSION < PANDAS_MINIUM_VERSION,
811+
reason="Requires `pandas version >= 1.0.0` which introduces pandas.NA",
812+
)
809813
def test_dataframe_to_json_generator(module_under_test):
810814
utcnow = datetime.datetime.utcnow()
811815
df_data = collections.OrderedDict(
@@ -833,8 +837,16 @@ def test_dataframe_to_json_generator(module_under_test):
833837
assert list(rows) == expected
834838

835839

836-
@pytest.mark.skipif(pandas is None, reason="Requires `pandas`")
837840
def test_dataframe_to_json_generator_repeated_field(module_under_test):
841+
pytest.importorskip(
842+
"pandas",
843+
minversion=str(PANDAS_MINIUM_VERSION),
844+
reason=(
845+
f"Requires `pandas version >= {PANDAS_MINIUM_VERSION}` "
846+
"which introduces pandas.NA"
847+
),
848+
)
849+
838850
df_data = [
839851
collections.OrderedDict(
840852
[("repeated_col", [pandas.NA, 2, None, 4]), ("not_repeated_col", "first")]

tests/unit/test_client.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
import requests
3131
import packaging
3232
import pytest
33-
34-
try:
35-
import importlib.metadata as metadata
36-
except ImportError:
37-
import importlib_metadata as metadata
33+
import pkg_resources
3834

3935
try:
4036
import pandas
@@ -80,10 +76,13 @@
8076
from test_utils.imports import maybe_fail_import
8177
from tests.unit.helpers import make_connection
8278

79+
PANDAS_MINIUM_VERSION = pkg_resources.parse_version("1.0.0")
80+
8381
if pandas is not None:
84-
PANDAS_INSTALLED_VERSION = metadata.version("pandas")
82+
PANDAS_INSTALLED_VERSION = pkg_resources.get_distribution("pandas").parsed_version
8583
else:
86-
PANDAS_INSTALLED_VERSION = "0.0.0"
84+
# Set to less than MIN version.
85+
PANDAS_INSTALLED_VERSION = pkg_resources.parse_version("0.0.0")
8786

8887

8988
def _make_credentials():
@@ -8146,7 +8145,10 @@ def test_load_table_from_dataframe_unknown_table(self):
81468145
timeout=DEFAULT_TIMEOUT,
81478146
)
81488147

8149-
@unittest.skipIf(pandas is None, "Requires `pandas`")
8148+
@unittest.skipIf(
8149+
pandas is None or PANDAS_INSTALLED_VERSION < PANDAS_MINIUM_VERSION,
8150+
"Only `pandas version >=1.0.0` supported",
8151+
)
81508152
@unittest.skipIf(pyarrow is None, "Requires `pyarrow`")
81518153
def test_load_table_from_dataframe_w_nullable_int64_datatype(self):
81528154
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
@@ -8191,7 +8193,10 @@ def test_load_table_from_dataframe_w_nullable_int64_datatype(self):
81918193
SchemaField("x", "INT64", "NULLABLE", None),
81928194
)
81938195

8194-
@unittest.skipIf(pandas is None, "Requires `pandas`")
8196+
@unittest.skipIf(
8197+
pandas is None or PANDAS_INSTALLED_VERSION < PANDAS_MINIUM_VERSION,
8198+
"Only `pandas version >=1.0.0` supported",
8199+
)
81958200
# @unittest.skipIf(pyarrow is None, "Requires `pyarrow`")
81968201
def test_load_table_from_dataframe_w_nullable_int64_datatype_automatic_schema(self):
81978202
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES

tests/unit/test_packaging.py

-37
This file was deleted.

0 commit comments

Comments
 (0)