rogerbinns / apsw

Another Python SQLite wrapper
https://rogerbinns.github.io/apsw/
Other
715 stars 96 forks source link

Unknown apsw attribute 'SQLiteValues' #507

Closed matrey closed 5 months ago

matrey commented 5 months ago

Testing the code snippet at https://rogerbinns.github.io/apsw/example.html#tracing-returned-rows

Python 3.12.1 (main, Dec 19 2023, 03:09:45) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import apsw
>>> def row_tracer(cursor: apsw.Cursor, row: apsw.SQLiteValues) -> apsw.SQLiteValues:
...     """Called with each row of results before they are handed off.  You can return None to
...     cause the row to be skipped or a different set of values to return"""
...     print("Row:", row)
...     return row
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Unknown apsw attribute 'SQLiteValues'
rogerbinns commented 5 months ago

To use type annotations you have to do what it says at the top of the file, specifically:

 from __future__ import annotations
matrey commented 5 months ago

Ah yeah indeed, sorry. I found the type definition in __init__ which is why I thought it might be a bug / regression. But it is __init__.pyi, not __init__.py

rogerbinns commented 5 months ago

Apologies for getting mixed up in this. I've updated the comments to make it clear that future import annotations is mandatory. If you make only that change then everything will be fine and all tools that deal with typing including mypy and language servers, vscode etc will do the right thing.

The import postpones evaluation of annotations (PEP 563). As you noticed SQLiteValues and several other types aren't real, and are there to help type checkers. The core of APSW is implemented in C which is why they aren't real. They are intentionally provided in the corresponding .pyi file - known as type stubs - PEP 561.

All this typing stuff is fairly messy behind the scenes. But you should experience a convenient and smooth experience using them, and if not then that is a bug.