Skip to content

test(vars): assert real error messages in computed var tests#6750

Open
anxkhn wants to merge 1 commit into
reflex-dev:mainfrom
anxkhn:patch-11
Open

test(vars): assert real error messages in computed var tests#6750
anxkhn wants to merge 1 commit into
reflex-dev:mainfrom
anxkhn:patch-11

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

What

Two computed-var error tests in tests/units/test_var.py placed their message
assertions inside the with pytest.raises(...) block, on the line right after
the statement that raises:

  • test_computed_var_without_annotation_error
  • test_computed_var_with_annotation_error

Because the preceding line raises, control leaves the with block immediately,
so the assertion lines were never reached. On top of that, err.value is only
populated after the block exits, so the in-block assertions could not have run
even structurally. Both asserted strings were also stale and no longer matched
the messages the code raises, so a regressed or wrong error message would still
pass CI.

Fix

Move each assertion out of the raises block and compare against the message the
code actually raises:

  • Unannotated case: UntypedComputedVarError (a TypeError), asserted exactly:
    "Computed var 'var_without_annotation' must have a type annotation."
  • Wrongly annotated case: VarAttributeError (an AttributeError). The message
    begins with a dynamic state-var path, so the test matches on the stable suffix
    via str.endswith(...):
    "of type <class 'str'> has no attribute 'foo' or may have been annotated wrongly."
    (Both parametrized fixtures declare the computed var as -> str, so
    <class 'str'> is correct.)

This is a test-only change; no framework behavior changes.

Testing

  • uv run pytest tests/units/test_var.py::test_computed_var_without_annotation_error tests/units/test_var.py::test_computed_var_with_annotation_error -> 4 passed
  • uv run pytest tests/units/test_var.py -> 204 passed
  • uv run ruff check tests/units/test_var.py and uv run ruff format --check tests/units/test_var.py -> clean
  • uv run pyright tests/units/test_var.py -> 0 errors
  • Verified the corrected assertions actually catch a regression: temporarily
    breaking the real source messages makes the corrected tests fail, whereas the
    original (pre-fix) assertions still passed against the same broken source,
    confirming they were dead code.

The message assertions in test_computed_var_without_annotation_error and
test_computed_var_with_annotation_error sat inside the with pytest.raises(...)
block, after the line that raises, so they were unreachable and err.value was
not yet populated. Both asserted strings were also stale and no longer matched
the raised messages, so a regressed error message would still pass.

Move each assertion out of the raises block and compare against the messages
the code actually raises: UntypedComputedVarError for the unannotated case, and
the VarAttributeError text for the wrongly annotated case (matched on its stable
suffix, since the state-var prefix is dynamic).

Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
@anxkhn anxkhn requested a review from a team as a code owner July 12, 2026 11:15
@greptile-apps

greptile-apps Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes two computed-var error-message checks execute correctly. The main changes are:

  • Moves both assertions outside their pytest.raises blocks.
  • Checks the exact missing-annotation message.
  • Checks the stable suffix of the invalid-attribute message.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.
  • The assertions now execute after pytest.raises captures each exception.
  • The expected text matches the inspected exception construction paths.

Important Files Changed

Filename Overview
tests/units/test_var.py Moves two unreachable message assertions after exception capture and updates their expected messages.

Reviews (1): Last reviewed commit: "test(vars): assert real error messages i..." | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Jul 12, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing anxkhn:patch-11 (f34e572) with main (9a5c4d3)

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.

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.

1 participant