-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix/sqlalchemy 2.0 compatibility - chapter6 UoW #110
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
base: chapter_05_uow_exercise
Are you sure you want to change the base?
Fix/sqlalchemy 2.0 compatibility - chapter6 UoW #110
Conversation
- Replace deprecated mapper() with registry.map_imperatively() - Replace session.query() with session.scalars(select()) - Wrap raw SQL strings with text() - Replace clear_mappers() with mapper_registry.dispose() - Update requirements.txt to require sqlalchemy>=2.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR updates the Cosmic Python book Chapter 6 exercise code to be compatible with SQLAlchemy 2.0, migrating away from deprecated APIs to modern SQLAlchemy 2.0 patterns.
Changes:
- Replaced deprecated
mapper()withregistry.map_imperatively()in ORM configuration - Updated repository queries from
session.query()tosession.scalars(select()) - Replaced
clear_mappers()withmapper_registry.dispose()in test fixtures - Wrapped raw SQL strings with
text()for SQLAlchemy 2.0 compatibility - Updated requirements to specify
sqlalchemy>=2.0 - Consolidated mypy configuration for pytest and sqlalchemy imports
Reviewed changes
Copilot reviewed 23 out of 29 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| src/allocation/adapters/orm.py | Updated to use registry and map_imperatively for SQLAlchemy 2.0 |
| src/allocation/adapters/repository.py | Replaced session.query() with session.scalars(select()) |
| tests/conftest.py | Updated mapper cleanup to use mapper_registry.dispose() |
| tests/integration/test_orm.py | Wrapped SQL strings with text(), contains critical bug in assertion order |
| tests/integration/test_repository.py | Wrapped SQL strings with text() |
| tests/integration/test_uow.py | Wrapped SQL strings with text() |
| requirements.txt | Specified sqlalchemy>=2.0 requirement |
| mypy.ini | Consolidated pytest and sqlalchemy ignore settings |
| README.md | Contains minor spelling error in documentation |
| tests/unit/test_*.py | New unit test files for exercises |
| tests/pytest.ini | New pytest configuration |
| tests/e2e/test_api.py | New end-to-end API tests |
| src/allocation/service_layer/*.py | New service layer files for UoW exercises |
| src/allocation/entrypoints/flask_app.py | New Flask application entrypoint |
| src/allocation/domain/model.py | Domain model definitions |
| src/allocation/config.py | Configuration for database and API connections |
| Dockerfile, docker-compose.yml, Makefile, .travis.yml, src/setup.py | Infrastructure and build configuration files |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Hi Harry! 👋
I'm a reader following along with the Cosmic Python book, and I really enjoyed Chapter 6 on the Unit of Work pattern!
While working through the exercises on the
chapter_06_uow_exercisebranch, I noticed the code wasn't compatible with SQLAlchemy 2.0, which caused some errors when running tests. So I made the necessary updates to get everything working smoothly.Changes
orm.py: Replace deprecatedmapper()withregistry.map_imperatively()repository.py: Replacesession.query()withsession.scalars(select())conftest.py: Replaceclear_mappers()withmapper_registry.dispose()test_*.py: Wrap raw SQL strings withtext()requirements.txt: Requiresqlalchemy>=2.0Test Results
All existing tests pass (20 passed, 3 skipped, 4 failed).
The 4 failed tests are expected - they are exercise-related tests designed to fail until the reader implements the UnitOfWork pattern.
I hope this PR can help other readers who encounter the same issue, and save them some debugging time! 🙏
Thank you for writing such a great book! 📚