Skip to content
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

Incorrect Type Hint for scan Method in Redis Python Library #3460

Open
nh916 opened this issue Dec 20, 2024 · 0 comments
Open

Incorrect Type Hint for scan Method in Redis Python Library #3460

nh916 opened this issue Dec 20, 2024 · 0 comments

Comments

@nh916
Copy link

nh916 commented Dec 20, 2024

Description

The type hint for the scan method in the Redis Python library appears to be incorrect or overly broad. While the method is used in sync mode, the type hint suggests that it can return an Awaitable[Any] | Any, causing issues when using type-checking tools like mypy.

In practice, when using the sync mode, the scan method reliably returns a tuple[int, list[str]] as per the Redis documentation and behavior. However, the current type hint does not accurately reflect this.

Environment

  • Redis Python Version: 5.2.0
  • Python Version: 3.12.1
  • Operating System: Mac

Steps to Reproduce

  1. Use the scan method in sync mode with a Redis client.
  2. Assign the result to a variable with the expected type tuple[int, list[str]].
  3. Run mypy type-checking on the code.
  4. Observe the mypy error.

Example Code

from redis import Redis

redis_client: Redis = Redis(host="localhost", port=6379, decode_responses=True)

# Expected type hint: tuple[int, list[str]]
scan_result: tuple[int, list[str]] = redis_client.scan(
    cursor=0, match="pattern:*", count=1000
)

Expected Behavior

When using the scan method in sync mode, the type should be annotated as:

tuple[int, list[str]]

This aligns with the Redis documentation and avoids unnecessary type-checking errors when using tools like mypy.

Current Behavior

The current type hint in the Redis Python library annotates the return value as:

Awaitable[Any] | Any

This causes mypy to raise errors when the scan method is used in a synchronous context. For example:

scan_result: tuple[int, list[str]] = redis_client.scan(cursor=0, match="pattern:*")

The above snippet results in the following error from mypy:

Incompatible types in assignment (expression has type "Awaitable[Any] | Any", variable has type "tuple[int, list[str]]")
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

No branches or pull requests

1 participant