FIX: Type annotation for executemany seq_of_parameters to accept Mapping#525
FIX: Type annotation for executemany seq_of_parameters to accept Mapping#525bewithgaurav wants to merge 1 commit intomainfrom
Conversation
The type annotation for seq_of_parameters in Cursor.executemany did not accept List[Mapping[str, Any]], which is valid for pyformat-style parameters. Updated both cursor.py and mssql_python.pyi to use Union[List[Sequence[Any]], List[Mapping[str, Any]]]. Fixes #469
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates the Cursor.executemany type annotations/stubs to accept named-parameter batches (mappings) in addition to positional-parameter batches (sequences), aligning the public API typing with broader parameter formats (GitHub Issue #469).
Changes:
- Expanded
executemanyparameter typing to allowMapping[str, Any]batches alongsideSequence[Any]batches. - Added
Mappingimport to both runtime implementation and.pyistub to support the new type hints.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| mssql_python/mssql_python.pyi | Updates the public type stub for Cursor.executemany to accept mapping-based parameter batches. |
| mssql_python/cursor.py | Updates the runtime type annotation for Cursor.executemany to accept mapping-based parameter batches. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| def executemany( # pylint: disable=too-many-locals,too-many-branches,too-many-statements | ||
| self, operation: str, seq_of_parameters: List[Sequence[Any]] | ||
| self, operation: str, seq_of_parameters: Union[List[Sequence[Any]], List[Mapping[str, Any]]] |
There was a problem hiding this comment.
The seq_of_parameters annotation is unnecessarily restrictive by requiring a List[...]. In practice (and per common DB-API usage), callers often pass tuples/other iterables. Consider widening the annotation to Sequence[...] or Iterable[...] (e.g., Iterable[Sequence[Any]] | Iterable[Mapping[str, Any]], or Iterable[Sequence[Any] | Mapping[str, Any]] if mixed batches are supported) so type checking matches the accepted runtime inputs.
| reset_cursor: bool = True, | ||
| ) -> "Cursor": ... | ||
| def executemany(self, operation: str, seq_of_parameters: List[Sequence[Any]]) -> None: ... | ||
| def executemany(self, operation: str, seq_of_parameters: Union[List[Sequence[Any]], List[Mapping[str, Any]]]) -> None: ... |
There was a problem hiding this comment.
Same as the runtime annotation: the stub currently forces List[...], which rejects valid inputs like tuples or other sequence/iterable types in type checkers. Please widen the stub to Sequence[...] or Iterable[...] to reflect typical executemany usage and keep typing ergonomic for callers.
|
|
||
| def executemany( # pylint: disable=too-many-locals,too-many-branches,too-many-statements | ||
| self, operation: str, seq_of_parameters: List[Sequence[Any]] | ||
| self, operation: str, seq_of_parameters: Union[List[Sequence[Any]], List[Mapping[str, Any]]] |
There was a problem hiding this comment.
The expanded executemany type is getting harder to read and now must be duplicated in both cursor.py and mssql_python.pyi. To reduce repetition and make future changes less error-prone, consider introducing a well-named type alias (e.g., ExecutemanyParams) and using it in the signature (and in the stub if feasible).
Work Item / Issue Reference
Summary
This pull request updates the
executemanymethod in theCursorclass to support both positional and named parameter sequences, improving compatibility with different parameter formats. The changes also update type hints and type stubs to reflect this enhancement.Type hint and API improvements:
executemanymethod incursor.pyto accept both lists of sequences (for positional parameters) and lists of mappings (for named parameters), allowing for greater flexibility in how parameters are passed.Mappingto the imports fromtypingin bothcursor.pyandmssql_python.pyito support the new type hints. [1] [2]executemanymethod inmssql_python.pyito match the new accepted parameter types, ensuring type checkers and IDEs recognize the expanded API.