coredis icon indicating copy to clipboard operation
coredis copied to clipboard

[breaking] enhance pubsub types

Open Le0Developer opened this issue 2 years ago • 2 comments

⚠️ This PR includes breaking changes.

Changes:

  • Split PubSubMessage into several types for more accurate type hints. (⚠️ breaking!)
  • All pubsub messages are now generics
  • Changed TypedDict to dataclasses (TypedDict and NamedTuple don't support generics yet) (⚠️ breaking!)
  • Added is_* constants for convenience because mypy type narrowing with message.type in ("message", "pmessage", "smessage") doesn't actually narrow it down. (see details below) (honestly might remove this since the fix is just message.type == "message" or message.type == "pmessage" or ...)
  • Removed channel from p(un)subscribe messages (⚠️ breaking!)
from typing import NamedTuple, Literal, cast


class AA(NamedTuple):
    a: Literal["a"]
    b: str


class AB(NamedTuple):
    a: Literal["b"]
    d: int


a = cast(AA | AB, AA("a", "x"))

if a.a == "a":
    reveal_type(a)  # AA

if a.a in ("a",):
    reveal_type(a)  # AA | AB (!)

Le0Developer avatar Aug 01 '22 11:08 Le0Developer

Codecov Report

Merging #82 (a1ba1b1) into master (39e1b7a) will decrease coverage by 13.52%. The diff coverage is 77.50%.

@@             Coverage Diff             @@
##           master      #82       +/-   ##
===========================================
- Coverage   98.24%   84.72%   -13.53%     
===========================================
  Files          56       56               
  Lines        7123     7174       +51     
===========================================
- Hits         6998     6078      -920     
- Misses        125     1096      +971     
Impacted Files Coverage Δ
coredis/commands/pubsub.py 44.36% <35.71%> (-46.87%) :arrow_down:
coredis/response/types.py 90.62% <100.00%> (-6.73%) :arrow_down:
coredis/stream.py 22.22% <0.00%> (-77.78%) :arrow_down:
coredis/sentinel.py 25.80% <0.00%> (-73.39%) :arrow_down:
coredis/commands/monitor.py 36.25% <0.00%> (-62.50%) :arrow_down:
coredis/response/_callbacks/sentinel.py 43.39% <0.00%> (-56.61%) :arrow_down:
coredis/commands/script.py 56.94% <0.00%> (-38.89%) :arrow_down:
coredis/pipeline.py 54.79% <0.00%> (-38.38%) :arrow_down:
coredis/client/keydb.py 62.00% <0.00%> (-38.00%) :arrow_down:
... and 21 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 39e1b7a...a1ba1b1. Read the comment docs.

codecov[bot] avatar Aug 01 '22 11:08 codecov[bot]

Seems like pyright doesn't support this kind of "advanced" type narrowing at all 🤦

Le0Developer avatar Aug 03 '22 19:08 Le0Developer

@Le0Developer okay if we close this PR for now?

alisaifee avatar Nov 16 '22 16:11 alisaifee