|
2 | 2 | """ |
3 | 3 |
|
4 | 4 | import io |
5 | | -from typing import Sequence, Mapping, List, Any, Optional, Union, IO |
6 | | - |
| 5 | +from typing import IO, Any, Iterable, Mapping, Optional, Sequence, Tuple, Union |
7 | 6 |
|
8 | 7 | try: |
9 | 8 | from typing import Protocol |
10 | 9 | except ImportError: |
11 | 10 | Protocol = object # type: ignore |
12 | 11 |
|
13 | | -RowType = Sequence[Any] |
| 12 | + |
| 13 | +ExecuteParams = Union[Sequence[Any], Mapping[str, Any]] |
| 14 | + |
| 15 | + |
| 16 | +class DictRow(Protocol): |
| 17 | + """Allow both key and index-based access. |
| 18 | +
|
| 19 | + Both Psycopg2 DictRow and PL/Python rows support this. |
| 20 | + """ |
| 21 | + def keys(self) -> Iterable[str]: ... |
| 22 | + def values(self) -> Iterable[Any]: ... |
| 23 | + def items(self) -> Iterable[Tuple[str, Any]]: ... |
| 24 | + def __getitem__(self, key: Union[str, int]) -> Any: ... |
| 25 | + def __iter__(self) -> Iterable[str]: ... |
| 26 | + def __len__(self) -> int: ... |
| 27 | + def __contains__(self, key: str) -> bool: ... |
14 | 28 |
|
15 | 29 |
|
16 | 30 | class Cursor(Protocol): |
17 | | - def execute(self, sql: str, params: Optional[Union[Sequence[Any], Mapping[str, Any]]] = None) -> None: ... |
18 | | - def fetchall(self) -> List[RowType]: ... |
19 | | - def fetchone(self) -> RowType: ... |
| 31 | + def execute(self, sql: str, params: Optional[ExecuteParams] = None) -> None: ... |
| 32 | + def fetchall(self) -> Sequence[DictRow]: ... |
| 33 | + def fetchone(self) -> DictRow: ... |
20 | 34 | def copy_from(self, buf: IO[str], hdr: str) -> None: ... |
21 | 35 | def copy_expert(self, sql: str, f: Union[IO[str], io.TextIOBase]) -> None: ... |
22 | 36 |
|
23 | 37 |
|
24 | 38 | class Connection(Protocol): |
25 | 39 | def cursor(self) -> Cursor: ... |
| 40 | + def rollback(self) -> None: ... |
| 41 | + def commit(self) -> None: ... |
26 | 42 |
|
27 | 43 |
|
28 | 44 | class Runnable(Protocol): |
|
0 commit comments