[sqlite3] Fix type of row_factory for Connection and Cursor#15316
[sqlite3] Fix type of row_factory for Connection and Cursor#15316srittau merged 2 commits intopython:mainfrom
Conversation
`row_factory`, if not `None`, is a callable that receives a Cursor instance and a tuple of row values. `sqlite3.Row` itself is such a callable, but claiming that it will be passed as an argument to the row_factory function is wrong, as it does not match documented and actual behavior. Although not necessary (since it's covered by the `Callable[...]`), the corrected type hint leaves `type[Row]` in place as an explicit option to make this connection immediately obvious. See https://docs.python.org/3.14/library/sqlite3.html#sqlite3.Cursor.row_factory
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
|
Sorry, I made a manual edit in Github instead of pushing my local changes and introduced a syntax error. 🙄 A fix is in a follow-up commit. I don't want to unilaterally change git history, so please feel free to squash that. I'd also be happy to force-push a squashed version myself if you prefer. |
Please don't look at the commit history of some of my PRs ...
We actually prefer not force-pushing as it makes reviewing changes easier. All PRs get squash merged when ready. |
Fixes #15308.
row_factory, if notNone, is a callable that receives a Cursor instance and a tuple of row values.sqlite3.Rowitself is such a callable, but claiming that it will be passed as an argument to the row_factory function is wrong, as it does not match documented and actual behavior.Although not necessary (since it's covered by the
Callable[...]), the corrected type hint leavestype[Row]in place as an explicit option to make this connection immediately obvious.See
https://docs.python.org/3.14/library/sqlite3.html#sqlite3.Cursor.row_factory
I'm not sure about PR etiquette on this project, so this is my best effort after reading CONTRIBUTING.md and the contents of
tests/. Happy to make any suggested corrections.Tested with
pre-commit run --all-filesandpython3 tests/runtests.py stdlib/sqlite3. (The latter produces unrelatedstubtesterrors for me, which are the same when running the command on the unmodifiedmainbranch.)Manual test
Use the following Python snippet with valid code that produces a type checker false positive with the faulty stubs, but works with the proposed fix:
Output of the script itself shows a tuple is being passed:
Unpatched false positive:
With fix: