Skip to content

Fix type hints for register_script to support RedisCluster#3876

Open
majiayu000 wants to merge 9 commits intoredis:masterfrom
majiayu000:fix/3712-register-script-type-hint
Open

Fix type hints for register_script to support RedisCluster#3876
majiayu000 wants to merge 9 commits intoredis:masterfrom
majiayu000:fix/3712-register-script-type-hint

Conversation

@majiayu000
Copy link
Contributor

@majiayu000 majiayu000 commented Dec 10, 2025

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

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
Copy link

jit-ci bot commented Dec 10, 2025

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
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
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>
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
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 2 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>
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.

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

2 participants