Skip to content

Commit 1839fb1

Browse files
committed
Exchanges: cleaner typing for subclass attributes
1 parent 0eff378 commit 1839fb1

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

p2p/protocol.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from abc import ABC, abstractmethod
1+
from abc import ABC
22
import logging
33
import struct
44
from typing import (
@@ -135,17 +135,14 @@ class BaseRequest(ABC, Generic[TRequestPayload]):
135135
Must define command_payload during init. This is the data that will
136136
be sent to the peer with the request command.
137137
"""
138+
# Defined at init time, with specific parameters:
138139
command_payload: TRequestPayload
139140

140-
@property
141-
@abstractmethod
142-
def cmd_type(self) -> Type[Command]:
143-
raise NotImplementedError
144-
145-
@property
146-
@abstractmethod
147-
def response_type(self) -> Type[Command]:
148-
raise NotImplementedError
141+
# Defined as class attributes in subclasses
142+
# outbound command type
143+
cmd_type: Type[Command]
144+
# response command type
145+
response_type: Type[Command]
149146

150147

151148
class Protocol:

trinity/protocol/common/exchanges.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class BaseExchange(ABC, Generic[TRequestPayload, TResponsePayload, TResult]):
4141
TResult is the response data after normalization
4242
"""
4343

44+
request_class: Type[BaseRequest[TRequestPayload]]
45+
4446
def __init__(self, mgr: ExchangeManager[TRequestPayload, TResponsePayload, TResult]) -> None:
4547
self._manager = mgr
4648

@@ -72,15 +74,9 @@ async def get_result(
7274
timeout=timeout
7375
)
7476

75-
@property
76-
@abstractmethod
77-
def request_class(cls) -> Type[BaseRequest[TRequestPayload]]:
78-
raise NotImplementedError('request_class must be defined on every Exchange')
79-
8077
@classproperty
8178
def response_cmd_type(cls) -> Type[Command]:
82-
# mypy is confused about the "abstract class property"
83-
return cls.request_class.response_type # type: ignore
79+
return cls.request_class.response_type
8480

8581
@abstractmethod
8682
async def __call__(self, *args: Any, **kwargs: Any) -> None:

0 commit comments

Comments
 (0)