Skip to content

Fix SQLAlchemy serializer feedback for #7831 - #7885

Open
harsh21234i wants to merge 8 commits into
reflex-dev:mainfrom
harsh21234i:codex/7831-declarative-serializer
Open

harsh21234i wants to merge 8 commits into
reflex-dev:mainfrom
harsh21234i:codex/7831-declarative-serializer

Conversation

@harsh21234i

@harsh21234i harsh21234i commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7831

  • Raise a clear VarAttributeError when an SQLAlchemy hybrid property is omitted from serialized state, without evaluating its SQL expression.
  • Warn once in development when all mapped columns are expired; keep production quiet and avoid lazy loading.
  • Add regression tests and a reflex-base bugfix news fragment.

Validation: 1,504 reflex_base tests passed, 30 skipped. Ruff format/lint and codespell passed.

View guided diff

@harsh21234i
harsh21234i requested a review from a team as a code owner November 13, 2026 09:07
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The changes appear safe to merge, with no new actionable issues found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The changes appear safe to merge, with no new actionable issues found.

Summary

Adds clearer feedback when SQLAlchemy values are missing from serialized state.

  • Accessing an omitted SQLAlchemy hybrid property gives a clear error.
  • Development warns once when every mapped column has expired.

Reviews (6) · Last reviewed commit: "test: guard production expiration scans ..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@codspeed

codspeed Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 19 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing harsh21234i:codex/7831-declarative-serializer (a03466e) with main (6f5c39c)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@FarhanAliRaza FarhanAliRaza added the bug Something isn't working label Nov 13, 2026 — with ChatGPT Codex Connector

@YusefSyed YusefSyed left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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). With packages/reflex-base/src reverted to main (cb9ee91), the hybrid test and the dev case of the expired test fail.
  • ruff check, ruff format --check and pyright on the touched files: clean.
  • I ran the model from #7831 (MappedAsDataclass, user: User | None). S.user.upper_name now raises VarAttributeError, and an instance assigned to state after commit() 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 calls len(self.name) raised a TypeError from 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.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeclarativeBase serializer renders blanks silently: hybrid_property values are never sent, expired instances serialize as {}

3 participants