Skip to content

refactor(api): enable strict typing for rate limit test fixtures - #44391

Open
ShousenZHANG wants to merge 1 commit into
langgenius:mainfrom
ShousenZHANG:refactor/strict-typing-rate-limit-conftest
Open

ShousenZHANG wants to merge 1 commit into
langgenius:mainfrom
ShousenZHANG:refactor/strict-typing-rate-limit-conftest

Conversation

@ShousenZHANG

Copy link
Copy Markdown
Contributor

Summary

Part of #24645, following #44331.

tests/unit_tests/core/app/features/rate_limiting/conftest.py was excluded from strict Pyrefly checks and produced 34 diagnostics, all missing annotations. With this change it is under strict checking. test_rate_limit.py next to it stays excluded, because #43201 and #43044 are changing it.

While typing it, mock_time.increment() turned out not to work. It advanced a local counter, but the patched time.time kept returning the return_value it was created with (1000.0), so calling it never moved the clock. No test calls it; the tests set mock_time.return_value directly. It is removed.

mock_redis is unused on main, but #43201 uses it, so it stays and is typed. No production code changes.

What changed

  • mock_redis. Its in-memory stores and fake Redis methods are typed. The TTL check uses isinstance(ttl, timedelta) instead of hasattr(ttl, "total_seconds"), which gives the same result for integer and timedelta TTLs.
  • mock_time. It patches time.time with return_value=1000.0 and yields the mock, without increment.
  • Return types. The other fixtures get them: Generator[...] for the yielding ones, and Callable[..., Generator[str]] for sample_generator.
  • Config. Remove conftest.py from the unit-test Pyrefly exclusions.

Verification

  • pyrefly check --config=tests/unit_tests/pyrefly.toml --use-ignore-files=false --disable-project-excludes-heuristics=true on conftest.py (from api/, Pyrefly 1.3.2 as pinned in uv.lock): 34 diagnostics before, zero after, with no suppressions.
  • pyrefly check --config=tests/unit_tests/pyrefly.toml over the whole unit-test project: passed.
  • These all passed:
    • ruff format --check and ruff check (without the cache), on the files and on the whole api/ tree
    • lint-imports: 68 contracts kept
    • scripts/check_no_new_getattr.py, check_no_new_controller_sqlalchemy.py, check_no_new_session_mock.py, check_no_new_base_model.py, check_no_new_test_doubles.py and check_no_spec_mock.py
    • dev/lint_response_contracts.py --fail-on-mismatch
  • pytest -v tests/unit_tests/core/app/features/rate_limiting on Python 3.13: 28 passed, the same 28 test IDs as before the change. With this branch merged into feat: add workspace max active requests limit #43201's head, its rate limiting tests (31) pass.

Screenshots

Before After
N/A: test and type-check configuration only; 34 strict diagnostics. N/A: zero strict diagnostics; files included in normal type checks.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods (backend only: ran the underlying ruff, import-linter, guard-script and Pyrefly commands listed above)

From Claude Code

Annotate the rate limiting conftest, drop the mock_time.increment
helper that never moved the patched clock, and remove the file from
the unit-test Pyrefly exclusions.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 04:44

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 75.77% 75.78% +0.01%
Strict coverage 75.44% 75.45% +0.01%
Typed symbols 63,293 63,298 +5
Untyped symbols 20,329 20,324 -5
Modules 3814 3814 0

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.55%. Comparing base (38a5efe) to head (1688432).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #44391   +/-   ##
=======================================
  Coverage   88.54%   88.55%           
=======================================
  Files        5522     5522           
  Lines      331079   331163   +84     
  Branches    66207    66214    +7     
=======================================
+ Hits       293169   293254   +85     
- Misses      32724    32727    +3     
+ Partials     5186     5182    -4     
Flag Coverage Δ
api 88.78% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 self-assigned this Oct 20, 2026
@asukaminato0721
asukaminato0721 added this pull request to the merge queue Oct 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants