Skip to content

Commit 7b00025

Browse files
ofeksfc-gh-mkeller
andauthored
Replace runtime dependency on setuptools with modern libraries (#1448)
Co-authored-by: Mark Keller <[email protected]>
1 parent 12f7d03 commit 7b00025

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

DESCRIPTION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
1313
- Improved the robustness of OCSP response caching to handle errors in cases of serialization and deserialization.
1414
- Fixed a bug where `AuthByKeyPair.handle_timeout` should pass keyword arguments instead of positional arguments when calling `AuthByKeyPair.prepare`. PR #1440 (@emilhe)
1515
- Fixed a bug where MFA token caching would refuse to work until restarted instead of reauthenticating
16+
- Replaced dependency on setuptools in favor of packaging
1617

1718
- v3.0.0(January 26, 2023)
1819

setup.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ install_requires =
5252
pyjwt<3.0.0
5353
pytz
5454
requests<3.0.0
55-
setuptools>34.0.0
55+
importlib-metadata; python_version < '3.8'
56+
packaging
5657
charset_normalizer>=2,<3
5758
idna>=2.5,<4
5859
urllib3>=1.21.1,<1.27

src/snowflake/connector/options.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
from __future__ import annotations
66

77
import importlib
8+
import sys
89
import warnings
910
from logging import getLogger
1011
from types import ModuleType
1112
from typing import Union
1213

13-
import pkg_resources
14+
from packaging.requirements import Requirement
1415

1516
from . import errors
1617

18+
if sys.version_info >= (3, 8):
19+
from importlib.metadata import distributions
20+
else:
21+
from importlib_metadata import distributions
22+
1723
logger = getLogger(__name__)
1824

1925
"""This module helps to manage optional dependencies.
@@ -54,7 +60,7 @@ class MissingKeyring(MissingOptionalDependency):
5460

5561

5662
def warn_incompatible_dep(
57-
dep_name: str, installed_ver: str, expected_ver: pkg_resources.Requirement
63+
dep_name: str, installed_ver: str, expected_ver: Requirement
5864
) -> None:
5965
warnings.warn(
6066
"You have an incompatible version of '{}' installed ({}), please install a version that "
@@ -78,31 +84,34 @@ def _import_or_missing_pandas_option() -> tuple[
7884

7985
pyarrow = importlib.import_module("pyarrow")
8086
# Check whether we have the currently supported pyarrow installed
81-
installed_packages = pkg_resources.working_set.by_key
82-
if all(
83-
k in installed_packages for k in ("snowflake-connector-python", "pyarrow")
84-
):
85-
_pandas_extras = installed_packages["snowflake-connector-python"]._dep_map[
86-
"pandas"
87-
]
88-
_expected_pyarrow_version = [
89-
dep for dep in _pandas_extras if dep.name == "pyarrow"
90-
][0]
91-
_installed_pyarrow_version = installed_packages["pyarrow"]
92-
if (
93-
_installed_pyarrow_version
94-
and _installed_pyarrow_version.version not in _expected_pyarrow_version
95-
):
87+
installed_packages = {
88+
package.metadata["Name"]: package for package in distributions()
89+
}
90+
if {"pyarrow", "snowflake-connector-python"} <= installed_packages.keys():
91+
dependencies = installed_packages[
92+
"snowflake-connector-python"
93+
].metadata.get_all("Requires-Dist", [])
94+
pandas_pyarrow_extra = None
95+
for dependency in dependencies:
96+
dep = Requirement(dependency)
97+
if (
98+
dep.marker is not None
99+
and dep.marker.evaluate({"extra": "pandas"})
100+
and dep.name == "pyarrow"
101+
):
102+
pandas_pyarrow_extra = dep
103+
break
104+
105+
installed_pyarrow_version = installed_packages["pyarrow"].version
106+
if not pandas_pyarrow_extra.specifier.contains(installed_pyarrow_version):
96107
warn_incompatible_dep(
97-
"pyarrow",
98-
_installed_pyarrow_version.version,
99-
_expected_pyarrow_version,
108+
"pyarrow", installed_pyarrow_version, pandas_pyarrow_extra
100109
)
101110

102111
else:
103112
logger.info(
104113
"Cannot determine if compatible pyarrow is installed because of missing package(s) from "
105-
"{}".format(installed_packages.keys())
114+
"{}".format(list(installed_packages.keys()))
106115
)
107116
return pandas, pyarrow, True
108117
except ImportError:

test/integ/pandas/test_unit_options.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
from __future__ import annotations
55

66
import logging
7-
from copy import deepcopy
7+
import sys
88
from unittest import mock
99

1010
import pytest
11-
from pkg_resources import working_set
1211

1312
try:
1413
from snowflake.connector.options import (
@@ -19,6 +18,11 @@
1918
MissingPandas = None
2019
_import_or_missing_pandas_option = None
2120

21+
if sys.version_info >= (3, 8):
22+
from importlib.metadata import distributions
23+
else:
24+
from importlib_metadata import distributions
25+
2226

2327
@pytest.mark.skipif(
2428
MissingPandas is None or _import_or_missing_pandas_option is None,
@@ -29,16 +33,25 @@ def test_pandas_option_reporting(caplog):
2933
3034
This issue was brought to attention in: https://github.com/snowflakedb/snowflake-connector-python/issues/412
3135
"""
32-
modified_by_key = deepcopy(working_set.by_key)
33-
modified_by_key.pop("snowflake-connector-python")
34-
modified_by_key.pop("pyarrow")
35-
with mock.patch.object(working_set, "by_key", modified_by_key):
36+
modified_distributions = list(
37+
d
38+
for d in distributions()
39+
if d.metadata["Name"]
40+
not in (
41+
"pyarrow",
42+
"snowflake-connecctor-python",
43+
)
44+
)
45+
with mock.patch(
46+
"snowflake.connector.options.distributions",
47+
return_value=modified_distributions,
48+
):
3649
caplog.set_level(logging.DEBUG, "snowflake.connector")
3750
pandas, pyarrow, installed_pandas = _import_or_missing_pandas_option()
3851
assert installed_pandas
3952
assert not isinstance(pandas, MissingPandas)
4053
assert not isinstance(pyarrow, MissingPandas)
4154
assert (
4255
"Cannot determine if compatible pyarrow is installed because of missing package(s) "
43-
"from dict_keys(["
56+
"from "
4457
) in caplog.text

0 commit comments

Comments
 (0)