Skip to content

typing: use mypy to verify runtime behaviour matches type stubs #338

@AllSeeingEyeTolledEweSew

Description

I'm using apsw from pypi (thanks for that!)

I found some issues with the type stubs.

Some valid code that fails type-checking:

import apsw
conn = apsw.Connection(":memory:")
cur = conn.cursor()
cur.execute("CREATE TABLE t (x INT PRIMARY KEY)")
cur.execute("SELECT * FROM t")
for row in cur:
    pass
row = cur.execute("SELECT * FROM t").fetchone()
$ pip freeze | grep -E '(mypy|apsw)=='
apsw==3.38.5.post1
mypy==0.961
$ mypy test.py
test.py:6: error: "Cursor" has no attribute "__iter__" (not iterable)
test.py:8: error: "Iterator[Any]" has no attribute "fetchone"
Found 2 errors in 1 file (checked 1 source file)
  • Cursor.execute() returns Iterator
    • It should return Cursor. (Actually it should return typing.Self, but that's new in 3.11 beta, and there isn't a nice way to declare this in a backwards-compatible way yet)
  • Cursor doesn't expose __iter__()/__next__() in type stubs.
    • The iterator protocol methods should be exposed in the type stubs, as well as any other methods that are part of the public API.
    • Cursor.__iter__() should return Cursor (or Self)
    • Cursor.__next__() should return Any, or perhaps a narrower row type (IMO the row type should be narrower than Any, but I'll file a separate issue about this)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions