Repository: ibis-project/ibis

Issue:
bug(snowflake): import of snowflake connector doesn't happen with snowflake-connector-python versions < 3.3.0
              Thank you @cpcloud :)

I had to cleanup the environment and reinstall ibis' latest dev version (`9.0.0.dev457`)


I can confirm there are no traces of `SQLAlchemy` (or `Snowflake-SQLAlchemy`) in the environment anymore.


See pipdeptree results below,

```
duckdb==0.10.1
ibis-framework==9.0.0.dev457
├── atpublic [required: >=2.3,<5, installed: 4.0]
├── bidict [required: >=0.22.1,<1, installed: 0.23.1]
├── numpy [required: >=1,<2, installed: 1.26.4]
├── pandas [required: >=1.2.5,<3, installed: 1.5.3]
│   ├── numpy [required: >=1.21.0, installed: 1.26.4]
│   ├── python-dateutil [required: >=2.8.1, installed: 2.9.0.post0]
│   │   └── six [required: >=1.5, installed: 1.16.0]
│   └── pytz [required: >=2020.1, installed: 2024.1]
├── parsy [required: >=2,<3, installed: 2.1]
├── pyarrow [required: >=2,<16, installed: 10.0.1]
│   └── numpy [required: >=1.16.6, installed: 1.26.4]
├── pyarrow-hotfix [required: >=0.4,<1, installed: 0.6]
├── python-dateutil [required: >=2.8.2,<3, installed: 2.9.0.post0]
│   └── six [required: >=1.5, installed: 1.16.0]
├── pytz [required: >=2022.7, installed: 2024.1]
├── rich [required: >=12.4.4,<14, installed: 13.7.1]
│   ├── markdown-it-py [required: >=2.2.0, installed: 3.0.0]
│   │   └── mdurl [required: ~=0.1, installed: 0.1.2]
│   └── Pygments [required: >=2.13.0,<3.0.0, installed: 2.17.2]
├── sqlglot [required: >=22.4,<22.6, installed: 22.5.0]
├── toolz [required: >=0.11,<1, installed: 0.12.1]
└── typing_extensions [required: >=4.3.0,<5, installed: 4.10.0]
keyring==23.13.1
├── importlib_metadata [required: >=4.11.4, installed: 7.1.0]
│   └── zipp [required: >=0.5, installed: 3.18.1]
└── jaraco.classes [required: Any, installed: 3.3.1]
    └── more-itertools [required: Any, installed: 10.2.0]
pip==23.3.1
pipdeptree==2.16.1
regex==2023.12.25
setuptools==68.2.2
snowflake-connector-python==3.0.3
├── asn1crypto [required: >0.24.0,<2.0.0, installed: 1.5.1]
├── certifi [required: >=2017.4.17, installed: 2024.2.2]
├── cffi [required: >=1.9,<2.0.0, installed: 1.16.0]
│   └── pycparser [required: Any, installed: 2.21]
├── charset-normalizer [required: >=2,<3, installed: 2.1.1]
├── cryptography [required: >=3.1.0,<41.0.0, installed: 40.0.2]
│   └── cffi [required: >=1.12, installed: 1.16.0]
│       └── pycparser [required: Any, installed: 2.21]
├── filelock [required: >=3.5,<4, installed: 3.13.1]
├── idna [required: >=2.5,<4, installed: 3.6]
├── oscrypto [required: <2.0.0, installed: 1.3.0]
│   └── asn1crypto [required: >=1.5.1, installed: 1.5.1]
├── packaging [required: Any, installed: 24.0]
├── pycryptodomex [required: >=3.2,<4.0.0,!=3.5.0, installed: 3.20.0]
├── PyJWT [required: <3.0.0, installed: 2.8.0]
├── pyOpenSSL [required: >=16.2.0,<24.0.0, installed: 23.2.0]
│   └── cryptography [required: >=38.0.0,<42,!=40.0.1,!=40.0.0, installed: 40.0.2]
│       └── cffi [required: >=1.12, installed: 1.16.0]
│           └── pycparser [required: Any, installed: 2.21]
├── pytz [required: Any, installed: 2024.1]
├── requests [required: <3.0.0, installed: 2.31.0]
│   ├── certifi [required: >=2017.4.17, installed: 2024.2.2]
│   ├── charset-normalizer [required: >=2,<4, installed: 2.1.1]
│   ├── idna [required: >=2.5,<4, installed: 3.6]
│   └── urllib3 [required: >=1.21.1,<3, installed: 1.26.18]
├── typing_extensions [required: >=4.3,<5, installed: 4.10.0]
└── urllib3 [required: >=1.21.1,<1.27, installed: 1.26.18]
wheel==0.41.2
```


