Coverage for faststream/nats/schemas/pull_sub.py: 93%
20 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1from typing import Generic, Literal, Optional, Union, overload
3from typing_extensions import TypeVar
5# Carries `batch` in the type, so `subscriber(pull_sub=PullSub(batch=True))`
6# resolves to the batch subscriber instead of a Union of both.
7BatchT_co = TypeVar("BatchT_co", bound=bool, default=bool, covariant=True)
10class PullSub(Generic[BatchT_co]):
11 """A class to represent a NATS pull subscription.
13 Args:
14 batch_size (int): Target batch size with `batch=True`; maximum messages per request
15 with `batch=False` (default is `1`).
16 timeout (:obj:`float`, optional): Maximum time in seconds to collect a batch with
17 `batch=True`; timeout per request with `batch=False`. `None` waits until the batch
18 is full with `batch=True` (default is `5.0`).
19 batch (bool): Whether to propagate consuming batch as iterable object to your handler (default is `False`).
20 """
22 __slots__ = (
23 "batch",
24 "batch_size",
25 "timeout",
26 )
28 @overload
29 def __init__(
30 self: "PullSub[Literal[False]]",
31 batch_size: int = 1,
32 timeout: float | None = 5.0,
33 batch: Literal[False] = False,
34 ) -> None: ...
36 @overload
37 def __init__(
38 self: "PullSub[Literal[True]]",
39 batch_size: int = 1,
40 timeout: float | None = 5.0,
41 batch: Literal[True] = ...,
42 ) -> None: ...
44 @overload
45 def __init__(
46 self: "PullSub[bool]",
47 batch_size: int = 1,
48 timeout: float | None = 5.0,
49 batch: bool = ...,
50 ) -> None: ...
52 def __init__(
53 self,
54 batch_size: int = 1,
55 timeout: float | None = 5.0,
56 batch: bool = False,
57 ) -> None:
58 if batch_size < 1:
59 message = "You must specify a positive `batch_size`."
60 raise ValueError(message)
61 if timeout is not None and timeout <= 0:
62 message = "You must specify a positive `timeout` or None."
63 raise ValueError(message)
65 self.batch_size = batch_size
66 self.batch = batch
67 self.timeout = timeout
69 @overload
70 @classmethod
71 def validate(cls, value: Literal[True]) -> "PullSub": ...
73 @overload
74 @classmethod
75 def validate(cls, value: Literal[False]) -> None: ...
77 @overload
78 @classmethod
79 def validate(cls, value: "PullSub") -> "PullSub": ...
81 @overload
82 @classmethod
83 def validate(cls, value: Union[bool, "PullSub"]) -> Optional["PullSub"]: ...
85 @classmethod
86 def validate(cls, value: Union[bool, "PullSub"]) -> Optional["PullSub"]:
87 if value is True: 87 ↛ 88line 87 didn't jump to line 88 because the condition on line 87 was never true
88 return PullSub()
89 if value is False:
90 return None
91 return value