-
Notifications
You must be signed in to change notification settings - Fork 15
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
xadd
& xread
& xreadgroup
use invariant dict typing
#129
Comments
There's an alternative but it's definitely messier. KT_co = TypeVar("KT_co", covariant=True)
VT_co = TypeVar("VT_co", covariant=True)
class CovariantMapping(Protocol[KT_co, VT_co]):
def __eq__(self, other: object) -> bool: ...
def __ne__(self, other: object) -> bool: ...
def keys(self) -> KeysView[KT_co]: ...
def items(self) -> ItemsView[KT_co, VT_co]: ...
def values(self) -> ValuesView[VT_co]: ... |
I have a draft PR up that I'm currently testing locally. Unfortunately, related to #84 (some really awesome effort there!) it's somewhat difficult to check types directly. |
First of all, sorry I've not been able to allocate more time to the #84. #130 seems fine, I don't see any obvious issue with it (note that I'm not an expert in Python typing either). Once merged, I can rebase #84 and keep going (given time). @imnotjames: if you're interesting in contributing to #84, I'd love that. There is a lot more work to do before it's mergeable. |
Just FYI, I re-did the PR (#134), this time with signoff so it's somewhat closer to mergeable. |
hi @Rafiot |
@amirreza8002 It probably makes more sense to have this discussion in the PR, here it is: #134 (comment) |
Along with all of the other choices made by the previous maintainers around typing, it seems the mappings passed to
xadd
,read
, andxreadgroup
are invariant and do not allow passing in values likedict[str, str]
. This makes sense when you're concerned about someone setting an incompatible value into the dict or mapping. A bit less for cases like this where we just want to read the values. (I think? I'm not an expert on Python typing!)This can be seen with
xreadgroup
, when passing in the streams and stream IDs:valkey-py/valkey/commands/core.py
Lines 3972 to 3980 in c490780
If I were to try to interact with this like such:
'course, things like pyright don't like this and emit an error about it. Invariant types and all that.
There's a helpful comment about this in the
typing.py
file.valkey-py/valkey/typing.py
Lines 42 to 46 in c490780
Using this, we can apply it to
StreamIdT
to create a version that's covariant.It seems to work well! I tested it with the above example with
xreadgroup
, at least. I can't seem to find any issues to this approach -- and it seems to be used elsewhere even if not here.The text was updated successfully, but these errors were encountered: