Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Use covariant type Mapping instead of invariant type Dict in arguments and let's wait for mypy upstream issue about nested Union #1182

Open
1 task done
achimnol opened this issue Oct 27, 2021 · 3 comments
Labels

Comments

@achimnol
Copy link
Contributor

achimnol commented Oct 27, 2021

Describe the bug

I see some typecheck failures in my code using aioredis v2,
which are related to python/mypy#8554
where the arguments with generics should use covariant type Mapping instead of Dict to allow aioredis user codes to be properly typechecked.

To Reproduce

    async def produce_event(
        self,
        event: AbstractEvent,
        *,
        source: str = 'manager',
    ) -> None:
        raw_event: Dict[bytes, bytes] = {
            b'name': event.name.encode(),
            b'source': source.encode(),
            b'args': msgpack.packb(event.serialize()),
        }
        await redis.execute(
            self.redis_client,
            lambda r: r.xadd('events', raw_event),  # <-- type error
        )

Type error:

error: Argument 2 to "xadd" of "Redis" has incompatible type "Dict[bytes, bytes]";
expected "Dict[Union[Union[bytes, memoryview], Union[str, int, float]], Union[Union[bytes, memoryview], Union[str, int, float]]]"

Expected behavior

There should be no type error. At the runtime the code runs fine.

Logs/tracebacks

Nothing for here.

Python Version

$ python --version
Python 3.9.6

aioredis Version

$ python -m pip show aioredis
Name: aioredis
Version: 2.0.0
Summary: asyncio (PEP 3156) Redis support
Home-page: https://github.com/aio-libs/aioredis
Author:
Author-email:
License: MIT
Location: /Users/joongi/.pyenv/versions/3.9.6/envs/venv-edge-common/lib/python3.9/site-packages
Requires: async-timeout, typing-extensions
Required-by: backend.ai-common

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@achimnol achimnol added the bug label Oct 27, 2021
@achimnol
Copy link
Contributor Author

achimnol commented Oct 27, 2021

Manually modifying aioredis source code to use Mapping still seems not to resolve the issue.
Maybe it's something to do with nested Unions? 🤔

error: Argument 2 to "xadd" of "Redis" has incompatible type "Dict[bytes, bytes]";
expected "Mapping[Union[Union[bytes, memoryview], Union[str, int, float]], Union[Union[bytes, memoryview], Union[str, int, float]]]"

@achimnol
Copy link
Contributor Author

Maybe... it's another mypy issue: python/mypy#10764 😭

@achimnol achimnol changed the title Use covariant type Mapping instead of invariant type Dict in arguments Use covariant type Mapping instead of invariant type Dict in arguments and let's wait for mypy upstream issue about nested Union Oct 27, 2021
@Andrew-Chen-Wang
Copy link
Collaborator

heads up: I have not been checking many mypy issues in #1156 which may be a downfall to the repository. Any help for #1156 would be greatly appreciated!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants