Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use psycopg2 type stubs #12269

Merged
merged 3 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/12269.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use type stubs for `psycopg2`.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def exec_file(path_segments):
"types-jsonschema>=3.2.0",
"types-opentracing>=2.4.2",
"types-Pillow>=8.3.4",
"types-psycopg2>=2.9.9",
"types-pyOpenSSL>=20.0.7",
"types-PyYAML>=5.4.10",
"types-requests>=2.26.0",
Expand Down
10 changes: 9 additions & 1 deletion synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,15 @@ def execute_values(self, sql: str, *args: Any, fetch: bool = True) -> List[Tuple
from psycopg2.extras import execute_values # type: ignore

return self._do_execute(
lambda *x: execute_values(self.txn, *x, fetch=fetch), sql, *args
# Type ignore: mypy is unhappy because if `x` is a 5-tuple, then there will
# be two values for `fetch`: one given positionally, and another given
# as a keyword argument. We might be able to fix this by
# - propagating the signature of psycopg2.extras.execute_values to this
# function, or
# - changing `*args: Any` to `values: T` for some appropriate T.
lambda *x: execute_values(self.txn, *x, fetch=fetch), # type: ignore[misc]
sql,
*args,
Comment on lines +308 to +316
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (as Patrick suggested in #synapse-dev) that we could change *args and *x to be args and x. But the rest of the source makes a lot of use of args: Any and I wasn't completely convinced that the change was safe or made things clearer.

)

def execute(self, sql: str, *args: Any) -> None:
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/engines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_engine(database_config) -> BaseDatabaseEngine:

if name == "psycopg2":
# Note that psycopg2cffi-compat provides the psycopg2 module on pypy.
import psycopg2 # type: ignore
import psycopg2

return PostgresEngine(psycopg2, database_config)

Expand Down