Skip to content

Conversation

thesayyn
Copy link

Currently there is no way to read positional arguments for type that implement the invoke function from the StarlarkValue trait.

Copy link

meta-cla bot commented Aug 18, 2025

Hi @thesayyn!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 19, 2025
Copy link

meta-cla bot commented Aug 19, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@thesayyn
Copy link
Author

thesayyn commented Aug 19, 2025

Current workaround is to do something like this to get args as a tuple.

        let __args: [_; 1usize] = starlark::__derive_refs::parse_args::parse_signature(
            &starlark::__derive_refs::sig::parameter_spec("get_memory", &[], &[], true, &[], false),
            args,
            eval.heap(),
        )?;
        let positionals: UnpackList<Value<'v>> =
            starlark::__derive_refs::parse_args::check_required("args", __args[0usize])?;

@linabutler
Copy link

I had to do a bit of spelunking in starlark-rust and buck2 to find this, but you can use ParametersSpec to extract positionals from an Arguments<'v>.

Here's a snippet from one of my custom types that implements StarlarkValue::invoke():

use starlark::eval::{Arguments, Evaluator, ParametersSpec, ParametersSpecParam};

#[starlark_value(type = FUNCTION_TYPE)]
impl<'v> StarlarkValue<'v> for RequestWithMethod {
    fn invoke(
        &self,
        _this: Value<'v>,
        arguments: &Arguments<'v, '_>,
        eval: &mut Evaluator<'v, '_, '_>,
    ) -> starlark::Result<Value<'v>> {
        let spec = ParametersSpec::<Value<'v>>::new_parts(
            "request", // Function name.
            [
                // Positional-only arguments, equivalent to
                // `#[starlark(require = named)]`.
            ],
            [
                // Positional or named arguments.
                ("url", ParametersSpecParam::Required),
            ],
            false, // Whether to accept `*args`.
            [
                // Named-only arguments, equivalent to annotating a param with
                // `#[starlark(require = named)]` in a `#[starlark_module]`.
                ("headers", ParametersSpecParam::Optional),
                ("body", ParametersSpecParam::Optional),
            ],
            false, // Whether to accept `**kwargs`.
        );

        let (url, headers, body) = spec.parser(arguments, eval, |parser, _eval| {
            // Unpacking a required argument.
            let url = parser.next::<UnpackUrl<'_>>()?;
            // Unpacking optional arguments.
            let headers = parser.next_opt::<UnpackHeaders<'_>>()?.unwrap_or_default();
            let body = parser
                .next_opt::<UnpackBytes<'_>>()?;
            Ok((url, headers, body))
        })?;

        Ok(eval.heap().alloc(Request {
            method: self.method,
            url,
            headers,
            body,
        }))
    }
}

@linabutler
Copy link

(It would be lovely to be able to use arguments.positional() directly, though; I was just sharing my workaround because I ran into the same thing you did! 😊)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants