Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
recrsn opened this issue Jul 24, 2023 · 2 comments
Assignees
Labels
bug status-triage_done Initial triage done, will be further handled by the driver team

Comments

@recrsn
Copy link

recrsn commented Jul 24, 2023

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

@github-actions github-actions bot changed the title Type hints for execute and executemany in DictCursor is incorrect SNOW-874658: Type hints for execute and executemany in DictCursor is incorrect Jul 24, 2023
@lokucrazy
Copy link

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?

@sfc-gh-dszmolka sfc-gh-dszmolka added status-triage_done Initial triage done, will be further handled by the driver team and removed needs triage labels Mar 8, 2024
@sfc-gh-dszmolka
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug status-triage_done Initial triage done, will be further handled by the driver team
Projects
None yet
Development

No branches or pull requests

4 participants