Skip to content

Commit 44ce1bf

Browse files
committed
Also define __new__ in .pyi
1 parent 4847f12 commit 44ce1bf

3 files changed

Lines changed: 71 additions & 60 deletions

File tree

Cargo.lock

Lines changed: 30 additions & 30 deletions
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.5.0"
3+
version = "0.5.1"
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"

codegen/pyi.rs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
6060

6161
write_fmt!(file, " item: Optional[{union_str}]");
6262
write_str!(file, "");
63+
write_str!(file, " def __new__(");
64+
write_fmt!(file, " cls, item: Optional[{union_str}] = None");
65+
write_str!(file, " ): ...");
6366
write_str!(file, " def __init__(");
6467
write_fmt!(file, " self, item: Optional[{union_str}] = None");
65-
write_str!(file, " ): ...");
68+
write_str!(file, " ): ...\n");
6669
}
6770
PythonBindType::Enum(gen) => {
6871
for variable_info in &gen.types {
@@ -76,13 +79,14 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
7679
}
7780

7881
write_str!(file, "");
82+
write_str!(file, " def __new__(cls, value: int = 0): ...");
7983
write_str!(file, " def __init__(self, value: int = 0):");
8084
write_str!(file, " \"\"\"");
8185
write_str!(
8286
file,
8387
" :raises ValueError: If the `value` is not a valid enum value"
8488
);
85-
write_str!(file, " \"\"\"\n");
89+
write_str!(file, " \"\"\"");
8690
write_str!(file, " def __int__(self) -> int: ...");
8791
write_fmt!(file, " def __eq__(self, other: {type_name}) -> bool: ...");
8892
write_str!(file, " def __hash__(self) -> str: ...");
@@ -192,36 +196,43 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
192196
write_str!(file, " def __init__(self): ...");
193197
} else {
194198
write_str!(file, "");
195-
write_str!(file, " def __init__(");
196-
write_str!(file, " self,");
197-
198-
for (variable_info, python_type) in gen.types.iter().zip(python_types) {
199-
let variable_name = variable_info.name.as_str();
200-
201-
let default_value = match variable_info.raw_type.as_str() {
202-
"bool" => Cow::Borrowed("False"),
203-
"i32" | "u32" | "f32" | "u8" => Cow::Borrowed("0"),
204-
"String" => Cow::Borrowed("\"\""),
205-
"Vec<u8>" => Cow::Borrowed("b\"\""),
206-
t => {
207-
if python_type.starts_with("Optional") || t.starts_with("Option<") {
208-
Cow::Borrowed("None")
209-
} else if t.starts_with("Vec<") {
210-
Cow::Borrowed("[]")
211-
} else if t.starts_with("Box<") {
212-
let inner_type =
213-
t.trim_start_matches("Box<").trim_end_matches('>').trim_end_matches('T');
214-
Cow::Owned(format!("{inner_type}()"))
215-
} else {
216-
Cow::Owned(format!("{}()", t.trim_end_matches('T')))
199+
200+
let inits = [("new", "cls"), ("init", "self")];
201+
202+
for (func, first_arg) in inits {
203+
write_fmt!(file, " def __{func}__(");
204+
write_fmt!(file, " {first_arg},");
205+
206+
for (variable_info, python_type) in gen.types.iter().zip(&python_types) {
207+
let variable_name = variable_info.name.as_str();
208+
209+
let default_value = match variable_info.raw_type.as_str() {
210+
"bool" => Cow::Borrowed("False"),
211+
"i32" | "u32" | "f32" | "u8" => Cow::Borrowed("0"),
212+
"String" => Cow::Borrowed("\"\""),
213+
"Vec<u8>" => Cow::Borrowed("b\"\""),
214+
t => {
215+
if python_type.starts_with("Optional") || t.starts_with("Option<") {
216+
Cow::Borrowed("None")
217+
} else if t.starts_with("Vec<") {
218+
Cow::Borrowed("[]")
219+
} else if t.starts_with("Box<") {
220+
let inner_type = t
221+
.trim_start_matches("Box<")
222+
.trim_end_matches('>')
223+
.trim_end_matches('T');
224+
Cow::Owned(format!("{inner_type}()"))
225+
} else {
226+
Cow::Owned(format!("{}()", t.trim_end_matches('T')))
227+
}
217228
}
218-
}
219-
};
229+
};
220230

221-
write_fmt!(file, " {variable_name}: {python_type} = {default_value},");
222-
}
231+
write_fmt!(file, " {variable_name}: {python_type} = {default_value},");
232+
}
223233

224-
write_str!(file, " ): ...");
234+
write_str!(file, " ): ...");
235+
}
225236
}
226237

227238
write_str!(file, " def pack(self) -> bytes: ...");

0 commit comments

Comments
 (0)