mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.88k stars 562 forks source link

Add typing definition for Row to be a valid Iterable #1148

Closed baluyotraf closed 1 year ago

baluyotraf commented 1 year ago

Using Row as an iterable in code results in this typing issue. For example:

zip(row, row)

The code above works fine, but it gives the typing error below

Argument of type "Row" cannot be assigned to parameter "__iter2" of type "Iterable[_T2@__new__]" in function "__new__"
  "Row" is incompatible with protocol "Iterable[_T2@__new__]"
    "__iter__" is not present

This can be worked around currently by casting the Row object manually

from typing import cast, Iterable, Any

irow = cast(Iterable[Any], row)

This fix simply adds the __iter__ method in the typing definition.

keitherskine commented 1 year ago

Many thanks for this @baluyotraf . I'm kinda surprised iter() on a Row object runs at all. After all, the underlying function "tp_iter" hasn't actually been coded up. But hey, it works! So yes, adding __iter__ to the Row class to formalize this is a good idea.

baluyotraf commented 1 year ago

Yeah I actually tried to play around it a bit to see if Row returns a more specific iterator. But I only got a generic one so I used it on the type. xD