-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix type hints for register_script to support RedisCluster #3876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix type hints for register_script to support RedisCluster #3876
Conversation
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 <[email protected]>
|
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. |
|
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 <[email protected]>
|
I've proactively added tests for the type hints fix:
These tests verify that the type hints fix works correctly for both sync and async implementations. |
- 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 <[email protected]>
| We use a mock-like approach since we don't need actual cluster connection. | ||
| Using bytes script to avoid encoder dependency in mock. | ||
| """ | ||
| from unittest.mock import MagicMock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import should be added at the top of the file.
|
|
||
| # Script should accept RedisCluster without type errors | ||
| # Using bytes script to bypass encoder.encode() call | ||
| script = Script(mock_cluster, script_bytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to validate that you can actually register the script using the RedisCluster client. With unit tests you won't be able to validate that we don't have a static code analyses errors, but what we can do is to validate that by providing the RedisCluster as valid input type - the client actually can be used to execute a register_script call.
Summary
Fixes #3712 #3123
Update type annotations in
Script,AsyncScript, and their correspondingregister_scriptmethods to accept bothRedisandRedisClustertypes.Changes
redis.clusterandredis.asyncio.clusterto TYPE_CHECKING importsScript.__init__to acceptUnion["redis.client.Redis", "redis.cluster.RedisCluster"]Script.__call__client parameter similarlyScriptCommands.register_scriptself typeAsyncScript,AsyncScriptCommands)Test
Before fix, mypy showed:
After fix, no such error.
🤖 Generated with Claude Code