From 4f1236746f3266526b0d35c0d8962ec8eea4b0d1 Mon Sep 17 00:00:00 2001 From: Cody Fincher Date: Sun, 22 Sep 2024 13:53:54 -0500 Subject: [PATCH] feat: adds additional coverage --- litestar/contrib/sqlalchemy/base.py | 43 ++++--- litestar/contrib/sqlalchemy/dto.py | 13 ++- .../contrib/sqlalchemy/plugins/init/plugin.py | 5 +- .../sqlalchemy/plugins/serialization.py | 4 +- .../contrib/sqlalchemy/repository/_async.py | 2 +- .../contrib/sqlalchemy/repository/_sync.py | 2 +- tests/unit/test_contrib/test_sqlalchemy.py | 108 ++++++++++++++++++ 7 files changed, 149 insertions(+), 28 deletions(-) diff --git a/litestar/contrib/sqlalchemy/base.py b/litestar/contrib/sqlalchemy/base.py index 2ad37ed4e2..9b1fd316c3 100644 --- a/litestar/contrib/sqlalchemy/base.py +++ b/litestar/contrib/sqlalchemy/base.py @@ -1,6 +1,7 @@ +# ruff: noqa: TCH004, F401 +# pyright: reportUnusedImport=false """Application ORM configuration.""" -# ruff: noqa: TCH004, F401 from __future__ import annotations from typing import TYPE_CHECKING @@ -24,36 +25,46 @@ def __getattr__(attr_name: str) -> object: - from advanced_alchemy import base # pyright: ignore[reportMissingImports,reportUnusedImport] - - try: - # v0.6.0+ - from advanced_alchemy._listeners import touch_updated_timestamp # pyright: ignore - except ImportError: - from advanced_alchemy.base import touch_updated_timestamp # type: ignore[no-redef,attr-defined] - if attr_name in __all__: if attr_name == "touch_updated_timestamp": + try: + # v0.6.0+ + from advanced_alchemy._listeners import touch_updated_timestamp # pyright: ignore + except ImportError: + from advanced_alchemy.base import touch_updated_timestamp # type: ignore[no-redef,attr-defined] warn_deprecation( - deprecated_name=f"litestar.contrib.sqlalchemy.{attr_name}", + deprecated_name=f"litestar.contrib.sqlalchemy.base.{attr_name}", version="2.12", kind="import", removal_in="3.0", - info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy' is deprecated, please" + info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.base' is deprecated, please" f"see the 'Advanced Alchemy' documentation for more details on how to use '{attr_name}' instead", ) - value = globals()[attr_name] = touch_updated_timestamp # pyright: ignore[reportUnknownVariableType] + value = globals()[attr_name] = locals()[attr_name] # pyright: ignore[reportUnknownVariableType] return value # pyright: ignore[reportUnknownVariableType] + from advanced_alchemy.base import ( # pyright: ignore[reportMissingImports] + AuditColumns, + BigIntAuditBase, + BigIntBase, + BigIntPrimaryKey, + CommonTableAttributes, + ModelProtocol, + UUIDAuditBase, + UUIDBase, + UUIDPrimaryKey, + create_registry, + orm_registry, + ) + warn_deprecation( - deprecated_name=f"litestar.contrib.sqlalchemy.{attr_name}", + deprecated_name=f"litestar.contrib.sqlalchemy.base.{attr_name}", version="2.12", kind="import", removal_in="3.0", - info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy' is deprecated, please" + info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.base' is deprecated, please" f"import it from 'litestar.plugins.sqlalchemy.base.{attr_name}' instead", ) - - value = globals()[attr_name] = getattr(base, attr_name) + value = globals()[attr_name] = locals()[attr_name] # pyright: ignore[reportUnknownVariableType] return value raise AttributeError(f"module {__name__!r} has no attribute {attr_name!r}") diff --git a/litestar/contrib/sqlalchemy/dto.py b/litestar/contrib/sqlalchemy/dto.py index 5a86ef91f2..a1e75c2322 100644 --- a/litestar/contrib/sqlalchemy/dto.py +++ b/litestar/contrib/sqlalchemy/dto.py @@ -1,6 +1,7 @@ +# ruff: noqa: TCH004, F401 +# pyright: reportUnusedImport=false """SQLAlchemy DTO configuration.""" -# ruff: noqa: TCH004, F401 from __future__ import annotations from typing import TYPE_CHECKING @@ -13,8 +14,8 @@ def __getattr__(attr_name: str) -> object: if attr_name in __all__: from advanced_alchemy.extensions.litestar.dto import ( - SQLAlchemyDTO, # pyright: ignore[reportMissingImports,reportUnusedImport] - SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports,reportUnusedImport] + SQLAlchemyDTO, # pyright: ignore[reportMissingImports] + SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports] ) warn_deprecation( @@ -22,7 +23,7 @@ def __getattr__(attr_name: str) -> object: version="2.12", kind="import", removal_in="3.0", - info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy' is deprecated, please " + info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.dto' is deprecated, please " f"import it from 'litestar.plugins.sqlalchemy.dto' instead", ) value = globals()[attr_name] = locals()[attr_name] @@ -33,6 +34,6 @@ def __getattr__(attr_name: str) -> object: if TYPE_CHECKING: from advanced_alchemy.extensions.litestar.dto import ( - SQLAlchemyDTO, # pyright: ignore[reportMissingImports,reportUnusedImport] - SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports,reportUnusedImport] + SQLAlchemyDTO, # pyright: ignore[reportMissingImports] + SQLAlchemyDTOConfig, # pyright: ignore[reportMissingImports] ) diff --git a/litestar/contrib/sqlalchemy/plugins/init/plugin.py b/litestar/contrib/sqlalchemy/plugins/init/plugin.py index 95b21baeb3..28245e13a4 100644 --- a/litestar/contrib/sqlalchemy/plugins/init/plugin.py +++ b/litestar/contrib/sqlalchemy/plugins/init/plugin.py @@ -1,4 +1,5 @@ -# ruff: noqa: TCH004 +# ruff: noqa: TCH004, F401 +# pyright: reportUnusedImport=false from __future__ import annotations from typing import TYPE_CHECKING @@ -20,7 +21,7 @@ def __getattr__(attr_name: str) -> object: ) from advanced_alchemy.extensions.litestar import SQLAlchemyInitPlugin - value = globals()[attr_name] = SQLAlchemyInitPlugin + value = globals()[attr_name] = locals()[attr_name] return value raise AttributeError(f"module {__name__!r} has no attribute {attr_name!r}") diff --git a/litestar/contrib/sqlalchemy/plugins/serialization.py b/litestar/contrib/sqlalchemy/plugins/serialization.py index 65898fe4ff..7c953d1fd7 100644 --- a/litestar/contrib/sqlalchemy/plugins/serialization.py +++ b/litestar/contrib/sqlalchemy/plugins/serialization.py @@ -11,11 +11,11 @@ def __getattr__(attr_name: str) -> object: if attr_name in __all__: warn_deprecation( - deprecated_name=f"litestar.contrib.sqlalchemy.plugins.{attr_name}", + deprecated_name=f"litestar.contrib.sqlalchemy.plugins.serialization.{attr_name}", version="2.12", kind="import", removal_in="3.0", - info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.plugins' is deprecated, please " + info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.plugins.serialization' is deprecated, please " f"import it from 'litstar.plugins.sqlalchemy' instead", ) from advanced_alchemy.extensions.litestar import SQLAlchemySerializationPlugin diff --git a/litestar/contrib/sqlalchemy/repository/_async.py b/litestar/contrib/sqlalchemy/repository/_async.py index 42b545a10b..89909cce5e 100644 --- a/litestar/contrib/sqlalchemy/repository/_async.py +++ b/litestar/contrib/sqlalchemy/repository/_async.py @@ -19,7 +19,7 @@ def __getattr__(attr_name: str) -> object: version="2.12", kind="import", removal_in="3.0", - info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository' is deprecated, please " + info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository._async' is deprecated, please " f"import it from 'litestar.plugins.sqlalchemy.repository' instead", ) value = globals()[attr_name] = locals()[attr_name] diff --git a/litestar/contrib/sqlalchemy/repository/_sync.py b/litestar/contrib/sqlalchemy/repository/_sync.py index 3e1828d2d6..0b744d4919 100644 --- a/litestar/contrib/sqlalchemy/repository/_sync.py +++ b/litestar/contrib/sqlalchemy/repository/_sync.py @@ -21,7 +21,7 @@ def __getattr__(attr_name: str) -> object: version="2.12", kind="import", removal_in="3.0", - info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository' is deprecated, please " + info=f"importing {attr_name} from 'litestar.contrib.sqlalchemy.repository._sync' is deprecated, please " f"import it from 'litestar.plugins.sqlalchemy.repository' instead", ) value = globals()[attr_name] = locals()[attr_name] diff --git a/tests/unit/test_contrib/test_sqlalchemy.py b/tests/unit/test_contrib/test_sqlalchemy.py index 106e6a241d..64ba6fcdc7 100644 --- a/tests/unit/test_contrib/test_sqlalchemy.py +++ b/tests/unit/test_contrib/test_sqlalchemy.py @@ -249,3 +249,111 @@ def test_deprecated_sqlalchemy_plugins_init_config_asyncio_imports() -> None: match="importing AsyncSessionConfig from 'litestar.contrib.sqlalchemy.plugins.init.config.asyncio' is deprecated", ): from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import AsyncSessionConfig + + +def test_deprecated_sqlalchemy_plugins_init_config_engine_imports() -> None: + purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.engine"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing EngineConfig from 'litestar.contrib.sqlalchemy.plugins.init.config.engine' is deprecated", + ): + from litestar.contrib.sqlalchemy.plugins.init.config.engine import EngineConfig + + +def test_deprecated_sqlalchemy_dto_imports() -> None: + purge_module(["litestar.contrib.sqlalchemy.dto"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing SQLAlchemyDTOConfig from 'litestar.contrib.sqlalchemy.dto' is deprecated", + ): + from litestar.contrib.sqlalchemy.dto import SQLAlchemyDTOConfig + + +def test_deprecated_sqlalchemy_plugins_init_plugin_imports() -> None: + purge_module(["litestar.contrib.sqlalchemy.plugins.init.plugin"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing SQLAlchemyInitPlugin from 'litestar.contrib.sqlalchemy.plugins.init' is deprecated", + ): + from litestar.contrib.sqlalchemy.plugins.init.plugin import SQLAlchemyInitPlugin + + +def test_deprecated_sqlalchemy_plugins_serialization_imports() -> None: + purge_module(["litestar.contrib.sqlalchemy.plugins.serialization"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing SQLAlchemySerializationPlugin from 'litestar.contrib.sqlalchemy.plugins.serialization' is deprecated", + ): + from litestar.contrib.sqlalchemy.plugins.serialization import SQLAlchemySerializationPlugin + + +def test_deprecated_sqlalchemy_repository_async_imports() -> None: + purge_module(["litestar.contrib.sqlalchemy.repository._async"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing SQLAlchemyAsyncRepository from 'litestar.contrib.sqlalchemy.repository._async' is deprecated", + ): + from litestar.contrib.sqlalchemy.repository._async import SQLAlchemyAsyncRepository + + +def test_deprecated_sqlalchemy_repository_sync_imports() -> None: + purge_module(["litestar.contrib.sqlalchemy.repository._sync"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing SQLAlchemySyncRepository from 'litestar.contrib.sqlalchemy.repository._sync' is deprecated", + ): + from litestar.contrib.sqlalchemy.repository._sync import SQLAlchemySyncRepository + + +def test_deprecated_sqlalchemy_base_imports() -> None: + purge_module(["litestar.contrib.sqlalchemy.base"], __file__) + with pytest.warns( + DeprecationWarning, + match="from 'litestar.contrib.sqlalchemy.base' is deprecated", + ): + from litestar.contrib.sqlalchemy.base import ( + AuditColumns, + BigIntAuditBase, + BigIntBase, + BigIntPrimaryKey, + CommonTableAttributes, + ModelProtocol, + UUIDAuditBase, + UUIDBase, + UUIDPrimaryKey, + create_registry, + orm_registry, + touch_updated_timestamp, + ) + + +def test_deprecated_sqlalchemy_plugins_init_config_asyncio_handlers() -> None: + purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.asyncio"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing default_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.asyncio' is deprecated", + ): + from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import default_before_send_handler + + purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.asyncio"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing autocommit_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.asyncio' is deprecated", + ): + from litestar.contrib.sqlalchemy.plugins.init.config.asyncio import autocommit_before_send_handler + + +def test_deprecated_sqlalchemy_plugins_init_config_sync_handlers() -> None: + purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.sync"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing default_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.sync' is deprecated", + ): + from litestar.contrib.sqlalchemy.plugins.init.config.sync import default_before_send_handler + + purge_module(["litestar.contrib.sqlalchemy.plugins.init.config.sync"], __file__) + with pytest.warns( + DeprecationWarning, + match="importing autocommit_before_send_handler from 'litestar.contrib.sqlalchemy.plugins.init.config.sync' is deprecated", + ): + from litestar.contrib.sqlalchemy.plugins.init.config.sync import autocommit_before_send_handler