Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psycopg2-binary is not considered as a suitable driver for Postgres. #210

Closed
mgedmin opened this issue Apr 15, 2024 · 2 comments
Closed

Comments

@mgedmin
Copy link

mgedmin commented Apr 15, 2024

Describe the bug

My tox.ini lists the following deps:

[testenv]
deps =
  -rrequirements.txt
  pytest-alembic
  pytest-mock-resources[postgres-binary]

and requirements.txt has

# alembic 1.8.1 matches debian bookworm
alembic==1.8.1
# sqlalchemy-citext is not packaged for debian
sqlalchemy-citext==1.8.0
# psycopg2 2.9.5 matches debian bookworm
psycopg2-binary==2.9.5
sqlalchemy >= 1.4, < 2.0

This used to work fine, but with recent pytest-mock-resources releases I'm getting test failures everywhere with this error:

.tox/py3.11/lib/python3.11/site-packages/pytest_mock_resources/container/postgres.py:103: in get_sqlalchemy_engine
    drivername = detect_driver(config.drivername, async_=async_)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

drivername = None, async_ = False

    def detect_driver(drivername: Optional[str] = None, async_: bool = False) -> str:
        if drivername:
            return drivername
    
        if any(Distribution.discover(name="psycopg")):
            return "postgresql+psycopg"
    
        if async_:
            if any(Distribution.discover(name="asyncpg")):
                return "postgresql+asyncpg"
        else:
            if any(Distribution.discover(name="psycopg2")):
                return "postgresql+psycopg2"
    
>       raise ValueError(  # pragma: no cover
            "No suitable driver found for Postgres. Please install `psycopg`, `psycopg2`, "
            "`asyncpg`, or explicitly configure the `drivername=` field of `PostgresConfig`."
        )
E       ValueError: No suitable driver found for Postgres. Please install `psycopg`, `psycopg2`, `asyncpg`, or explicitly configure the `drivername=` field of `PostgresConfig`.

.tox/py3.11/lib/python3.11/site-packages/pytest_mock_resources/container/postgres.py:141: ValueError

Environment

  • Host OS: Ubuntu 23.10 but actually anywhere
  • Docker image if applicable
  • Python Version: 3.11
  • Virtualenv/Pyenv etc.. if applicable: tox-managed virtualenv

To Reproduce
Steps to reproduce the behavior:

  1. virtualenv testenv && testenv/bin/pip install pytest-mock-resources[postgres-binary]
  2. testenv/bin/python -c 'from pytest_mock_resources.container.postgres import detect_driver; print(detect_driver())'
  3. See error

Expected behavior
I expected it to print postgresql+psycopg2.

Actual Behavior

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/testenv/lib/python3.11/site-packages/pytest_mock_resources/container/postgres.py", line 141, in detect_driver
    raise ValueError(  # pragma: no cover
ValueError: No suitable driver found for Postgres. Please install `psycopg`, `psycopg2`, `asyncpg`, or explicitly configure the `drivername=` field of `PostgresConfig`.

Additional context
BTW testenv/bin/pip list confirms that psycopg2-binary is installed.

@DanCardin
Copy link
Contributor

DanCardin commented Apr 15, 2024

Hopefully fixed in #208 a little bit ago? 2.10.3

@mgedmin
Copy link
Author

mgedmin commented Apr 16, 2024

Wow, that PR was opened 30 minutes before I submitted this ticket. I will claim that I searched for existing issues, didn't find one, and then spent 30 minutes composing this one. ;-)

@mgedmin mgedmin closed this as completed Apr 16, 2024
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

No branches or pull requests

2 participants