Make the FromArgs derive less verbose · RustPython/RustPython@acd0bf5 · GitHub
Skip to content

Commit acd0bf5

Browse files
committed
Make the FromArgs derive less verbose
1 parent f8cb2f3 commit acd0bf5

27 files changed

Lines changed: 170 additions & 180 deletions

derive/src/from_args.rs

Lines changed: 15 additions & 42 deletions

vm/src/anystr.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ where
1515
S: ?Sized + AnyStr<'a, E>,
1616
E: Copy,
1717
{
18-
#[pyarg(positional_or_keyword, default = "None")]
18+
#[pyarg(any, default)]
1919
sep: Option<T>,
20-
#[pyarg(positional_or_keyword, default = "-1")]
20+
#[pyarg(any, default = "-1")]
2121
maxsplit: isize,
2222
_phantom1: std::marker::PhantomData<&'a S>,
2323
_phantom2: std::marker::PhantomData<E>,
@@ -45,13 +45,13 @@ where
4545

4646
#[derive(FromArgs)]
4747
pub struct SplitLinesArgs {
48-
#[pyarg(positional_or_keyword, default = "false")]
48+
#[pyarg(any, default = "false")]
4949
pub keepends: bool,
5050
}
5151

5252
#[derive(FromArgs)]
5353
pub struct ExpandTabsArgs {
54-
#[pyarg(positional_or_keyword, default = "8")]
54+
#[pyarg(any, default = "8")]
5555
tabsize: isize,
5656
}
5757

@@ -63,11 +63,11 @@ impl ExpandTabsArgs {
6363

6464
#[derive(FromArgs)]
6565
pub struct StartsEndsWithArgs {
66-
#[pyarg(positional_only, optional = false)]
66+
#[pyarg(positional)]
6767
affix: PyObjectRef,
68-
#[pyarg(positional_only, default = "None")]
68+
#[pyarg(positional, default)]
6969
start: Option<PyIntRef>,
70-
#[pyarg(positional_only, default = "None")]
70+
#[pyarg(positional, default)]
7171
end: Option<PyIntRef>,
7272
}
7373

vm/src/builtins.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ mod decl {
107107
#[derive(FromArgs)]
108108
#[allow(dead_code)]
109109
struct CompileArgs {
110-
#[pyarg(positional_only, optional = false)]
110+
#[pyarg(positional)]
111111
source: Either<PyStrRef, PyBytesRef>,
112-
#[pyarg(positional_only, optional = false)]
112+
#[pyarg(positional)]
113113
filename: PyStrRef,
114-
#[pyarg(positional_only, optional = false)]
114+
#[pyarg(positional)]
115115
mode: PyStrRef,
116-
#[pyarg(positional_or_keyword, optional = true)]
116+
#[pyarg(any, optional)]
117117
flags: OptionalArg<PyIntRef>,
118-
#[pyarg(positional_or_keyword, optional = true)]
118+
#[pyarg(any, optional)]
119119
dont_inherit: OptionalArg<bool>,
120-
#[pyarg(positional_or_keyword, optional = true)]
120+
#[pyarg(any, optional)]
121121
optimize: OptionalArg<PyIntRef>,
122122
}
123123

@@ -188,10 +188,10 @@ mod decl {
188188
#[cfg(feature = "rustpython-compiler")]
189189
#[derive(FromArgs)]
190190
struct ScopeArgs {
191-
#[pyarg(positional_or_keyword, default = "None")]
191+
#[pyarg(any, default)]
192192
globals: Option<PyDictRef>,
193193
// TODO: support any mapping for `locals`
194-
#[pyarg(positional_or_keyword, default = "None")]
194+
#[pyarg(any, default)]
195195
locals: Option<PyDictRef>,
196196
}
197197

@@ -669,13 +669,13 @@ mod decl {
669669

670670
#[derive(Debug, Default, FromArgs)]
671671
pub struct PrintOptions {
672-
#[pyarg(keyword_only, default = "None")]
672+
#[pyarg(named, default)]
673673
sep: Option<PyStrRef>,
674-
#[pyarg(keyword_only, default = "None")]
674+
#[pyarg(named, default)]
675675
end: Option<PyStrRef>,
676-
#[pyarg(keyword_only, default = "IntoPyBool::FALSE")]
676+
#[pyarg(named, default = "IntoPyBool::FALSE")]
677677
flush: IntoPyBool,
678-
#[pyarg(keyword_only, default = "None")]
678+
#[pyarg(named, default)]
679679
file: Option<PyObjectRef>,
680680
}
681681

vm/src/bytesinner.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ impl TryFromObject for PyBytesInner {
6464

6565
#[derive(FromArgs)]
6666
pub struct ByteInnerNewOptions {
67-
#[pyarg(positional_or_keyword, optional = true)]
67+
#[pyarg(any, optional)]
6868
source: OptionalArg<PyObjectRef>,
69-
#[pyarg(positional_or_keyword, optional = true)]
69+
#[pyarg(any, optional)]
7070
encoding: OptionalArg<PyStrRef>,
71-
#[pyarg(positional_or_keyword, optional = true)]
71+
#[pyarg(any, optional)]
7272
errors: OptionalArg<PyStrRef>,
7373
}
7474

@@ -155,11 +155,11 @@ impl ByteInnerNewOptions {
155155

156156
#[derive(FromArgs)]
157157
pub struct ByteInnerFindOptions {
158-
#[pyarg(positional_only, optional = false)]
158+
#[pyarg(positional)]
159159
sub: Either<PyBytesInner, PyIntRef>,
160-
#[pyarg(positional_only, default = "None")]
160+
#[pyarg(positional, default)]
161161
start: Option<PyIntRef>,
162-
#[pyarg(positional_only, default = "None")]
162+
#[pyarg(positional, default)]
163163
end: Option<PyIntRef>,
164164
}
165165

@@ -180,9 +180,9 @@ impl ByteInnerFindOptions {
180180

181181
#[derive(FromArgs)]
182182
pub struct ByteInnerPaddingOptions {
183-
#[pyarg(positional_only, optional = false)]
183+
#[pyarg(positional)]
184184
width: isize,
185-
#[pyarg(positional_only, optional = true)]
185+
#[pyarg(positional, optional)]
186186
fillchar: OptionalArg<PyObjectRef>,
187187
}
188188

@@ -207,9 +207,9 @@ impl ByteInnerPaddingOptions {
207207

208208
#[derive(FromArgs)]
209209
pub struct ByteInnerTranslateOptions {
210-
#[pyarg(positional_only, optional = false)]
210+
#[pyarg(positional)]
211211
table: Either<PyBytesInner, PyNoneRef>,
212-
#[pyarg(positional_or_keyword, optional = true)]
212+
#[pyarg(any, optional)]
213213
delete: OptionalArg<PyBytesInner>,
214214
}
215215

@@ -1190,9 +1190,9 @@ impl<'s> AnyStr<'s, u8> for [u8] {
11901190

11911191
#[derive(FromArgs)]
11921192
pub struct DecodeArgs {
1193-
#[pyarg(positional_or_keyword, default = "None")]
1193+
#[pyarg(any, default)]
11941194
encoding: Option<PyStrRef>,
1195-
#[pyarg(positional_or_keyword, default = "None")]
1195+
#[pyarg(any, default)]
11961196
errors: Option<PyStrRef>,
11971197
}
11981198

vm/src/function.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ pub trait FromArgs: Sized {
244244
fn from_args(vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError>;
245245
}
246246

247+
pub trait FromArgOptional {
248+
type Inner: TryFromObject;
249+
fn from_inner(x: Self::Inner) -> Self;
250+
}
251+
impl<T: TryFromObject> FromArgOptional for OptionalArg<T> {
252+
type Inner = T;
253+
fn from_inner(x: T) -> Self {
254+
Self::Present(x)
255+
}
256+
}
257+
impl<T: TryFromObject> FromArgOptional for T {
258+
type Inner = Self;
259+
fn from_inner(x: Self) -> Self {
260+
x
261+
}
262+
}
263+
247264
/// A map of keyword arguments to their values.
248265
///
249266
/// A built-in function with a `KwArgs` parameter is analagous to a Python

vm/src/obj/objcomplex.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ impl Hashable for PyComplex {
308308

309309
#[derive(FromArgs)]
310310
struct ComplexArgs {
311-
#[pyarg(positional_or_keyword, default = "None")]
311+
#[pyarg(any, default)]
312312
real: Option<PyObjectRef>,
313-
#[pyarg(positional_or_keyword, default = "None")]
313+
#[pyarg(any, default)]
314314
imag: Option<PyObjectRef>,
315315
}
316316

vm/src/obj/objint.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -691,29 +691,29 @@ impl Hashable for PyInt {
691691

692692
#[derive(FromArgs)]
693693
struct IntOptions {
694-
#[pyarg(positional_only, optional = true)]
694+
#[pyarg(positional, optional)]
695695
val_options: OptionalArg<PyObjectRef>,
696-
#[pyarg(positional_or_keyword, optional = true)]
696+
#[pyarg(any, optional)]
697697
base: OptionalArg<PyObjectRef>,
698698
}
699699

700700
#[derive(FromArgs)]
701701
struct IntFromByteArgs {
702-
#[pyarg(positional_or_keyword)]
702+
#[pyarg(any)]
703703
bytes: PyBytesInner,
704-
#[pyarg(positional_or_keyword)]
704+
#[pyarg(any)]
705705
byteorder: PyStrRef,
706-
#[pyarg(keyword_only, optional = true)]
706+
#[pyarg(named, optional)]
707707
signed: OptionalArg<IntoPyBool>,
708708
}
709709

710710
#[derive(FromArgs)]
711711
struct IntToByteArgs {
712-
#[pyarg(positional_or_keyword)]
712+
#[pyarg(any)]
713713
length: PyIntRef,
714-
#[pyarg(positional_or_keyword)]
714+
#[pyarg(any)]
715715
byteorder: PyStrRef,
716-
#[pyarg(keyword_only, optional = true)]
716+
#[pyarg(named, optional)]
717717
signed: OptionalArg<IntoPyBool>,
718718
}
719719

vm/src/obj/objlist.rs

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)