Package imports pkg_resources at import time without declaring setuptools, so a clean install cannot load the dialect
Versions
- sqlalchemy-monetdb 2.0.0
- pymonetdb 1.9.1
- SQLAlchemy 2.0.51
- Python 3.12.9, macOS
- Server:
monetdb/monetdb:latest, 11.55.7
What happens
sqlalchemy_monetdb/__init__.py imports pkg_resources at package import, unconditionally, only to read its own version string:
import pkg_resources
try:
__version__ = pkg_resources.require("sqlalchemy-monetdb")[0].version
except pkg_resources.DistributionNotFound:
...
pkg_resources ships inside setuptools, which the distribution doesn't declare — requires_dist is pymonetdb>=1.8.2, sqlalchemy>=2.0.34 and test extras, nothing else. A modern environment doesn't carry setuptools unless something asks for it, so on a clean install every test in my suite errored at setup with:
ModuleNotFoundError: No module named 'pkg_resources'
There's no way around it from the caller's side: SQLAlchemy loads the dialect through the entry point monetdb -> sqlalchemy_monetdb.dialect:MonetDialect, which runs the package __init__ before anything else.
The version ceiling this forces
Adding setuptools isn't quite enough, because pkg_resources was removed from setuptools itself. Measured by installing each version into a clean target and looking, rather than reading a changelog:
| setuptools | ships pkg_resources |
warns on import |
|---|---|---|
| 80.10.2 | yes | yes |
| 81.0.0 | yes | yes |
| 82.0.1 | no | — |
| 83.0.0 | no | — |
So a consumer has to pin setuptools<82, and that pin propagates to anything that depends on them in turn. In my case it's the only version ceiling in the whole project, which is an annoying thing to carry for a version string. Both surviving setuptools versions also emit a deprecation warning per session, and the warning's own advice ("pin to Setuptools<81") doesn't silence it, because 80.10.2 warns too.
Suggested fix
importlib.metadata, stdlib since 3.8:
from importlib.metadata import version, PackageNotFoundError
try:
__version__ = version("sqlalchemy-monetdb")
except PackageNotFoundError:
__version__ = "unknown"
Happy to send a PR if that's welcome.
To be clear about where the fault lies — this is the dialect, not MonetDB, and not pymonetdb. I'm recording it that way on my side so nobody later goes looking in the database for it.
I was thinking of taking this as a small import-time compatibility fix: replace the deprecated pkg_resources version lookup with importlib.metadata, keep the existing unknown-version fallback, and add a clean-install regression test that imports the dialect without setuptools. I checked the repository and did not find an overlapping open PR. Would that focused approach be useful here? If so, I can prepare it.
Thanks for the offer, but I think that part is already done — I went and looked at master before answering rather than at the sdist I filed against.
sqlalchemy_monetdb/__init__.py on master is now a docstring and __version__ = '2.1.0a0', nothing else, and pyproject points [tool.hatch.version] at that file. So the pkg_resources lookup went away in 2b1b3c71 back in February. A PR replacing it with importlib.metadata would have nothing left to change.
What's actually missing is a release. The latest on PyPI is 2.0.0 from 2024-09-17, which is what pip install sqlalchemy-monetdb still gives you, so a clean install today hits the ModuleNotFoundError anyway and still needs the setuptools<82 pin. The fix exists, it just isn't reachable from the index.
The one piece of your proposal that would still add something is the regression test, since it guards against the import creeping back in. If you do it, make sure the test really installs into a clean target (pip install --no-deps --target ..., or an isolated venv) — in a normal dev environement setuptools is usually dragged in by something else and the test passes for the wrong reason without ever excercising the case.
I'm leaving this open since it's about the published artifact, not the tree, but a maintainer may reasonably prefer to close it when a 2.1 (or a 2.0.1 backport) lands.
You were right that master already removed the version lookup, so I left the package code untouched and focused on the remaining gap: a regression test against an installed copy. It installs the checkout into an empty target with --no-deps, blocks pkg_resources, and verifies the import came from that target. I opened draft PR #62; the focused test, lint, and whitespace checks pass locally. Let me know what you think.