Skip to content

Commit 62162d2

Browse files
committed
fix: avoid Python 3.9 dataclass unsafe_hash inheritance error
1 parent ac4628e commit 62162d2

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

schema_salad/rust_codegen.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,15 @@ class RustMeta(ABC):
175175
pass
176176

177177

178-
@dataclass(unsafe_hash=True) # ASSERT: Immutable class
178+
@dataclass # ASSERT: Immutable class
179179
class RustAttribute:
180180
"""Represents a Rust attribute (e.g., `#[derive(Debug)]`)."""
181181

182182
meta: RustMeta
183183

184+
def __hash__(self) -> int:
185+
return hash(self.meta)
186+
184187
def __str__(self) -> str:
185188
return f"#[{str(self.meta)}]"
186189

@@ -193,13 +196,16 @@ def __str__(self) -> str:
193196
RustGenericsMut = MutableSequence[Union[RustLifetime, "RustPath"]] # alias
194197

195198

196-
@dataclass(unsafe_hash=True) # ASSERT: Immutable class
199+
@dataclass # ASSERT: Immutable class
197200
class RustPathSegment:
198201
"""Represents a segment in a Rust path with optional generics."""
199202

200203
ident: RustIdent
201204
generics: RustGenerics = dataclasses.field(default_factory=tuple)
202205

206+
def __hash__(self) -> int:
207+
return hash((self.ident, self.generics))
208+
203209
REX: ClassVar[Pattern[str]] = re.compile(
204210
r"^([a-zA-Z_]\w*)(?:<([ \w\t,'<>]+)>)?$"
205211
) # Using `re.Pattern[str]` raise CI build errors
@@ -256,14 +262,17 @@ def parse_generics_string(value_generics: str) -> RustGenerics:
256262
RustPathSegmentsMut = MutableSequence[RustPathSegment] # alias
257263

258264

259-
@dataclass(unsafe_hash=True) # ASSERT: Immutable class
265+
@dataclass # ASSERT: Immutable class
260266
class RustPath(RustMeta):
261267
"""Represents a complete Rust path (e.g., `::std::vec::Vec<T>`)."""
262268

263269
# ASSERT: Never initialized with an empty sequence
264270
segments: RustPathSegments
265271
leading_colon: bool = False
266272

273+
def __hash__(self) -> int:
274+
return hash((self.segments, self.leading_colon))
275+
267276
def __truediv__(self, other: Union["RustPath", RustPathSegment]) -> "RustPath":
268277
if self.segments[-1].generics:
269278
raise ValueError("Cannot chain to a RustPath with generics.")
@@ -304,13 +313,16 @@ def from_str(cls, value: str) -> "RustPath":
304313
return cls(segments=tuple(segments), leading_colon=leading_colon)
305314

306315

307-
@dataclass(unsafe_hash=True) # ASSERT: Immutable class
316+
@dataclass # ASSERT: Immutable class
308317
class RustTypeTuple(RustType):
309318
"""Represents a Rust tuple type (e.g., `(T, U)`)."""
310319

311320
# ASSERT: Never initialized with an empty sequence
312321
types: Sequence[RustPath]
313322

323+
def __hash__(self) -> int:
324+
return hash(self.types)
325+
314326
def __str__(self) -> str:
315327
types_str = ", ".join(str(ty) for ty in self.types)
316328
return f"({types_str})"

0 commit comments

Comments
 (0)