snowflake-connector-python icon indicating copy to clipboard operation
snowflake-connector-python copied to clipboard

SNOW-874658: Type hints for execute and executemany in DictCursor is incorrect

Open recrsn opened this issue 2 years ago • 2 comments

Python version

Python 3.9.17

Operating system and processor architecture

macOS-13.4.1-arm64-arm-64bit

Installed packages

asn1crypto==1.5.1
certifi==2023.5.7
cffi==1.15.1
charset-normalizer==3.1.0
cryptography==40.0.2
filelock==3.12.0
idna==3.4
oscrypto==1.3.0
packaging==23.1
pycparser==2.21
pycryptodomex==3.18.0
PyJWT==2.7.0
pyOpenSSL==23.1.1
pytz==2023.3
requests==2.31.0
snowflake-connector-python==3.0.4
sortedcontainers==2.4.0
typing_extensions==4.6.0
urllib3==1.26.16

What did you do?

# Write some code using execute or executemany and run mypy
#
# Like 


connection = snowflake.connector.connect(...)
with connection.cursor(DictCursor) as cur: # issue here too, as cursor always returns a SnowflakeCursor, this should be generified
    cur.execute("SELECT c1,c2 FROM example")
    results = cur.fetchall()
    for row in results:
       c1 = row["C1"] # error here, as SnowflakeCursor marks return type as list


# The issue stems from DictCursor not defining overrides correctly.

What did you expect to see?

No mypy errors

Can you set logging to DEBUG and collect the logs?

No response

recrsn avatar Jul 24 '23 07:07 recrsn

howdy, having a similar problem with mypy and snowflake-connector-python, ended up doing this redundant casting to appease mypy

cursor = connection.cursor(DictCursor)
cursor.execute(SQL)
result = cursor.fetchone()
r_dict = dict(result) if result and isinstance(result, dict) else None
if r_dict:
        return r_dict["Col"]

this was the error messages I would get with mypy

error: No overload variant of "__getitem__" of "tuple" matches argument type "str"  [call-overload]
note: Possible overload variants:
note:     def __getitem__(self, SupportsIndex, /) -> Any
note:     def __getitem__(self, slice, /) -> tuple[Any, ...]
error: Value of type "dict[Any, Any] | tuple[Any, ...] | None" is not indexable  [index]

So yeah the issue for me is that we can't specify with DictCursor that the internal _result field is going to be a dictionary. Is this getting worked on?

lokucrazy avatar Mar 01 '24 20:03 lokucrazy

hi and thank you for raising this ! i understand this is still an issue and definitely needs to be addressed somehow. we'll take a look and I'll keep this thread posted with new information.

sfc-gh-dszmolka avatar Mar 08 '24 15:03 sfc-gh-dszmolka