Skip to content

some typing nits#6722

Draft
benedikt-bartscher wants to merge 1 commit into
reflex-dev:mainfrom
benedikt-bartscher:port-typing-fixes
Draft

some typing nits#6722
benedikt-bartscher wants to merge 1 commit into
reflex-dev:mainfrom
benedikt-bartscher:port-typing-fixes

Conversation

@benedikt-bartscher

Copy link
Copy Markdown
Contributor

No description provided.

@greptile-apps

greptile-apps Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes minor typing and test-hygiene improvements across several files with no functional regressions. All changes are small, self-contained, and backward-compatible.

  • Type narrowing: EventChain.invocation is narrowed from Var | None to FunctionVar | None, matching the runtime guard already in EventChain.create(); the combined Field.__get__ overload is split into four for better type-checker resolution; and isinstance(plugin, Mapping) guards in shared_tailwind.py are replaced with the equivalent not isinstance(plugin, str) (removing the now-unused Mapping import).
  • Defensive attribute access: hasattr + direct attribute access is replaced with getattr(..., None) is not None in both field.py and vars/base.py, correctly handling the edge case where the field is explicitly set to None.
  • Test hygiene: tempfile.mktemp() (deprecated, TOCTOU-unsafe) is replaced with pytest's tmp_path fixture, and direct mutation of HandlerState.handler is replaced with monkeypatch.setattr to guarantee state restoration after the test.

Confidence Score: 5/5

All changes are narrow, non-breaking cleanups with no observable behavioral difference in normal usage.

Every change is a safe refinement: types are narrowed to match already-enforced runtime invariants, attribute-access patterns are made more defensive, and deprecated test utilities are replaced with idiomatic pytest equivalents. No new logic is introduced, and the not isinstance(plugin, str) substitution in shared_tailwind.py is semantically identical given the union type constraint.

No files require special attention.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/components/field.py Replaces hasattr guards with walrus-operator getattr checks to safely handle the edge case where _inherited_fields or _own_fields is explicitly None.
packages/reflex-base/src/reflex_base/event/init.py Narrows the type of EventChain.invocation from Var
packages/reflex-base/src/reflex_base/plugins/shared_tailwind.py Replaces isinstance(plugin, Mapping) guards with not isinstance(plugin, str), removes the now-unused Mapping import; semantically equivalent given TailwindPluginConfig = TypedDict
packages/reflex-base/src/reflex_base/vars/base.py Splits one combined get overload covering four Field variants into four separate overloads for better type-checker resolution, and mirrors the same hasattr→getattr walrus-operator cleanup as field.py.
tests/units/test_prerequisites.py Replaces deprecated tempfile.mktemp() calls (TOCTOU-unsafe) with pytest tmp_path paths; uses uuid4 for uniqueness where needed; tempfile module is retained for the mkdtemp() call elsewhere.
tests/units/test_state.py Adds monkeypatch fixture to properly restore HandlerState.handler after the test, and retrieves the underlying function via event_handlers["handler"].fn instead of the pyright-suppressed .fn access on the EventHandler descriptor.

Reviews (1): Last reviewed commit: "some typing nits" | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Jul 8, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing benedikt-bartscher:port-typing-fixes (d257f14) with main (2d446c3)2

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.

  2. No successful run was found on main (d54ad96) during the generation of this report, so 2d446c3 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Comment on lines -111 to +110
if isinstance(plugin, Mapping) and "import" in plugin
if not isinstance(plugin, str) and "import" in plugin

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i dont like this one because it's a runtime check. despite the static typing, if this plugin object is not a str, it could be anything else and indexing into it with a str would be invalid. hence, checking if it's explicitly a Mapping makes the in check and subsequent __getitem__ call safe.

what was the motivation for this change? i might be misreading it.

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.

2 participants