types.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Iterator, List, Mapping, Optional, Sequence, Tuple, Union
  15. from typing_extensions import Protocol
  16. """
  17. Some very basic protocol definitions for the DB-API2 classes specified in PEP-249
  18. """
  19. _Parameters = Union[Sequence[Any], Mapping[str, Any]]
  20. class Cursor(Protocol):
  21. def execute(self, sql: str, parameters: _Parameters = ...) -> Any:
  22. ...
  23. def executemany(self, sql: str, parameters: Sequence[_Parameters]) -> Any:
  24. ...
  25. def fetchone(self) -> Optional[Tuple]:
  26. ...
  27. def fetchmany(self, size: Optional[int] = ...) -> List[Tuple]:
  28. ...
  29. def fetchall(self) -> List[Tuple]:
  30. ...
  31. @property
  32. def description(
  33. self,
  34. ) -> Optional[
  35. Sequence[
  36. # Note that this is an approximate typing based on sqlite3 and other
  37. # drivers, and may not be entirely accurate.
  38. Tuple[
  39. str,
  40. Optional[Any],
  41. Optional[int],
  42. Optional[int],
  43. Optional[int],
  44. Optional[int],
  45. Optional[int],
  46. ]
  47. ]
  48. ]:
  49. ...
  50. @property
  51. def rowcount(self) -> int:
  52. return 0
  53. def __iter__(self) -> Iterator[Tuple]:
  54. ...
  55. def close(self) -> None:
  56. ...
  57. class Connection(Protocol):
  58. def cursor(self) -> Cursor:
  59. ...
  60. def close(self) -> None:
  61. ...
  62. def commit(self) -> None:
  63. ...
  64. def rollback(self) -> None:
  65. ...
  66. def __enter__(self) -> "Connection":
  67. ...
  68. def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]:
  69. ...