Repository navigation
Fix SQLAlchemy serializer feedback for #7831 - #7885
harsh21234i wants to merge 8 commits into
Conversation
[Medium impact] The changes appear safe to merge, with no new actionable issues found. SummaryAdds clearer feedback when SQLAlchemy values are missing from serialized state.
Reviews (6) · Last reviewed commit: "test: guard production expiration scans ..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 6 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
YusefSyed
left a comment
There was a problem hiding this comment.
Checked at cb6a016 on Python 3.13.14 and 3.11.15 with SQLAlchemy 2.1.4.
pytest tests/units/reflex_base/utils/test_serializers.py tests/units/reflex_base/utils/test_types.py: 90 passed, 1 skipped on 3.13 (87 passed, 1 skipped on 3.11). Withpackages/reflex-base/srcreverted to main (cb9ee91), the hybrid test and thedevcase of the expired test fail.ruff check,ruff format --checkand pyright on the touched files: clean.- I ran the model from #7831 (
MappedAsDataclass,user: User | None).S.user.upper_namenow raisesVarAttributeError, and an instance assigned to state aftercommit()logs the warning once and still serializes as{}. - I also tried a hybrid on a mixin, on a mapped subclass and through
S.users[0], and instances that were detached, rolled back or pickled. All behave the same way. A hybrid whose getter callslen(self.name)raised aTypeErrorfrom the Var access on main and gets the clear error here.
Two things I found.
1. The REFLEX_ENV_MODE lookup is slower than the scan it skips.
environment.REFLEX_ENV_MODE.get() now runs first for every model on every serialization. I timed serialize_sqlalchemy_base on 1000 loaded instances of a 4-column model (Python 3.13.14, macOS arm64, best of 7):
| per model | main | this PR |
|---|---|---|
REFLEX_ENV_MODE unset |
0.9 µs | 2.4 µs |
REFLEX_ENV_MODE=prod |
0.9 µs | 3.7 µs |
The lookup alone takes about 1.0 µs unset and 2.5 µs when set. The all(...) scan takes about 0.15 µs on a loaded instance, because it stops at the first loaded column. format.json_dumps on the same list goes from 1.5 µs to 4.4 µs per model with prod. I found nothing under tests/benchmarks that serializes a SQLAlchemy model, which would explain why CodSpeed reports no regression.
Fully expired instances cost 3.7 µs in both modes (0.8 to 0.9 µs on main).
Reading the variable once per model class brought loaded instances back to about 0.95 µs and expired ones to 1.1 µs in dev and prod, with the same 90 tests passing:
if (
state.expired_attributes
and has_identity_key
and layout.columns
and type(m) not in _WARNED_EXPIRED_SQLALCHEMY_MODELS
and all(key in state.expired_attributes for key in layout.columns)
):
_WARNED_EXPIRED_SQLALCHEMY_MODELS.add(type(m))
if environment.REFLEX_ENV_MODE.get() == constants.Env.DEV:
logger.warning(...)2. The new test passes without the expired check.
I removed and all(key in state.expired_attributes for key in layout.columns) and the new test still passed, along with the rest of the serializer, types, object var, model and state unit tests I ran (778 passed). With that line gone, a loaded instance that serializes as {'id': 1, 'name': 'alice'} logs the "all mapped columns expired" warning. Changing all to any also passes.
A case with a loaded instance and one with only name expired (session.expire(user, ["name"])), both asserting no log record, catches both. I tried that test: it passes here and fails with either change.
There was a problem hiding this comment.
All reported issues were addressed across 2 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 4 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Fixes #7831
Validation: 1,504 reflex_base tests passed, 30 skipped. Ruff format/lint and codespell passed.