Skip to content

Commit 2901464

Browse files
committed
address review: literal default rendering and fstat supports sets
- Literal defaults are rendered by python_literal_repr, which recurses through negation, parentheses and groups, so `-(1)` renders `-1`. - Float literals without a dot or exponent, such as `1f64`, render with a trailing `.0`. - os.fstat is removed from supports_fd, supports_dir_fd and supports_follow_symlinks. Assisted-by: Claude:claude-opus-5-5
1 parent 73c4609 commit 2901464

2 files changed

Lines changed: 39 additions & 35 deletions

File tree

‎crates/derive-impl/src/from_args.rs‎

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -248,43 +248,47 @@ fn python_default_repr(default: Option<&DefaultValue>, py_default: Option<&str>)
248248
if let Some(py_default) = py_default {
249249
return Some(py_default.to_owned());
250250
}
251-
match default {
252-
None => None,
253-
Some(None) => Some("<unrepresentable>".to_owned()),
254-
Some(Some(expr)) => match expr {
255-
Expr::Lit(syn::ExprLit { lit, .. }) => match lit {
256-
Lit::Bool(b) => Some(if b.value {
257-
"True".to_owned()
251+
let expr = default?.as_ref();
252+
Some(
253+
expr.and_then(python_literal_repr)
254+
.unwrap_or_else(|| "<unrepresentable>".to_owned()),
255+
)
256+
}
257+
258+
/// Python source for a Rust literal default, or `None` when it has none.
259+
fn python_literal_repr(expr: &Expr) -> Option<String> {
260+
match expr {
261+
Expr::Lit(syn::ExprLit { lit, .. }) => match lit {
262+
Lit::Bool(b) => Some(if b.value { "True" } else { "False" }.to_owned()),
263+
Lit::Int(i) => Some(i.base10_digits().to_owned()),
264+
// `1f64` has digits `1`; keep it a float.
265+
Lit::Float(f) => {
266+
let digits = f.base10_digits();
267+
Some(if digits.contains(['.', 'e', 'E']) {
268+
digits.to_owned()
258269
} else {
259-
"False".to_owned()
260-
}),
261-
Lit::Int(i) => Some(i.base10_digits().to_owned()),
262-
Lit::Float(f) => Some(f.base10_digits().to_owned()),
263-
Lit::Str(s) => Some(python_str_repr(&s.value())),
264-
_ => Some("<unrepresentable>".to_owned()),
265-
},
266-
Expr::Path(path) if path.qself.is_none() && path.path.is_ident("None") => {
267-
Some("None".to_owned())
270+
format!("{digits}.0")
271+
})
268272
}
269-
Expr::Unary(syn::ExprUnary {
270-
op: syn::UnOp::Neg(_),
271-
expr: inner,
272-
..
273-
}) => match inner.as_ref() {
274-
Expr::Lit(syn::ExprLit {
275-
lit: Lit::Int(i), ..
276-
}) => Some(format!("-{}", i.base10_digits())),
277-
Expr::Lit(syn::ExprLit {
278-
lit: Lit::Float(f), ..
279-
}) => Some(format!("-{}", f.base10_digits())),
280-
_ => Some("<unrepresentable>".to_owned()),
281-
},
282-
Expr::Paren(syn::ExprParen { expr: inner, .. })
283-
| Expr::Group(syn::ExprGroup { expr: inner, .. }) => {
284-
python_default_repr(Some(&Some(inner.as_ref().clone())), None)
285-
}
286-
_ => Some("<unrepresentable>".to_owned()),
273+
Lit::Str(s) => Some(python_str_repr(&s.value())),
274+
_ => None,
287275
},
276+
Expr::Path(path) if path.qself.is_none() && path.path.is_ident("None") => {
277+
Some("None".to_owned())
278+
}
279+
Expr::Unary(syn::ExprUnary {
280+
op: syn::UnOp::Neg(_),
281+
expr: inner,
282+
..
283+
}) => {
284+
let inner = python_literal_repr(inner)?;
285+
inner
286+
.starts_with(|c: char| c.is_ascii_digit())
287+
.then(|| format!("-{inner}"))
288+
}
289+
Expr::Paren(syn::ExprParen { expr: inner, .. })
290+
| Expr::Group(syn::ExprGroup { expr: inner, .. }) => python_literal_repr(inner),
291+
_ => None,
288292
}
289293
}
290294

‎crates/vm/src/stdlib/os.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,7 @@ pub(super) mod _os {
21922192
SupportFunc::new("rmdir", Some(false), Some(RMDIR_DIR_FD), Some(false)),
21932193
SupportFunc::new("scandir", Some(SCANDIR_FD), Some(false), Some(false)),
21942194
SupportFunc::new("stat", Some(true), Some(STAT_DIR_FD), Some(true)),
2195-
SupportFunc::new("fstat", Some(true), Some(STAT_DIR_FD), Some(true)),
2195+
SupportFunc::new("fstat", Some(false), Some(false), Some(false)),
21962196
SupportFunc::new("symlink", Some(false), Some(SYMLINK_DIR_FD), Some(false)),
21972197
SupportFunc::new("truncate", Some(true), Some(false), Some(false)),
21982198
SupportFunc::new("ftruncate", Some(true), Some(false), Some(false)),

0 commit comments

Comments
 (0)