From: Francisco Del Roio Date: Fri, 5 Apr 2024 16:05:51 +0000 (-0400) Subject: Fix typing issue in `MetaData.reflect()` with asyncio. X-Git-Tag: rel_2_0_30~29^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ceb465d09e60c95f269aac6f740ef0ecaae368ed;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix typing issue in `MetaData.reflect()` with asyncio. 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) --- diff --git a/doc/build/changelog/unreleased_20/11200.rst b/doc/build/changelog/unreleased_20/11200.rst new file mode 100644 index 0000000000..61ab6506b1 --- /dev/null +++ b/doc/build/changelog/unreleased_20/11200.rst @@ -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 diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 6d5f941786..aa359fdbbd 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -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, diff --git a/test/typing/plain_files/ext/asyncio/engines.py b/test/typing/plain_files/ext/asyncio/engines.py index 1f7843082a..7c93466e0b 100644 --- a/test/typing/plain_files/ext/asyncio/engines.py +++ b/test/typing/plain_files/ext/asyncio/engines.py @@ -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)