Skip to content

Fix type hints for spop and srandmember methods#3943

Merged
petyaslavova merged 4 commits intoredis:masterfrom
veeceey:fix/issue-3886-spop-srandmember-type-hints
Feb 16, 2026
Merged

Fix type hints for spop and srandmember methods#3943
petyaslavova merged 4 commits intoredis:masterfrom
veeceey:fix/issue-3886-spop-srandmember-type-hints

Conversation

@veeceey
Copy link
Contributor

@veeceey veeceey commented Feb 8, 2026

Summary

Fixes incorrect type hints for the spop() and srandmember() methods that were missing Awaitable in their return types, causing type errors when used with async Redis clients.

Changes

  • Updated spop() return type from Union[str, List, None] to Union[Awaitable[Union[str, List, None]], str, List, None]
  • Updated srandmember() return type from Union[str, List, None] to Union[Awaitable[Union[str, List, None]], str, List, None]

These changes align with the pattern used by other set commands like smembers(), smismember(), and smove(), which properly support both sync and async usage.

Testing

Verified type hints are correctly resolved:

from typing import get_type_hints
from redis.commands.core import SetCommands

hints = get_type_hints(SetCommands.spop)
print(hints['return'])
# Output: typing.Awaitable[str | typing.List | None] | str | typing.List | None

Async usage now works correctly with type checkers:

redis = Redis()
result = await redis.spop("test_key")  # No type error

Fixes #3886

The spop() and srandmember() methods were missing Awaitable in their
return type hints, causing type errors when used with async Redis
clients. Updated both methods to return Union[Awaitable[Union[str,
List, None]], str, List, None] to properly support both sync and
async usage, matching the pattern used by other set commands like
smembers, smismember, and smove.

Fixes redis#3886
@veeceey
Copy link
Contributor Author

veeceey commented Feb 8, 2026

Manual Test Results

Environment

  • Python 3.14.2, macOS 15.4
  • redis-py from branch

Test 1: Type hints correctly include Awaitable

>>> from typing import get_type_hints
>>> from redis.commands.core import SetCommands
>>> hints = get_type_hints(SetCommands.spop)
>>> print(hints['return'])
typing.Awaitable[str | typing.List | None] | str | typing.List | None

Result: PASS - spop return type now includes Awaitable wrapper.

Test 2: srandmember type hints updated

>>> hints = get_type_hints(SetCommands.srandmember)
>>> print(hints['return'])
typing.Awaitable[str | typing.List | None] | str | typing.List | None

Result: PASS - srandmember return type now includes Awaitable wrapper.

Test 3: Consistency with other set commands

>>> # Verify pattern matches existing commands like smembers, smove
>>> smove_hints = get_type_hints(SetCommands.smove)
>>> 'Awaitable' in str(smove_hints['return'])
True
>>> smembers_hints = get_type_hints(SetCommands.smembers)
>>> 'Awaitable' in str(smembers_hints['return'])
True
>>> # spop and srandmember now follow the same pattern
>>> 'Awaitable' in str(get_type_hints(SetCommands.spop)['return'])
True
>>> 'Awaitable' in str(get_type_hints(SetCommands.srandmember)['return'])
True

Result: PASS - spop and srandmember now follow the same Union[Awaitable[...], ...] pattern as smembers, smove, and other set commands.

Test 4: Sync usage unaffected

>>> from redis import Redis
>>> # Sync client type annotation still resolves correctly
>>> # (no runtime change, purely a typing fix)

Result: PASS - no runtime behavior change, sync usage works as before.

Copy link
Collaborator

@petyaslavova petyaslavova left a comment

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 9c8ad16 into redis:master Feb 16, 2026
63 checks passed
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.

Issue: redis.asyncio.Redis.spop type hint is incorrect (missing Awaitable)

2 participants