Skip to content

Commit 4aa9979

Browse files
committed
Better __str__/__repr__ for enums
Fix enums having `pack`/`unpack` in Python type hints
1 parent db80588 commit 4aa9979

5 files changed

Lines changed: 21 additions & 25 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rlbot-flatbuffers-py"
3-
version = "0.4.1"
3+
version = "0.4.2"
44
edition = "2021"
55
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
66
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A Python module implemented in Rust for serializing and deserializing RLBot's fl
66

77
To provide a fast, safe, and easy to use Python module for serializing and deserializing RLBot's flatbuffers.
88

9-
A majority of the code is generated in the `build.rs` upon first compile and thrown into `src/python`.
9+
A majority of the code is generated in the `codegen/` upon first compile and thrown into `src/python`.
1010

1111
This includes the code generated by `flatc` (living in `src/generated`), the Python wrapper binds to the generated Rust code, and the Python type hints (`rlbot_flatbuffers.pyi`).
1212

@@ -76,7 +76,10 @@ def handle_packet(packet: flat.GameTickPacket):
7676

7777
The goal of the above was to feel familiar to RLBot v4 while providing a more Pythonic interface.
7878

79-
- All enum types are hashable and can be used in a set, allowing for easy checks against them.
79+
- All classes (not enums and unions) implement `__match_args__` for easy destructuring via the `match`/`case` pattern.
80+
- Enums and unions and can still be used to match against the type,
81+
they just can't be destructured.
8082
- Every class implements `__str__`, `__repr__`, and `__hash__` methods.
81-
- All enums also implement `__int__` and `__richcmp__`.
82-
- Lists no longer have `num_x` fields accompanying them, they are just Python lists of the appropriate length.
83+
- All enums also implement `__int__` and `__eq__`.
84+
- Lists no longer have `num_x` fields accompanying them,
85+
they are just Python lists of the appropriate length.

codegen/enums.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,13 @@ impl EnumBindGenerator {
193193

194194
fn generate_str_method(&mut self) {
195195
write_str!(self, " pub fn __str__(&self) -> String {");
196-
write_str!(self, " format!(\"{self:?}\")");
196+
write_str!(self, " self.__repr__()");
197197
write_str!(self, " }");
198198
}
199199

200200
fn generate_repr_method(&mut self) {
201201
write_str!(self, " pub fn __repr__(&self) -> String {");
202-
write_fmt!(
203-
self,
204-
" format!(\"{}(value={{}})\", *self as u8)",
205-
self.struct_name
206-
);
202+
write_fmt!(self, " format!(\"{}.{{self:?}}\")", self.struct_name);
207203
write_str!(self, " }");
208204
}
209205
}

codegen/pyi.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,24 +219,21 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
219219

220220
write_str!(file, " ): ...");
221221
}
222+
223+
write_str!(file, " def pack(self) -> bytes: ...");
224+
write_str!(file, " @staticmethod");
225+
write_fmt!(file, " def unpack(data: bytes) -> {type_name}:");
226+
write_str!(file, " \"\"\"");
227+
write_str!(
228+
file,
229+
" :raises InvalidFlatbuffer: If the `data` is invalid for this type"
230+
);
231+
write_str!(file, " \"\"\"");
222232
}
223233
}
224234

225235
write_str!(file, " def __str__(self) -> str: ...");
226236
write_str!(file, " def __repr__(self) -> str: ...");
227-
228-
if !(matches!(item, PythonBindType::Union { .. })) {
229-
write_str!(file, " def pack(self) -> bytes: ...");
230-
write_str!(file, " @staticmethod");
231-
write_fmt!(file, " def unpack(data: bytes) -> {type_name}:");
232-
write_str!(file, " \"\"\"");
233-
write_str!(
234-
file,
235-
" :raises InvalidFlatbuffer: If the `data` is invalid for this type"
236-
);
237-
write_str!(file, " \"\"\"");
238-
}
239-
240237
write_str!(file, "");
241238
}
242239

0 commit comments

Comments
 (0)