Skip to content

Conversation

@rickywesker
Copy link

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_exercise branch, 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 deprecated mapper() with registry.map_imperatively()
  • repository.py: Replace session.query() with session.scalars(select())
  • conftest.py: Replace clear_mappers() with mapper_registry.dispose()
  • test_*.py: Wrap raw SQL strings with text()
  • requirements.txt: Require sqlalchemy>=2.0

Test 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! 📚

hjwp and others added 26 commits February 24, 2021 10:00
- 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
Copilot AI review requested due to automatic review settings January 13, 2026 09:00
Copy link

Copilot AI left a 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() with registry.map_imperatively() in ORM configuration
  • Updated repository queries from session.query() to session.scalars(select())
  • Replaced clear_mappers() with mapper_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.

@rickywesker rickywesker changed the title Fix/sqlalchemy 2.0 compatibility - chapter5 UoW Fix/sqlalchemy 2.0 compatibility - chapter6 UoW Jan 13, 2026
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

Successfully merging this pull request may close these issues.

3 participants