]> 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:51 +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
(cherry picked from commit 40fc02d93f3f8b4d9ae2f7bf987f5f965a761dd4)

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 6d5f941786aeb22afea954391e02f080bbbc7eab..aa359fdbbd7956ba3c754db94b9afff18c6eab16 100644 (file)
@@ -5616,6 +5616,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 1f7843082a915ba355b487f792d5ff0b12602f28..7c93466e0bf20c121fd46c7c75f1ab0b73607e82 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)