-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
redis: add acl stubs #4690
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
redis: add acl stubs #4690
Conversation
srittau
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, especially the link to the implementation is very useful! Remarks below.
third_party/2and3/redis/client.pyi
Outdated
| def pubsub(self, shard_hint: Any = ..., ignore_subscribe_messages: bool = ...) -> PubSub: ... | ||
| def execute_command(self, *args, **options): ... | ||
| def parse_response(self, connection, command_name, **options): ... | ||
| def acl_cat(self, category: Optional[Text]) -> List[str]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def acl_cat(self, category: Optional[Text]) -> List[str]: ... | |
| def acl_cat(self, category: Optional[Text] = ...) -> List[str]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
third_party/2and3/redis/client.pyi
Outdated
| def acl_cat(self, category: Optional[Text]) -> List[str]: ... | ||
| def acl_deluser(self, username: Text) -> int: ... | ||
| def acl_genpass(self) -> Text: ... | ||
| def acl_getuser(self, username: Text) -> Union[None, Dict[str, Union[List[str], bool]]]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In cases like this, where the return value contains an Union (in this case Union inside the Dict, not the outer Union), we prefer to use Any instead. Otherwise the caller has to use isinstance checks each time they access the dict.
An even better alternative here would be to use a TypedDict, which can define exactly which fields and types the returned dict has. I'll leave the decision which of the three options to use (as is, Any, or a TypedDict) to you.
Also a style nit: Instead of Union[None, ...] we usually just use Optional[...].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed explanation; I chose to use Any - I would prefer to have used a TypedDict, but that requires me to name the type, which I would prefer not to do as I'm not the author of the library. (Unless there's a way to create an anonymous TypedDict that I have not been able to find!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine. What we usually do in these cases is creating a name marked with an underscore. There is also the @typing.type_check_only decorator, which hasn't gained traction, but we should use it more.
third_party/2and3/redis/client.pyi
Outdated
| enabled: bool, | ||
| nopass: bool, | ||
| passwords: bool, | ||
| hashed_passwords: Optional[List[Text]], | ||
| categories: Optional[List[Text]], | ||
| commands: Optional[List[Text]], | ||
| keys: Optional[List[Text]], | ||
| reset: bool, | ||
| reset_keys: bool, | ||
| reset_passwords: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these options have default values, so they should have = ....
A few more from looking at the code:
hashed_passwordscan use the looserSequencetype and also accepts plain strings, so I suggest usingOptional[Union[Text, Sequence[Text]]].- Similar for
passwords. categories,commands, andkeysalso can useSequenceinstead of `List.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated with ... for default values and Sequence for List
- add ... for params with defaults - use Sequence instead of List - use Any for return value of Union
Adds stubs for the ACL commands available in Redis 6 that were added to redis-py in v 3.4.0, except
acl_logandacl_log_resetthat are in redis-py master but not yet released https://github.com/andymccurdy/redis-py/blob/master/CHANGES#L11Reference code
https://github.com/andymccurdy/redis-py/blob/master/redis/client.py#L965
Examples
acl_cathttps://redis.io/commands/acl-catacl_deluserhttps://redis.io/commands/acl-deluser (returns number of users that were deleted.)acl_genpasshttps://redis.io/commands/acl-genpassacl_getuserhttps://redis.io/commands/acl-getuseracl_listhttps://redis.io/commands/acl-listacl_loadhttps://redis.io/commands/acl-loadacl_setuser-> https://github.com/andymccurdy/redis-py/blob/master/redis/client.py#L1042acl_usershttps://redis.io/commands/acl-usersacl_whoamihttps://redis.io/commands/acl-whoami