Skip to content

Improve Type Hinting for Wait Time Functions#3374

Merged
cyberw merged 5 commits intolocustio:masterfrom
abstract-333:typing/add-wait-time
Mar 18, 2026
Merged

Improve Type Hinting for Wait Time Functions#3374
cyberw merged 5 commits intolocustio:masterfrom
abstract-333:typing/add-wait-time

Conversation

@abstract-333
Copy link
Copy Markdown
Contributor

✨ Summary

This PR enhances type safety across Locust's wait time utilities by introducing explicit type hints and resolving related issues flagged by Mypy.

🧠 What’s Included

1. Type Hint Improvements

  • Added precise type annotations to functions in:

    • locust/user/wait_time.py
  • Ensures better static analysis and developer experience when working with wait time utilities.

2. Circular Import Fixes

  • Resolved circular import issues introduced by type hints using TYPE_CHECKING.
  • Clean and safe typing without runtime side effects.

3. Internal State Tracking Fix 🛠️

  • Introduced:

    self._cp_last_wait_time: float = 0

    in:

    • locust/user/task.py
    • locust/user/users.py
  • This mirrors the existing pattern:

    self._cp_last_run: float = time()
  • Prevents Mypy errors when using constant_pacing and similar wait strategies.

4. Functional Improvement in constant_pacing

  • Fully typed internal function:

    def wait_time_func(self: "User") -> float:
  • Explicit tracking of:

    • _cp_last_run
    • _cp_last_wait_time

🧪 Validation

✅ All tests passed
✅ All checks passed
🔗 Workflow

@cyberw cyberw merged commit 3892c42 into locustio:master Mar 18, 2026
18 checks passed
@cyberw cyberw changed the title Improve Type Safety for Wait Time Functions Improve Type Hinting for Wait Time Functions Mar 18, 2026
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