Skip to content

Make psycopg2 import optional in db_utils#123

Merged
tim-band merged 2 commits into
mssqlfrom
mssql-ci-tests
Jul 15, 2026
Merged

Make psycopg2 import optional in db_utils#123
tim-band merged 2 commits into
mssqlfrom
mssql-ci-tests

Conversation

@myyong

@myyong myyong commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • datafaker/db_utils.py imported psycopg2.errors.UndefinedObject unconditionally at module level, but psycopg2-binary is only installed via the optional postgres extra (see pyproject.toml).
  • Any environment that installs only the mssql extra (not postgres) fails to import datafaker at all, which previously broke the standalone tests-mssql.yml workflow before it was replaced.
  • Fix: import UndefinedObject inside a try/except ImportError, falling back to an empty placeholder class. UndefinedObject is psycopg2-specific and can only ever match errors raised by the psycopg2 driver, so the isinstance check at the one call site safely evaluates to False when psycopg2 isn't installed -- no behavior change when it is.

(Note: this PR originally also fixed a gpg//dev/tty failure in tests-mssql.yml, but that workflow was removed upstream in favor of running MSSQL tests directly in tests.yml, which sidesteps the issue. Rebased to drop that now-moot commit.)

Test plan

  • Verified locally that datafaker.db_utils imports successfully with psycopg2 mocked as absent, and that the isinstance check safely returns False
  • CI passes on this branch

Generated with Claude Code

@myyong
myyong marked this pull request as draft July 15, 2026 10:27
@myyong myyong self-assigned this Jul 15, 2026
@myyong myyong changed the title Fix gpg /dev/tty failure in MSSQL ODBC driver install Make psycopg2 import optional in db_utils Jul 15, 2026
psycopg2 is only installed via the "postgres" extra, but db_utils.py
imported it unconditionally, breaking any import of datafaker (and
therefore any test, including MSSQL-only ones) when only the "mssql"
extra is installed. UndefinedObject is psycopg2-specific and can never
match when a different driver raised the error, so fall back to an
empty placeholder class when psycopg2 isn't present.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
pre-commit's isort hook was failing on the mssql branch tip because
these files had imports out of alphabetical order within their group.
Ran `pre-commit run isort --all-files` to match CI exactly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@myyong
myyong marked this pull request as ready for review July 15, 2026 11:00
@myyong
myyong requested a review from tim-band July 15, 2026 11:02
@tim-band
tim-band merged commit b278aba into mssql Jul 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants