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

Add more stubs for redis-py #4480

Merged
merged 3 commits into from
Sep 17, 2020
Merged

Add more stubs for redis-py #4480

merged 3 commits into from
Sep 17, 2020

Conversation

brainix
Copy link
Contributor

@brainix brainix commented Aug 25, 2020

I've tested these stubs against my own project here:
brainix/pottery#221

The output is clean for all these new stubs when running $ mypy --strict.

I've tested these stubs against my own project here:
brainix/pottery#221

The output is clean for all these new stubs when running `$ mypy
--strict`.
Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A few remarks below.

third_party/2and3/redis/client.pyi Outdated Show resolved Hide resolved
third_party/2and3/redis/client.pyi Outdated Show resolved Hide resolved
alpha: bool = ...,
store: Optional[_Key] = ...,
groups: Optional[bool] = ...,
) -> Union[List[bytes], int]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the result type depends on whether store is given, we should use overloads to choose the right type (most arguments omitted in the example for brevity):

@overload
def sort(
    self,
    name: _Key, 
    start: Optional[int] = ...,
    # etc.
    store: None = ...,
    groups: Optional[bool] = ...,
) -> List[bytes]: ...
@overload
def sort(
    self,
    name: _Key, 
    start: Optional[int] = ...,
    # etc. with default
    *,
    store: _Key,
    groups: Optional[bool] = ...,
) -> int: ...
@overload
def sort(
    self,
    name: _Key, 
    start: Optional[int],
    # etc. without default
    store: _Key,
    groups: Optional[bool] = ...,
) -> int: ...

Unfortunately we need to overloads for the second case, otherwise we'd have a syntax error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. However, I'm a little stuck. Whenever I try to implement this solution, I get the following errors:

stubs/redis/client.pyi:190: error: Overloaded function signatures 1 and 2 overlap with incompatible return types
stubs/redis/client.pyi:190: error: Overloaded function signatures 1 and 3 overlap with incompatible return types

Any tips?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I can't reproduce this here locally, although we sometimes need # type: ignore when overloading. Could you commit what you have so far? We squash merge anyway.

brainix added a commit to brainix/pottery that referenced this pull request Aug 28, 2020
brainix added a commit to brainix/pottery that referenced this pull request Aug 28, 2020
@brainix brainix force-pushed the redis-py-stubs branch 2 times, most recently from 0d437d9 to f84a497 Compare August 28, 2020 03:19
brainix added a commit to brainix/pottery that referenced this pull request Aug 28, 2020
brainix added a commit to brainix/pottery that referenced this pull request Aug 28, 2020
@srittau srittau merged commit 98667b1 into python:master Sep 17, 2020
@brainix brainix deleted the redis-py-stubs branch September 17, 2020 19:22
brainix added a commit to brainix/pottery that referenced this pull request Sep 18, 2020
* Annotate linsert()'s where argument as a Literal[]

Addresses this feedback:
python/typeshed#4480 (comment)

* Annotate sort()'s groups argument as a bool

Addresses this feedback:
python/typeshed#4480 (comment)
brainix added a commit to brainix/pottery that referenced this pull request Jun 9, 2021
My type annotation stubs made it upstream!
python/typeshed#4480

But then I had to install `types-redis` because running Mypy 0.901 gave
me these error messages:

```
Running static type checks
pottery/exceptions.py:22: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/monkey.py:37: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/base.py:45: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/base.py:45: note: Hint: "python3 -m pip install types-redis"
pottery/base.py:45: note: (or run "mypy --install-types" to install all missing stub packages)
pottery/base.py:45: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
pottery/base.py:47: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/set.py:28: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/set.py:29: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/redlock.py:52: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/redlock.py:54: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/nextid.py:37: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/nextid.py:39: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/list.py:30: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/list.py:32: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/hyper.py:25: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/dict.py:31: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/dict.py:32: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/deque.py:25: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/deque.py:26: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/counter.py:30: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/cache.py:36: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/cache.py:37: error: Library stubs not installed for "redis.exceptions" (or incompatible with Python 3.9)
Found 20 errors in 12 files (checked 18 source files)
```
brainix added a commit to brainix/pottery that referenced this pull request Jun 9, 2021
My type annotation stubs made it upstream!
python/typeshed#4480

But then I had to install `types-redis` because running Mypy 0.901 gave
me these error messages:

```
Running static type checks
pottery/exceptions.py:22: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/monkey.py:37: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/base.py:45: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/base.py:45: note: Hint: "python3 -m pip install types-redis"
pottery/base.py:45: note: (or run "mypy --install-types" to install all missing stub packages)
pottery/base.py:45: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
pottery/base.py:47: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/set.py:28: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/set.py:29: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/redlock.py:52: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/redlock.py:54: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/nextid.py:37: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/nextid.py:39: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/list.py:30: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/list.py:32: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/hyper.py:25: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/dict.py:31: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/dict.py:32: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/deque.py:25: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/deque.py:26: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/counter.py:30: error: Library stubs not installed for "redis.client" (or incompatible with Python 3.9)
pottery/cache.py:36: error: Library stubs not installed for "redis" (or incompatible with Python 3.9)
pottery/cache.py:37: error: Library stubs not installed for "redis.exceptions" (or incompatible with Python 3.9)
Found 20 errors in 12 files (checked 18 source files)
```
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.

None yet

2 participants