Skip to content

Fix type hints for register_script to support RedisCluster - #3876

Merged
petyaslavova merged 10 commits into
redis:masterfrom
majiayu000:fix/3712-register-script-type-hint
Mar 24, 2026
Merged

petyaslavova merged 10 commits into
redis:masterfrom
majiayu000:fix/3712-register-script-type-hint

Conversation

@majiayu000

@majiayu000 majiayu000 commented Dec 10, 2025 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #3712 #3123

Update type annotations in Script, AsyncScript, and their corresponding register_script methods to accept both Redis and RedisCluster types.

Changes

  • Added redis.cluster and redis.asyncio.cluster to TYPE_CHECKING imports
  • Updated Script.__init__ to accept Union["redis.client.Redis", "redis.cluster.RedisCluster"]
  • Updated Script.__call__ client parameter similarly
  • Updated ScriptCommands.register_script self type
  • Made equivalent changes for async versions (AsyncScript, AsyncScriptCommands)

Test

Before fix, mypy showed:

Invalid self argument "RedisCluster" to attribute function "register_script" with type "Callable[[Redis, Union[bytes, str, memoryview[int]]], Script]"

After fix, no such error.

πŸ€– Generated with Claude Code


Note

Low Risk
Low risk: changes are limited to type hints plus small cluster-only tests, with no functional logic changes beyond widened accepted client types.

Overview
Fixes typing for Lua scripting helpers so register_script, Script, and AsyncScript accept both standalone Redis and RedisCluster clients (including the optional client override when calling a script).

Adds cluster-only tests (sync + asyncio) that register a script on a cluster client and assert it executes successfully.

Written by Cursor Bugbot for commit 63a259d. This will update automatically on new commits. Configure here.

Update type annotations in Script, AsyncScript, and their corresponding
register_script methods to accept both Redis and RedisCluster types.

This fixes mypy errors when calling register_script on RedisCluster instances.

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@jit-ci

jit-ci Bot commented Dec 10, 2025

Copy link
Copy Markdown

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@petyaslavova

Copy link
Copy Markdown
Collaborator

Hi @majiayu000, thank you for your contribution! We will take a look at it soon.

Add tests to verify that Script and AsyncScript classes accept
RedisCluster as registered_client, validating the type hints fix
for register_script to support RedisCluster.

- test_script_with_cluster_client: Tests sync Script with RedisCluster
- test_async_script_with_cluster_client: Tests AsyncScript with RedisCluster

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@majiayu000

Copy link
Copy Markdown
Contributor Author

I've proactively added tests for the type hints fix:

  • test_script_with_cluster_client in tests/test_scripting.py: Tests that sync Script class accepts RedisCluster as registered_client
  • test_async_script_with_cluster_client in tests/test_asyncio/test_scripting.py: Tests that AsyncScript class accepts RedisCluster as registered_client

These tests verify that the type hints fix works correctly for both sync and async implementations.

petyaslavova and others added 3 commits December 15, 2025 13:26
- Fix ruff format issue in AsyncScriptCommands.register_script by
  putting Union type hint on single line
- Fix test_script_with_cluster_client and test_async_script_with_cluster_client
  by using bytes script instead of str to avoid MagicMock encoder issue
  (hashlib.sha1 requires buffer-like object, not MagicMock)

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Comment thread tests/test_asyncio/test_scripting.py Resolved
Comment thread tests/test_scripting.py Resolved
majiayu000 and others added 2 commits December 21, 2025 13:26
Address reviewer feedback:
- Remove MagicMock imports (no longer needed)
- Replace unit tests using mocks with integration tests marked @pytest.mark.onlycluster
- Tests now use real RedisCluster client via the r fixture
- Verify register_script actually works by executing the registered script

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@majiayu000

Copy link
Copy Markdown
Contributor Author

Thanks! I moved the import to the top and added onlycluster tests that register and execute scripts using RedisCluster (sync + asyncio). Please take another look.

majiayu000 and others added 3 commits January 8, 2026 22:55
The test_xreadgroup_with_claim_min_idle_time test is flaky on PyPy
due to timing issues. This empty commit triggers a CI re-run.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@petyaslavova petyaslavova added the maintenance Maintenance (CI, Releases, etc) label Mar 24, 2026

@petyaslavova petyaslavova left a comment

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.

LGTM.

@petyaslavova
petyaslavova merged commit 7e8ea88 into redis:master Mar 24, 2026
129 of 130 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Maintenance (CI, Releases, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mypy error: Invalid self argument "RedisCluster" to attribute function "register_script"

2 participants