Point at the db extra when a table model is declared without it

Merged
IS
3mo ago
+26-0

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Description

closes #6973

On an install without the db extra, rx.Model is aliased to _ClassThatErrorsOnInit, which only raises the guided ImportError from __init__. Declaring a table model (class Item(rx.Model, table=True)) never gets that far: table=True is a class keyword, so Python calls object.__init_subclass__ with an unexpected keyword and the user sees TypeError: Item.__init_subclass__() takes no keyword arguments, with no hint about reflex[db].

_ClassThatErrorsOnInit now defines __init_subclass__ and raises the same guided ImportError there, so the class-definition path and the instantiation path both point at pip install "reflex[db]".

One behaviour note: a plain class Item(rx.Model) without keywords used to define fine on a bare install and only failed at instantiation; it now fails at definition with the same guidance. That seemed like the more useful moment to fail, but if you would rather keep plain subclasses lazy I can gate the check on the presence of class keywords.

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Tests: new tests/units/test_model_no_db.py subclasses _ClassThatErrorsOnInit directly, with and without table=True, and asserts the ImportError names reflex[db], so it runs regardless of whether sqlmodel is installed in the test environment. Ran it locally, plus ruff and pyright on the touched files. Towncrier fragment in news/6973.bugfix.md.