Skip to content

Commit e246795

Browse files
committed
feat: Implement PEP 673 Self Type for improved type annotations
- Add `Self` import from `typing` module to affected files - Replace `TypeVar` used for self-references with `Self` type - Update method return type annotations in: - Field protocol and implementations in finite_field.py - EllipticCurve class in elliptic_curve.py - State class in state_db.py - Blake2 class in blake2.py related to ethereum#1167
1 parent 2926c00 commit e246795

File tree

4 files changed

+63
-70
lines changed

4 files changed

+63
-70
lines changed

src/ethereum/crypto/blake2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
import struct
66
from dataclasses import dataclass
7-
from typing import Final, List, Tuple
7+
from typing import Final, List, Tuple, Self
88

99
from ethereum_types.numeric import Uint
1010

@@ -151,7 +151,7 @@ def get_blake2_parameters(self, data: bytes) -> Tuple:
151151

152152
def G(
153153
self, v: List, a: Uint, b: Uint, c: Uint, d: Uint, x: Uint, y: Uint
154-
) -> List:
154+
) -> Self:
155155
"""
156156
The mixing function used in Blake2
157157
https://datatracker.ietf.org/doc/html/rfc7693#section-3.1
@@ -185,7 +185,7 @@ def G(
185185
(v[b] ^ v[c]) << self.w_R4
186186
) % self.max_word
187187

188-
return v
188+
return self
189189

190190
def compress(
191191
self,

src/ethereum/crypto/elliptic_curve.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
^^^^^^^^^^^^^^^
44
"""
55

6-
from typing import Generic, Type, TypeVar
6+
from typing import Generic, Type, TypeVar, Self
77

88
import coincurve
99
from ethereum_types.bytes import Bytes
@@ -17,7 +17,6 @@
1717
)
1818

1919
F = TypeVar("F", bound=Field)
20-
T = TypeVar("T", bound="EllipticCurve")
2120

2221

2322
def secp256k1_recover(r: U256, s: U256, v: U256, msg_hash: Hash32) -> Bytes:
@@ -69,7 +68,7 @@ class EllipticCurve(Generic[F]):
6968
x: F
7069
y: F
7170

72-
def __new__(cls: Type[T], x: F, y: F) -> T:
71+
def __new__(cls, x: F, y: F) -> Self:
7372
"""
7473
Make new point on the curve. The point is not checked to see if it is
7574
on the curve.
@@ -104,7 +103,7 @@ def __str__(self) -> str:
104103
return str((self.x, self.y))
105104

106105
@classmethod
107-
def point_at_infinity(cls: Type[T]) -> T:
106+
def point_at_infinity(cls) -> Self:
108107
"""
109108
Return the point at infinity. This is the identity element of the group
110109
operation.
@@ -114,7 +113,7 @@ def point_at_infinity(cls: Type[T]) -> T:
114113
"""
115114
return cls.__new__(cls, cls.FIELD.zero(), cls.FIELD.zero())
116115

117-
def double(self: T) -> T:
116+
def double(self) -> Self:
118117
"""
119118
Add a point to itself.
120119
"""
@@ -126,7 +125,7 @@ def double(self: T) -> T:
126125
new_y = lam * (x - new_x) - y
127126
return self.__new__(type(self), new_x, new_y)
128127

129-
def __add__(self: T, other: T) -> T:
128+
def __add__(self, other: "EllipticCurve") -> Self:
130129
"""
131130
Add two points together.
132131
"""
@@ -146,7 +145,7 @@ def __add__(self: T, other: T) -> T:
146145
y = lam * (self_x - x) - self_y
147146
return self.__new__(type(self), x, y)
148147

149-
def mul_by(self: T, n: int) -> T:
148+
def mul_by(self, n: int) -> Self:
150149
"""
151150
Multiply `self` by `n` using the double and add algorithm.
152151
"""

0 commit comments

Comments
 (0)