Skip to content

Commit 3e21188

Browse files
fix(alembic): narrow enum-import guard to ModuleNotFoundError
Catching ImportError also swallowed genuine import-time failures (e.g. version mismatches), silently disabling enum autogenerate support even when alembic-postgresql-enum is installed. Catch ModuleNotFoundError so a missing package is handled quietly while real errors surface. Addresses review feedback on litestar-org#774.
1 parent 66925ef commit 3e21188

3 files changed

Lines changed: 3 additions & 3 deletions

File tree

advanced_alchemy/alembic/templates/asyncio/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# required autogenerate comparators as a side effect. Install with the
1515
# ``advanced-alchemy[postgresql]`` extra to enable.
1616
import alembic_postgresql_enum # type: ignore[import-not-found,unused-ignore] # pyright: ignore[reportMissingImports] # noqa: F401
17-
except ImportError:
17+
except ModuleNotFoundError:
1818
pass
1919

2020
if TYPE_CHECKING:

advanced_alchemy/alembic/templates/sync/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# required autogenerate comparators as a side effect. Install with the
1313
# ``advanced-alchemy[postgresql]`` extra to enable.
1414
import alembic_postgresql_enum # type: ignore[import-not-found,unused-ignore] # pyright: ignore[reportMissingImports] # noqa: F401
15-
except ImportError:
15+
except ModuleNotFoundError:
1616
pass
1717

1818
if TYPE_CHECKING:

tests/integration/test_alembic_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ async def test_alembic_init_renders_postgres_enum_import(
246246
alembic_commands.init(directory=f"{tmp_project_dir}/migrations/")
247247
env_contents = await async_(Path(f"{tmp_project_dir}/migrations/env.py").read_text)()
248248
assert "import alembic_postgresql_enum" in env_contents
249-
assert "except ImportError:" in env_contents
249+
assert "except ModuleNotFoundError:" in env_contents
250250

251251

252252
async def test_alembic_init_already(alembic_commands: commands.AlembicCommands, tmp_project_dir: Path) -> None:

0 commit comments

Comments
 (0)