Skip to content

Commit 66156d0

Browse files
committed
Code review feedback
1 parent 7cb3913 commit 66156d0

File tree

3 files changed

+76
-61
lines changed

3 files changed

+76
-61
lines changed

pyo3-introspection/src/introspection.rs

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -101,70 +101,83 @@ fn convert_members(
101101
)?);
102102
}
103103
Chunk::Class { name, id } => {
104-
let (_, _, mut methods) = convert_members(
105-
chunks_by_parent
106-
.get(&id.as_str())
107-
.map(Vec::as_slice)
108-
.unwrap_or_default(),
109-
chunks_by_id,
110-
chunks_by_parent,
111-
)?;
112-
// We sort methods to get a stable output
113-
methods.sort_by(|l, r| match l.name.cmp(&r.name) {
114-
Ordering::Equal => {
115-
// We put the getter before the setter
116-
if l.decorators.iter().any(|d| d == "property") {
117-
Ordering::Less
118-
} else if r.decorators.iter().any(|d| d == "property") {
119-
Ordering::Greater
120-
} else {
121-
// We pick an ordering based on decorators
122-
l.decorators.cmp(&r.decorators)
123-
}
124-
}
125-
o => o,
126-
});
127-
classes.push(Class {
128-
name: name.into(),
129-
methods,
130-
})
104+
classes.push(convert_class(id, name, chunks_by_id, chunks_by_parent)?)
131105
}
132106
Chunk::Function {
133107
name,
134108
id: _,
135109
arguments,
136110
parent: _,
137111
decorators,
138-
} => functions.push(Function {
139-
name: name.into(),
140-
decorators: decorators.clone(),
141-
arguments: Arguments {
142-
positional_only_arguments: arguments
143-
.posonlyargs
144-
.iter()
145-
.map(convert_argument)
146-
.collect(),
147-
arguments: arguments.args.iter().map(convert_argument).collect(),
148-
vararg: arguments
149-
.vararg
150-
.as_ref()
151-
.map(convert_variable_length_argument),
152-
keyword_only_arguments: arguments
153-
.kwonlyargs
154-
.iter()
155-
.map(convert_argument)
156-
.collect(),
157-
kwarg: arguments
158-
.kwarg
159-
.as_ref()
160-
.map(convert_variable_length_argument),
161-
},
162-
}),
112+
} => functions.push(convert_function(name, arguments, decorators)),
163113
}
164114
}
165115
Ok((modules, classes, functions))
166116
}
167117

118+
fn convert_class(
119+
id: &str,
120+
name: &str,
121+
chunks_by_id: &HashMap<&str, &Chunk>,
122+
chunks_by_parent: &HashMap<&str, Vec<&Chunk>>,
123+
) -> Result<Class> {
124+
let (nested_modules, nested_classes, mut methods) = convert_members(
125+
chunks_by_parent
126+
.get(&id)
127+
.map(Vec::as_slice)
128+
.unwrap_or_default(),
129+
chunks_by_id,
130+
chunks_by_parent,
131+
)?;
132+
ensure!(
133+
nested_modules.is_empty(),
134+
"Classes cannot contain nested modules"
135+
);
136+
ensure!(
137+
nested_classes.is_empty(),
138+
"Nested classes are not supported yet"
139+
);
140+
// We sort methods to get a stable output
141+
methods.sort_by(|l, r| match l.name.cmp(&r.name) {
142+
Ordering::Equal => {
143+
// We put the getter before the setter
144+
if l.decorators.iter().any(|d| d == "property") {
145+
Ordering::Less
146+
} else if r.decorators.iter().any(|d| d == "property") {
147+
Ordering::Greater
148+
} else {
149+
// We pick an ordering based on decorators
150+
l.decorators.cmp(&r.decorators)
151+
}
152+
}
153+
o => o,
154+
});
155+
Ok(Class {
156+
name: name.into(),
157+
methods,
158+
})
159+
}
160+
161+
fn convert_function(name: &str, arguments: &ChunkArguments, decorators: &[String]) -> Function {
162+
Function {
163+
name: name.into(),
164+
decorators: decorators.to_vec(),
165+
arguments: Arguments {
166+
positional_only_arguments: arguments.posonlyargs.iter().map(convert_argument).collect(),
167+
arguments: arguments.args.iter().map(convert_argument).collect(),
168+
vararg: arguments
169+
.vararg
170+
.as_ref()
171+
.map(convert_variable_length_argument),
172+
keyword_only_arguments: arguments.kwonlyargs.iter().map(convert_argument).collect(),
173+
kwarg: arguments
174+
.kwarg
175+
.as_ref()
176+
.map(convert_variable_length_argument),
177+
},
178+
}
179+
}
180+
168181
fn convert_argument(arg: &ChunkArgument) -> Argument {
169182
Argument {
170183
name: arg.name.clone(),

pyo3-macros-backend/src/introspection.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn function_introspection_code(
110110
if let Some(parent) = parent {
111111
desc.insert(
112112
"parent",
113-
IntrospectionNode::IntrospectionId(Some(parent.clone())),
113+
IntrospectionNode::IntrospectionId(Some(Cow::Borrowed(parent))),
114114
);
115115
}
116116
IntrospectionNode::Map(desc).emit(pyo3_crate_path)
@@ -219,7 +219,7 @@ fn argument_introspection_data<'a>(
219219

220220
enum IntrospectionNode<'a> {
221221
String(Cow<'a, str>),
222-
IntrospectionId(Option<Type>),
222+
IntrospectionId(Option<Cow<'a, Type>>),
223223
Map(HashMap<&'static str, IntrospectionNode<'a>>),
224224
List(Vec<IntrospectionNode<'a>>),
225225
}
@@ -369,10 +369,12 @@ fn unique_element_id() -> u64 {
369369
hasher.finish()
370370
}
371371

372-
fn ident_to_type(ident: &Ident) -> Type {
373-
TypePath {
374-
path: ident.clone().into(),
375-
qself: None,
376-
}
377-
.into()
372+
fn ident_to_type(ident: &Ident) -> Cow<'static, Type> {
373+
Cow::Owned(
374+
TypePath {
375+
path: ident.clone().into(),
376+
qself: None,
377+
}
378+
.into(),
379+
)
378380
}

pyo3-macros-backend/src/pymethod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ impl<'a> PyMethod<'a> {
165165
meth_attrs: &mut Vec<syn::Attribute>,
166166
options: PyFunctionOptions,
167167
) -> Result<Self> {
168-
ensure_function_options_valid(&options)?;
169168
check_generic(sig)?;
169+
ensure_function_options_valid(&options)?;
170170
let spec = FnSpec::parse(sig, meth_attrs, options)?;
171171

172172
let method_name = spec.python_name.to_string();

0 commit comments

Comments
 (0)