Skip to content

Commit 7a7fb37

Browse files
committed
Make ChannelError generic
Instead of using `Any`, which will lose type information if the user wants to inspect the channel that caused the error, we make it generic. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 1564a00 commit 7a7fb37

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/frequenz/channels/_exceptions.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
```
6767
"""
6868

69-
from typing import Any
69+
from typing import Generic
70+
71+
from ._generic import ErroredChannelT_co
7072

7173

7274
class Error(RuntimeError):
@@ -84,28 +86,28 @@ def __init__(self, message: str):
8486
super().__init__(message)
8587

8688

87-
class ChannelError(Error):
89+
class ChannelError(Error, Generic[ErroredChannelT_co]):
8890
"""An error that originated in a channel.
8991
9092
All exceptions generated by channels inherit from this exception.
9193
"""
9294

93-
def __init__(self, message: str, channel: Any):
95+
def __init__(self, message: str, channel: ErroredChannelT_co):
9496
"""Initialize this error.
9597
9698
Args:
9799
message: The error message.
98100
channel: The channel where the error happened.
99101
"""
100102
super().__init__(message)
101-
self.channel: Any = channel
103+
self.channel: ErroredChannelT_co = channel
102104
"""The channel where the error happened."""
103105

104106

105-
class ChannelClosedError(ChannelError):
107+
class ChannelClosedError(ChannelError[ErroredChannelT_co]):
106108
"""A closed channel was used."""
107109

108-
def __init__(self, channel: Any):
110+
def __init__(self, channel: ErroredChannelT_co):
109111
"""Initialize this error.
110112
111113
Args:

0 commit comments

Comments
 (0)