However, the test waasn't successful due to an unrelated reason, see stacktrace below,

```
Traceback (most recent call last):
  File "/.../scratch/test_scratch/test_reproduce.py", line 41, in test_reproduce
    snowflake_backend: SnowflakeBackend = ibis_snowflake_backend(
  File "/.../scratch/test_scratch/test_reproduce.py", line 18, in ibis_snowflake_backend
    connection: SnowflakeBackend = ibis.snowflake.connect(
  File "/.../opt/anaconda3/envs/scratch_env/lib/python3.10/site-packages/ibis/__init__.py", line 108, in connect
    return backend.connect(*args, **kwargs)
  File "/.../opt/anaconda3/envs/scratch_env/lib/python3.10/site-packages/ibis/backends/__init__.py", line 764, in connect
    new_backend.reconnect()
  File "/.../opt/anaconda3/envs/scratch_env/lib/python3.10/site-packages/ibis/backends/__init__.py", line 778, in reconnect
    self.do_connect(*self._con_args, **self._con_kwargs)
  File "/.../opt/anaconda3/envs/scratch_env/lib/python3.10/site-packages/ibis/backends/snowflake/__init__.py", line 246, in do_connect
    con = sc.connect(**connect_args)
UnboundLocalError: local variable 'sc' referenced before assignment
```

I investigated this issue further and noticed, a conditional `import` but unconditional access (in `ibis/backends/snowflake/__init__.py`),


```
if vparse(
	importlib.metadata.version("snowflake-connector-python")
) >= vparse("3.3.0"):
	warnings.filterwarnings(
		"ignore",
		message="You have an incompatible version of 'pyarrow' installed",
		category=UserWarning,
	)
	import snowflake.connector as sc

...
...
...


con = sc.connect(**connect_args)
```

(Notice in the `pipdeptree` results the test uses `snowflake-connector-python==3.0.3`, so the if condition is not exercised)

I can confirm this code issue is still in `main` branch - see [here](https://github.com/ibis-project/ibis/blob/69d6c7386e6c79859037629dd247e2e07ec5e67c/ibis/backends/snowflake/__init__.py#L214-L246).

Happy to offer help fixing or re-test when the fix is available.

_Originally posted by @yazad3 in https://github.com/ibis-project/ibis/issues/8738#issuecomment-2015080941_
            


Discussion:

Resolving patch:
diff --git a/ibis/backends/snowflake/__init__.py b/ibis/backends/snowflake/__init__.py
--- a/ibis/backends/snowflake/__init__.py
+++ b/ibis/backends/snowflake/__init__.py
@@ -3,7 +3,6 @@
 import contextlib
 import functools
 import glob
-import importlib
 import inspect
 import itertools
 import json
@@ -24,7 +23,6 @@
 import pyarrow_hotfix  # noqa: F401
 import sqlglot as sg
 import sqlglot.expressions as sge
-from packaging.version import parse as vparse
 
 import ibis
 import ibis.common.exceptions as com
@@ -211,16 +209,7 @@ def do_connect(self, create_object_udfs: bool = True, **kwargs: Any):
             Additional arguments passed to the URL constructor.
 
         """
-        with warnings.catch_warnings():
-            if vparse(
-                importlib.metadata.version("snowflake-connector-python")
-            ) >= vparse("3.3.0"):
-                warnings.filterwarnings(
-                    "ignore",
-                    message="You have an incompatible version of 'pyarrow' installed",
-                    category=UserWarning,
-                )
-                import snowflake.connector as sc
+        import snowflake.connector as sc
 
         connect_args = kwargs.copy()
         session_parameters = connect_args.pop("session_parameters", {})


Test patch: