Fix/sqlalchemy 2.0 compatibility - chapter6 UoW#110
Open
rickywesker wants to merge 67 commits intocosmicpython:chapter_05_uow_exercisefrom
Open
Fix/sqlalchemy 2.0 compatibility - chapter6 UoW#110rickywesker wants to merge 67 commits intocosmicpython:chapter_05_uow_exercisefrom
rickywesker wants to merge 67 commits intocosmicpython:chapter_05_uow_exercisefrom
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.
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.
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.
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! 📚