Skip to content

Commit

Permalink
Better __str__/__repr__ for enums
Browse files Browse the repository at this point in the history
Fix enums having `pack`/`unpack` in Python type hints
  • Loading branch information
VirxEC committed Jun 29, 2024
1 parent db80588 commit 4aa9979
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rlbot-flatbuffers-py"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
description = "A Python module implemented in Rust for serializing and deserializing RLBot's flatbuffers"
repository = "https://github.com/VirxEC/rlbot_flatbuffers_py"
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A Python module implemented in Rust for serializing and deserializing RLBot's fl

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

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

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`).

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

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

- All enum types are hashable and can be used in a set, allowing for easy checks against them.
- All classes (not enums and unions) implement `__match_args__` for easy destructuring via the `match`/`case` pattern.
- Enums and unions and can still be used to match against the type,
they just can't be destructured.
- Every class implements `__str__`, `__repr__`, and `__hash__` methods.
- All enums also implement `__int__` and `__richcmp__`.
- Lists no longer have `num_x` fields accompanying them, they are just Python lists of the appropriate length.
- All enums also implement `__int__` and `__eq__`.
- Lists no longer have `num_x` fields accompanying them,
they are just Python lists of the appropriate length.
8 changes: 2 additions & 6 deletions codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,13 @@ impl EnumBindGenerator {

fn generate_str_method(&mut self) {
write_str!(self, " pub fn __str__(&self) -> String {");
write_str!(self, " format!(\"{self:?}\")");
write_str!(self, " self.__repr__()");
write_str!(self, " }");
}

fn generate_repr_method(&mut self) {
write_str!(self, " pub fn __repr__(&self) -> String {");
write_fmt!(
self,
" format!(\"{}(value={{}})\", *self as u8)",
self.struct_name
);
write_fmt!(self, " format!(\"{}.{{self:?}}\")", self.struct_name);
write_str!(self, " }");
}
}
Expand Down
23 changes: 10 additions & 13 deletions codegen/pyi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,24 +219,21 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {

write_str!(file, " ): ...");
}

write_str!(file, " def pack(self) -> bytes: ...");
write_str!(file, " @staticmethod");
write_fmt!(file, " def unpack(data: bytes) -> {type_name}:");
write_str!(file, " \"\"\"");
write_str!(
file,
" :raises InvalidFlatbuffer: If the `data` is invalid for this type"
);
write_str!(file, " \"\"\"");
}
}

write_str!(file, " def __str__(self) -> str: ...");
write_str!(file, " def __repr__(self) -> str: ...");

if !(matches!(item, PythonBindType::Union { .. })) {
write_str!(file, " def pack(self) -> bytes: ...");
write_str!(file, " @staticmethod");
write_fmt!(file, " def unpack(data: bytes) -> {type_name}:");
write_str!(file, " \"\"\"");
write_str!(
file,
" :raises InvalidFlatbuffer: If the `data` is invalid for this type"
);
write_str!(file, " \"\"\"");
}

write_str!(file, "");
}

Expand Down

0 comments on commit 4aa9979

Please sign in to comment.