Skip to content

Commit 32c4137

Browse files
sfc-gh-rebelingsfc-gh-pczajka
authored andcommitted
Optimize import time: Directly lookup target distributions instead of filtering manually (#2120)
1 parent d4e1829 commit 32c4137

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

src/snowflake/connector/options.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import importlib
88
import os
99
import warnings
10-
from importlib.metadata import distributions
10+
from importlib.metadata import PackageNotFoundError, distribution
1111
from logging import getLogger
1212
from types import ModuleType
1313
from typing import Union
@@ -85,13 +85,13 @@ def _import_or_missing_pandas_option() -> (
8585
os.environ["ARROW_DEFAULT_MEMORY_POOL"] = "system"
8686

8787
# Check whether we have the currently supported pyarrow installed
88-
installed_packages = {
89-
package.metadata["Name"]: package for package in distributions()
90-
}
91-
if {"pyarrow", "snowflake-connector-python"} <= installed_packages.keys():
92-
dependencies = installed_packages[
93-
"snowflake-connector-python"
94-
].metadata.get_all("Requires-Dist", [])
88+
try:
89+
pyarrow_dist = distribution("pyarrow")
90+
snowflake_connector_dist = distribution("snowflake-connector-python")
91+
92+
dependencies = snowflake_connector_dist.metadata.get_all(
93+
"Requires-Dist", []
94+
)
9595
pandas_pyarrow_extra = None
9696
for dependency in dependencies:
9797
dep = Requirement(dependency)
@@ -103,16 +103,15 @@ def _import_or_missing_pandas_option() -> (
103103
pandas_pyarrow_extra = dep
104104
break
105105

106-
installed_pyarrow_version = installed_packages["pyarrow"].version
106+
installed_pyarrow_version = pyarrow_dist.version
107107
if not pandas_pyarrow_extra.specifier.contains(installed_pyarrow_version):
108108
warn_incompatible_dep(
109109
"pyarrow", installed_pyarrow_version, pandas_pyarrow_extra
110110
)
111111

112-
else:
112+
except PackageNotFoundError as e:
113113
logger.info(
114-
"Cannot determine if compatible pyarrow is installed because of missing package(s) from "
115-
"{}".format(list(installed_packages.keys()))
114+
f"Cannot determine if compatible pyarrow is installed because of missing package(s): {e}"
116115
)
117116
return pandas, pyarrow, True
118117
except ImportError:

test/integ/pandas/test_unit_options.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
MissingPandas = None
1919
_import_or_missing_pandas_option = None
2020

21-
from importlib.metadata import distributions
21+
from importlib.metadata import PackageNotFoundError, distribution
2222

2323

2424
@pytest.mark.skipif(
@@ -30,25 +30,23 @@ def test_pandas_option_reporting(caplog):
3030
3131
This issue was brought to attention in: https://github.com/snowflakedb/snowflake-connector-python/issues/412
3232
"""
33-
modified_distributions = list(
34-
d
35-
for d in distributions()
36-
if d.metadata["Name"]
37-
not in (
38-
"pyarrow",
39-
"snowflake-connecctor-python",
40-
)
41-
)
33+
34+
def modified_distribution(name, *args, **kwargs):
35+
if name in ["pyarrow", "snowflake-connector-python"]:
36+
raise PackageNotFoundError("TestErrorMessage")
37+
return distribution(name, *args, **kwargs)
38+
4239
with mock.patch(
43-
"snowflake.connector.options.distributions",
44-
return_value=modified_distributions,
40+
"snowflake.connector.options.distribution",
41+
wraps=modified_distribution,
4542
):
4643
caplog.set_level(logging.DEBUG, "snowflake.connector")
4744
pandas, pyarrow, installed_pandas = _import_or_missing_pandas_option()
4845
assert installed_pandas
4946
assert not isinstance(pandas, MissingPandas)
5047
assert not isinstance(pyarrow, MissingPandas)
5148
assert (
52-
"Cannot determine if compatible pyarrow is installed because of missing package(s) "
53-
"from "
54-
) in caplog.text
49+
"Cannot determine if compatible pyarrow is installed because of missing package(s)"
50+
in caplog.text
51+
)
52+
assert "TestErrorMessage" in caplog.text

0 commit comments

Comments
 (0)