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

hjwpand 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
CopilotAI review requested due to automatic review settings January 13, 2026 09:00
Copy link

CopilotAI 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
FileDescription
src/allocation/adapters/orm.pyUpdated to use registry and map_imperatively for SQLAlchemy 2.0
src/allocation/adapters/repository.pyReplaced session.query() with session.scalars(select())
tests/conftest.pyUpdated mapper cleanup to use mapper_registry.dispose()
tests/integration/test_orm.pyWrapped SQL strings with text(), contains critical bug in assertion order
tests/integration/test_repository.pyWrapped SQL strings with text()
tests/integration/test_uow.pyWrapped SQL strings with text()
requirements.txtSpecified sqlalchemy>=2.0 requirement
mypy.iniConsolidated pytest and sqlalchemy ignore settings
README.mdContains minor spelling error in documentation
tests/unit/test_*.pyNew unit test files for exercises
tests/pytest.iniNew pytest configuration
tests/e2e/test_api.pyNew end-to-end API tests
src/allocation/service_layer/*.pyNew service layer files for UoW exercises
src/allocation/entrypoints/flask_app.pyNew Flask application entrypoint
src/allocation/domain/model.pyDomain model definitions
src/allocation/config.pyConfiguration for database and API connections
Dockerfile, docker-compose.yml, Makefile, .travis.yml, src/setup.pyInfrastructure and build configuration files

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@rickyweskerrickywesker changed the title Fix/sqlalchemy 2.0 compatibility - chapter5 UoWFix/sqlalchemy 2.0 compatibility - chapter6 UoWJan 13, 2026
Sign up for freeto 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

@rickywesker@hjwp@karolpawlowski