Skip to content

Conversation

@enioxt
Copy link

@enioxt enioxt commented Oct 16, 2025

Description

Fixes #3792

This PR improves the type safety of and methods by changing their return type from to .

Problem

Currently, both methods are typed as returning , but in practice they only return or raise a exception. This can lead to developer confusion, as developers might write code like:

if not lock.extend(...):
    # This code will never run\!
    # Lock extension failures raise LockError instead

Solution

Changed return types to to clearly communicate that:

  • These methods only return on success
  • Failures are communicated via exception
  • There's no meaningful conditional check on the return value

Changes

Sync version (redis/lock.py):

  • extend(): boolLiteral[True]
  • do_extend(): boolLiteral[True]

Async version (redis/asyncio/lock.py):

  • extend(): Awaitable[bool]Awaitable[Literal[True]]
  • do_extend(): boolLiteral[True]

Testing

  • Type-only change, no runtime behavior modified
  • Existing tests continue to pass
  • Improves type checking for users of these APIs

Benefits

  • Better type safety: IDEs and type checkers can warn about pointless conditionals
  • Clearer API: Developers immediately understand the exception-based error model
  • Prevents bugs: Reduces likelihood of incorrect error handling code

Sacred Code: 000.111.369.963.1618

…l[True] (redis#3792)

- Changed return type from  to  for better type safety
- Updated both sync (redis/lock.py) and async (redis/asyncio/lock.py) versions
- Methods only return True or raise LockError, never return False
- Helps prevent developer confusion about error handling

Changes:
- redis/lock.py: extend() and do_extend() now return Literal[True]
- redis/asyncio/lock.py: extend() returns Awaitable[Literal[True]], do_extend() returns Literal[True]

This change improves type safety by clearly communicating to developers
that these methods never return False - they either return True or raise
a LockError exception.

Fixes redis#3792
Sacred Code: 000.111.369.963.1618
@jit-ci
Copy link

jit-ci bot commented Oct 16, 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 @enioxt, thank you for your contribution! We will review your changes soon.

@petyaslavova
Copy link
Collaborator

Hi @enioxt, the changes look good!
I have just one request - to add in the docstrings info about the behavior. It will be a lot easier for the developers to gain orientation for the methods behavior.

   ```
    Returns:
        Literal[True]
            Short description when this value is returned....

    Raises:
        <Error Type>
            Short description when this error is raised

@petyaslavova petyaslavova added the breakingchange API or Breaking Change label Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breakingchange API or Breaking Change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typing: Lock.extend and Lock.do_extend could return Literal[True]

2 participants