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

fix: FakeRedisMixin.from_url() return type is really Self. #305

Merged
merged 1 commit into from Apr 19, 2024

Conversation

ben-xo
Copy link
Contributor

@ben-xo ben-xo commented Apr 19, 2024

This fixes issues when you have strongly typed client code using that method.

An example would be something like

REDIS_ACTION_STORE_CLIENT: FakeRedis | RedisCluster | None = None

def get_redis_client() -> FakeRedis | RedisCluster:
    if REDIS_ACTION_STORE_CLIENT is None:
        redis_class: type[FakeRedis | RedisCluster] = (
            FakeRedis
            if getattr(settings, "REDIS_TEST_BACKEND", False)
            else RedisCluster
        )
        REDIS_ACTION_STORE_CLIENT = redis_class.from_url(
            url=settings.REDIS_ACTION_STORE_URL, encoding="utf-8", decode_responses=True
        )

    return REDIS_ACTION_STORE_CLIENT

Without this patch, mypy complains that an object of type FakeRedisMixin cannot be assigned to a variable of type FakeRedis | RedisCluster, even though the class we called it on is of type FakeRedis and the returned object will also be a FakeRedis.

client code which explicitly types for FakeRedisMixin should be unaffected.

Tested locally and passes fine with poetry run mypy fakeredis/_server.py on both python 3.7 and 3.11. (a full mypy run isn't currently clean, so i limited my test just to this file).

Additionally, also fixed…

_server.py:142: error: Incompatible default for argument "lua_modules" (default has type "None", argument has type "Set[str]")  [assignment]
_server.py:142: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
_server.py:142: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase

… in this file. That change seems to be needed in other files too, but I figured that was out of scope.

This fixes issues when you have strongly typed client code using that method.
@ben-xo ben-xo requested a review from cunla as a code owner April 19, 2024 15:14
@github-actions github-actions bot added the bug Something isn't working label Apr 19, 2024
@cunla cunla merged commit 1ec2939 into cunla:master Apr 19, 2024
64 checks passed
@ben-xo ben-xo deleted the fakeredismixin-self-type branch April 19, 2024 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants