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

SNOW-680197: cursor.execute can return None which requires checking

Open tekumara opened this issue 3 years ago • 5 comments

What is the current behavior?

SnowflakeCursor execute returns SnowflakeCursor | None.

This means every excecute needs to be checked for None. eg:

    c = cur.execute("select * from mytable")

    if c:
        results = c.fetchone()

If the check isn't performed then pyright/vscode will error with

 "fetchone" is not a known member of "None" (reportOptionalMemberAccess)

Or the application will fail at runtime.

What is the desired behavior?

cursor.execute does not return None for any cases, therefore avoiding the need for a None check.

How would this improve snowflake-connector-python?

This would enable the more fluent syntax without the need for a check:

cur.execute("select * from mytable").fetchone()

References, Other Background

From the pyright docs:

reportOptionalMemberAccess [boolean or string, optional]: Generate or suppress diagnostics for an attempt to access a member of a variable with an Optional type. The default value for this setting is 'error'.

tekumara avatar Oct 18 '22 22:10 tekumara

According to the function documentation, None does have legit meaning, so I'm not sure if removing it is the correct behavior.

https://github.com/snowflakedb/snowflake-connector-python/blob/98677d56a6031b8bafb2765a1a2d2976397b0308/src/snowflake/connector/cursor.py#L624-L625

sfc-gh-sfan avatar Oct 18 '22 22:10 sfc-gh-sfan

Yeh None is returned when the caller provides an empty command:

https://github.com/snowflakedb/snowflake-connector-python/blob/98677d56a6031b8bafb2765a1a2d2976397b0308/src/snowflake/connector/cursor.py#L643

Could this be a ValueError exception instead perhaps?

From the python docs:

exception ValueError Raised when an operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.

tekumara avatar Oct 19 '22 09:10 tekumara

Could this be a ValueError exception instead perhaps?

This is going to be a behavior change, but it might not be a bad idea. WDYT? @sfc-gh-mkeller

sfc-gh-sfan avatar Oct 19 '22 16:10 sfc-gh-sfan

This would be pretty neat. Is there any update on this @sfc-gh-sfan?

mhasanbulli avatar Feb 07 '23 02:02 mhasanbulli