From bc06d1f3ddc0e7a40ae55f5cd9db092cf81e3170 Mon Sep 17 00:00:00 2001 From: Rajan-vish Date: Mon, 9 Nov 2026 16:07:10 +0530 Subject: [PATCH] Send informational dependency messages to stderr (#451) --- CHANGES.rst | 3 +++ src/sqlacodegen/cli.py | 8 +++++--- tests/test_cli.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 40e0b3cf..998c8d0f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,9 @@ Version history **UNRELEASED** +- Printed informational dependency messages to ``stderr`` instead of ``stdout`` + to prevent corrupting generated model code when stdout is redirected + (`#451 `_; PR by @Rajan-vish) - **BACKWARD INCOMPATIBLE** The SQLModel generator no longer emits ``t_`` ``Table`` variables for many-to-many association tables that have a primary key, and their relationships no longer pass ``secondary`` via ``sa_relationship_kwargs``. diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index 8f37373f..e3304d3a 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -101,13 +101,15 @@ def main() -> None: return if citext: - print(f"Using sqlalchemy-citext {version('sqlalchemy-citext')}") + print( + f"Using sqlalchemy-citext {version('sqlalchemy-citext')}", file=sys.stderr + ) if geoalchemy2: - print(f"Using geoalchemy2 {version('geoalchemy2')}") + print(f"Using geoalchemy2 {version('geoalchemy2')}", file=sys.stderr) if pgvector: - print(f"Using pgvector {version('pgvector')}") + print(f"Using pgvector {version('pgvector')}", file=sys.stderr) # Use reflection to fill in the metadata engine_args = _parse_engine_args(args.engine_arg) diff --git a/tests/test_cli.py b/tests/test_cli.py index d4410ff7..c7edbf87 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -221,3 +221,19 @@ def test_main() -> None: check=True, ) assert completed.stdout.decode().strip() == expected_version + + +def test_cli_stdout_clean_from_informational_messages(db_path: Path) -> None: + completed = subprocess.run( + [ + sys.executable, + "-m", + "sqlacodegen", + f"sqlite:///{db_path}", + ], + capture_output=True, + text=True, + check=True, + ) + assert "Using " not in completed.stdout + assert "from sqlalchemy import" in completed.stdout