Make psycopg2 import optional in db_utils#123
Merged
Merged
Conversation
myyong
marked this pull request as draft
July 15, 2026 10:27
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>
tim-band
approved these changes
Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
datafaker/db_utils.pyimportedpsycopg2.errors.UndefinedObjectunconditionally at module level, butpsycopg2-binaryis only installed via the optionalpostgresextra (seepyproject.toml).mssqlextra (notpostgres) fails to importdatafakerat all, which previously broke the standalonetests-mssql.ymlworkflow before it was replaced.UndefinedObjectinside atry/except ImportError, falling back to an empty placeholder class.UndefinedObjectis psycopg2-specific and can only ever match errors raised by the psycopg2 driver, so theisinstancecheck at the one call site safely evaluates toFalsewhen psycopg2 isn't installed -- no behavior change when it is.(Note: this PR originally also fixed a gpg/
/dev/ttyfailure intests-mssql.yml, but that workflow was removed upstream in favor of running MSSQL tests directly intests.yml, which sidesteps the issue. Rebased to drop that now-moot commit.)Test plan
datafaker.db_utilsimports successfully withpsycopg2mocked as absent, and that theisinstancecheck safely returnsFalseGenerated with Claude Code