-
Notifications
You must be signed in to change notification settings - Fork 876
Introspection: add function signatures #5025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3f68386
ab7abe5
49be236
c12ff46
57ef77c
9d63c36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth adding unit tests to this file for various cases to show they format as we want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! (note we already got good coverage with the integration tests) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use crate::model::{Class, Function, Module}; | ||
use crate::model::{Argument, Class, Function, Module, VariableLengthArgument}; | ||
use std::collections::HashMap; | ||
use std::path::{Path, PathBuf}; | ||
|
||
|
@@ -48,5 +48,104 @@ fn class_stubs(class: &Class) -> String { | |
} | ||
|
||
fn function_stubs(function: &Function) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be nice to check that vararg and kwarg have no default as part of this function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added a new |
||
format!("def {}(*args, **kwargs): ...", function.name) | ||
// Signature | ||
let mut parameters = Vec::new(); | ||
for argument in &function.arguments.positional_only_arguments { | ||
parameters.push(argument_stub(argument)); | ||
} | ||
if !function.arguments.positional_only_arguments.is_empty() { | ||
parameters.push("/".into()); | ||
} | ||
for argument in &function.arguments.arguments { | ||
parameters.push(argument_stub(argument)); | ||
} | ||
if let Some(argument) = &function.arguments.vararg { | ||
parameters.push(format!("*{}", variable_length_argument_stub(argument))); | ||
} else if !function.arguments.keyword_only_arguments.is_empty() { | ||
parameters.push("*".into()); | ||
} | ||
for argument in &function.arguments.keyword_only_arguments { | ||
parameters.push(argument_stub(argument)); | ||
} | ||
if let Some(argument) = &function.arguments.kwarg { | ||
parameters.push(format!("**{}", variable_length_argument_stub(argument))); | ||
} | ||
format!("def {}({}): ...", function.name, parameters.join(", ")) | ||
} | ||
|
||
fn argument_stub(argument: &Argument) -> String { | ||
let mut output = argument.name.clone(); | ||
if let Some(default_value) = &argument.default_value { | ||
output.push('='); | ||
output.push_str(default_value); | ||
} | ||
output | ||
} | ||
|
||
fn variable_length_argument_stub(argument: &VariableLengthArgument) -> String { | ||
argument.name.clone() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::model::Arguments; | ||
|
||
#[test] | ||
fn function_stubs_with_variable_length() { | ||
let function = Function { | ||
name: "func".into(), | ||
arguments: Arguments { | ||
positional_only_arguments: vec![Argument { | ||
name: "posonly".into(), | ||
default_value: None, | ||
}], | ||
arguments: vec![Argument { | ||
name: "arg".into(), | ||
default_value: None, | ||
}], | ||
vararg: Some(VariableLengthArgument { | ||
name: "varargs".into(), | ||
}), | ||
keyword_only_arguments: vec![Argument { | ||
name: "karg".into(), | ||
default_value: None, | ||
}], | ||
kwarg: Some(VariableLengthArgument { | ||
name: "kwarg".into(), | ||
}), | ||
}, | ||
}; | ||
assert_eq!( | ||
"def func(posonly, /, arg, *varargs, karg, **kwarg): ...", | ||
function_stubs(&function) | ||
) | ||
} | ||
|
||
#[test] | ||
fn function_stubs_without_variable_length() { | ||
let function = Function { | ||
name: "afunc".into(), | ||
arguments: Arguments { | ||
positional_only_arguments: vec![Argument { | ||
name: "posonly".into(), | ||
default_value: Some("1".into()), | ||
}], | ||
arguments: vec![Argument { | ||
name: "arg".into(), | ||
default_value: Some("True".into()), | ||
}], | ||
vararg: None, | ||
keyword_only_arguments: vec![Argument { | ||
name: "karg".into(), | ||
default_value: Some("\"foo\"".into()), | ||
}], | ||
kwarg: None, | ||
}, | ||
}; | ||
assert_eq!( | ||
"def afunc(posonly=1, /, arg=True, *, karg=\"foo\"): ...", | ||
function_stubs(&function) | ||
) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am mostly happy with this, though I do somewhat wish that this encoded the fact that vararg and kwarg cannot be defaulted (and also that required positional arguments cannot follow positional arguments with defaults).
In practice this is probably good enough, as long as we add suitable checks elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a new
VariableLengthArgument
type without thedefault
field.