Skip to content

Commit e4027bd

Browse files
committed
Fix mypy in cursor.py
1 parent 842811b commit e4027bd

File tree

6 files changed

+147
-74
lines changed

6 files changed

+147
-74
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ repos:
7474
(?x)^src/snowflake/connector/(
7575
constants
7676
| compat
77+
| cursor
7778
| dbapi
7879
| description
7980
| errorcode

src/snowflake/connector/bind_upload_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .errors import BindUploadError, Error
1515

1616
if TYPE_CHECKING: # pragma: no cover
17-
from .cursor import SnowflakeCursor
17+
from .cursor import SnowflakeCursorBase
1818

1919
logger = getLogger(__name__)
2020

@@ -23,7 +23,7 @@ class BindUploadAgent:
2323

2424
def __init__(
2525
self,
26-
cursor: SnowflakeCursor,
26+
cursor: SnowflakeCursorBase,
2727
rows: list[bytes],
2828
stream_buffer_size: int = 1024 * 1024 * 10,
2929
) -> None:

src/snowflake/connector/connection.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@
109109
ER_NO_USER,
110110
ER_NOT_IMPLICITY_SNOWFLAKE_DATATYPE,
111111
)
112-
from .errors import DatabaseError, Error, OperationalError, ProgrammingError
112+
from .errors import (
113+
DatabaseError,
114+
Error,
115+
ErrorHandler,
116+
OperationalError,
117+
ProgrammingError,
118+
)
113119
from .log_configuration import EasyLoggingConfigPython
114120
from .network import (
115121
DEFAULT_AUTHENTICATOR,
@@ -500,6 +506,12 @@ class SnowflakeConnection:
500506

501507
OCSP_ENV_LOCK = Lock()
502508

509+
# Tell mypy these fields exist.
510+
# TODO: Replace the dynamic setattr() with static methods
511+
_interpolate_empty_sequences: bool
512+
_reraise_error_in_file_transfer_work_function: bool
513+
_reuse_results: bool
514+
503515
def __init__(
504516
self,
505517
connection_name: str | None = None,
@@ -780,12 +792,12 @@ def application(self) -> str:
780792
return self._application
781793

782794
@property
783-
def errorhandler(self) -> Callable: # TODO: callable args
795+
def errorhandler(self) -> ErrorHandler:
784796
return self._errorhandler
785797

786798
@errorhandler.setter
787-
# Note: Callable doesn't implement operator|
788-
def errorhandler(self, value: Callable | None) -> None:
799+
def errorhandler(self, value: ErrorHandler | None) -> None:
800+
# TODO: Why is value `ErrorHandler | None` if it always errors on None?
789801
if value is None:
790802
raise ProgrammingError("None errorhandler is specified")
791803
self._errorhandler = value
@@ -1096,9 +1108,9 @@ def execute_string(
10961108
sql_text: str,
10971109
remove_comments: bool = False,
10981110
return_cursors: bool = True,
1099-
cursor_class: SnowflakeCursor = SnowflakeCursor,
1111+
cursor_class: SnowflakeCursorBase = SnowflakeCursor,
11001112
**kwargs,
1101-
) -> Iterable[SnowflakeCursor]:
1113+
) -> Iterable[SnowflakeCursorBase]:
11021114
"""Executes a SQL text including multiple statements. This is a non-standard convenience method."""
11031115
stream = StringIO(sql_text)
11041116
stream_generator = self.execute_stream(
@@ -1111,9 +1123,9 @@ def execute_stream(
11111123
self,
11121124
stream: StringIO,
11131125
remove_comments: bool = False,
1114-
cursor_class: SnowflakeCursor = SnowflakeCursor,
1126+
cursor_class: SnowflakeCursorBase = SnowflakeCursor,
11151127
**kwargs,
1116-
) -> Generator[SnowflakeCursor]:
1128+
) -> Generator[SnowflakeCursorBase]:
11171129
"""Executes a stream of SQL statements. This is a non-standard convenient method."""
11181130
split_statements_list = split_statements(
11191131
stream, remove_comments=remove_comments
@@ -1830,7 +1842,6 @@ def _write_params_to_byte_rows(
18301842
18311843
Args:
18321844
params: Binding parameters to bulk array insertion query with qmark/numeric format.
1833-
cursor: SnowflakeCursor.
18341845
18351846
Returns:
18361847
List of bytes string corresponding to rows
@@ -1847,7 +1858,7 @@ def _write_params_to_byte_rows(
18471858

18481859
def _get_snowflake_type_and_binding(
18491860
self,
1850-
cursor: SnowflakeCursor | None,
1861+
cursor: SnowflakeCursorBase | None,
18511862
v: tuple[str, Any] | Any,
18521863
) -> TypeAndBinding:
18531864
if isinstance(v, tuple):
@@ -1888,7 +1899,7 @@ def _get_snowflake_type_and_binding(
18881899
def _process_params_qmarks(
18891900
self,
18901901
params: Sequence | None,
1891-
cursor: SnowflakeCursor | None = None,
1902+
cursor: SnowflakeCursorBase | None = None,
18921903
) -> dict[str, dict[str, str]] | None:
18931904
if not params:
18941905
return None
@@ -1922,14 +1933,14 @@ def _process_params_qmarks(
19221933
def _process_params_pyformat(
19231934
self,
19241935
params: Any | Sequence[Any] | dict[Any, Any] | None,
1925-
cursor: SnowflakeCursor | None = None,
1936+
cursor: SnowflakeCursorBase | None = None,
19261937
) -> tuple[Any] | dict[str, Any] | None:
19271938
"""Process parameters for client-side parameter binding.
19281939
19291940
Args:
19301941
params: Either a sequence, or a dictionary of parameters, if anything else
19311942
is given then it will be put into a list and processed that way.
1932-
cursor: The SnowflakeCursor used to report errors if necessary.
1943+
cursor: The SnowflakeCursorBase used to report errors if necessary.
19331944
"""
19341945
if params is None:
19351946
if self._interpolate_empty_sequences:
@@ -1961,7 +1972,7 @@ def _process_params_pyformat(
19611972
)
19621973

19631974
def _process_params_dict(
1964-
self, params: dict[Any, Any], cursor: SnowflakeCursor | None = None
1975+
self, params: dict[Any, Any], cursor: SnowflakeCursorBase | None = None
19651976
) -> dict:
19661977
try:
19671978
res = {k: self._process_single_param(v) for k, v in params.items()}

0 commit comments

Comments
 (0)