]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix typing issue in `MetaData.reflect()` with asyncio.
authorFrancisco Del Roio <francipvb@hotmail.com>
Fri, 5 Apr 2024 16:05:51 +0000 (12:05 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Wed, 10 Apr 2024 19:01:36 +0000 (21:01 +0200)
Fixed typing regression caused by PR :ticket:`11055` in version 2.0.29 that
attempted to add ``ParamSpec`` to the asyncio ``run_sync()`` methods, where
using :meth:`_asyncio.AsyncConnection.run_sync` with
meth:`_schema.MetaData.reflect` would fail on mypy due to a bug.
See https://github.com/python/mypy/issues/17093 for details.
Pull request courtesy of Francisco R. Del Roio

Fixes: #11200
Closes: #11201
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/11201
Pull-request-sha: 49e10e0d2a7bbadf471212033e25b7616b99c092

Change-Id: Ie2ebaebd1bc1ee1b865b78561cb6cb8937e85eca

doc/build/changelog/unreleased_20/11200.rst [new file with mode: 0644]
lib/sqlalchemy/sql/schema.py
test/typing/plain_files/ext/asyncio/engines.py

diff --git a/doc/build/changelog/unreleased_20/11200.rst b/doc/build/changelog/unreleased_20/11200.rst
new file mode 100644 (file)
index 0000000..61ab650
--- /dev/null
@@ -0,0 +1,10 @@
+.. change::
+    :tags: bug, typing, regression
+    :tickets: 11200
+
+    Fixed typing regression caused by PR :ticket:`11055` in version 2.0.29 that
+    attempted to add ``ParamSpec`` to the asyncio ``run_sync()`` methods, where
+    using :meth:`_asyncio.AsyncConnection.run_sync` with
+    :meth:`_schema.MetaData.reflect` would fail on mypy due to a bug.
+    See https://github.com/python/mypy/issues/17093 for details.
+    Pull request courtesy of Francisco R. Del Roio
index 8436aac434185addce0b6afc0dc6758cac788d0f..0ee69df44faf9cf1acb46737b05bdb2ce59ab0e5 100644 (file)
@@ -5687,6 +5687,38 @@ class MetaData(HasSchemaAttr):
             sorted(self.tables.values(), key=lambda t: t.key)  # type: ignore
         )
 
+    # overload needed to work around mypy this mypy
+    # https://github.com/python/mypy/issues/17093
+    @overload
+    def reflect(
+        self,
+        bind: Engine,
+        schema: Optional[str] = ...,
+        views: bool = ...,
+        only: Union[
+            _typing_Sequence[str], Callable[[str, MetaData], bool], None
+        ] = ...,
+        extend_existing: bool = ...,
+        autoload_replace: bool = ...,
+        resolve_fks: bool = ...,
+        **dialect_kwargs: Any,
+    ) -> None: ...
+
+    @overload
+    def reflect(
+        self,
+        bind: Connection,
+        schema: Optional[str] = ...,
+        views: bool = ...,
+        only: Union[
+            _typing_Sequence[str], Callable[[str, MetaData], bool], None
+        ] = ...,
+        extend_existing: bool = ...,
+        autoload_replace: bool = ...,
+        resolve_fks: bool = ...,
+        **dialect_kwargs: Any,
+    ) -> None: ...
+
     @util.preload_module("sqlalchemy.engine.reflection")
     def reflect(
         self,
index df4b0a0f645aef26cebf647db94bd06c6ab9bfe8..7af764ecd8afe61fdbc5cc875e043026d6bcdf40 100644 (file)
@@ -1,6 +1,8 @@
 from typing import Any
 
 from sqlalchemy import Connection
+from sqlalchemy import Enum
+from sqlalchemy import MetaData
 from sqlalchemy import select
 from sqlalchemy import text
 from sqlalchemy.ext.asyncio import create_async_engine
@@ -71,3 +73,25 @@ async def asyncio() -> None:
     ce.statement
     cc = select(1).compile(conn)
     cc.statement
+
+    async with e.connect() as conn:
+        metadata = MetaData()
+
+        await conn.run_sync(metadata.create_all)
+        await conn.run_sync(metadata.reflect)
+        await conn.run_sync(metadata.drop_all)
+
+        # Just to avoid creating new constructs manually:
+        for _, table in metadata.tables.items():
+            await conn.run_sync(table.create)
+            await conn.run_sync(table.drop)
+
+            # Indexes:
+            for index in table.indexes:
+                await conn.run_sync(index.create)
+                await conn.run_sync(index.drop)
+
+        # Test for enum types:
+        enum = Enum("a", "b")
+        await conn.run_sync(enum.create)
+        await conn.run_sync(enum.drop)