Skip to main content

python_ast/ast/tree/
starred.rs

1use proc_macro2::TokenStream;
2use pyo3::{Borrowed, FromPyObject, PyAny, PyResult, prelude::PyAnyMethods, types::PyTypeMethods};
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    CodeGen, CodeGenContext, ExprType, Node, PythonOptions, SymbolTableScopes,
7    PyAttributeExtractor,
8};
9
10/// Starred expression for unpacking (*args)
11#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
12pub struct Starred {
13    /// The expression being unpacked
14    pub value: Box<ExprType>,
15    /// Context (Load, Store, etc.) - not used in Rust generation
16    pub ctx: Option<String>,
17    /// Position information
18    pub lineno: Option<usize>,
19    pub col_offset: Option<usize>,
20    pub end_lineno: Option<usize>,
21    pub end_col_offset: Option<usize>,
22}
23
24impl<'a, 'py> FromPyObject<'a, 'py> for Starred {
25    type Error = pyo3::PyErr;
26    fn extract(ob: Borrowed<'a, 'py, PyAny>) -> PyResult<Self> {
27        // Extract the value being starred
28        let value = ob.extract_attr_with_context("value", "starred expression value")?;
29        let value: ExprType = value.extract()?;
30        
31        // Extract context (Load, Store, etc.) - optional
32        let ctx = ob.getattr("ctx").ok().and_then(|ctx_obj| {
33            ctx_obj.get_type().name().ok().and_then(|name| name.extract().ok())
34        });
35        
36        Ok(Starred {
37            value: Box::new(value),
38            ctx,
39            lineno: ob.lineno(),
40            col_offset: ob.col_offset(),
41            end_lineno: ob.end_lineno(),
42            end_col_offset: ob.end_col_offset(),
43        })
44    }
45}
46
47impl Node for Starred {
48    fn lineno(&self) -> Option<usize> { self.lineno }
49    fn col_offset(&self) -> Option<usize> { self.col_offset }
50    fn end_lineno(&self) -> Option<usize> { self.end_lineno }
51    fn end_col_offset(&self) -> Option<usize> { self.end_col_offset }
52}
53
54impl CodeGen for Starred {
55    type Context = CodeGenContext;
56    type Options = PythonOptions;
57    type SymbolTable = SymbolTableScopes;
58
59    fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable {
60        (*self.value).clone().find_symbols(symbols)
61    }
62
63    fn to_rust(
64        self,
65        _ctx: Self::Context,
66        _options: Self::Options,
67        _symbols: Self::SymbolTable,
68    ) -> Result<TokenStream, Box<dyn std::error::Error>> {
69        // Emitting the bare value would pass the whole collection as ONE
70        // argument/element — silently different from unpacking it.
71        Err(
72            "starred unpacking (`*expr`) is not supported yet: it would \
73             silently pass the collection as a single value instead of \
74             spreading its elements. Spell the elements out explicitly."
75                .to_string()
76                .into(),
77        )
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    // Note: These tests will likely fail until full starred expression support is added
84    // create_parse_test!(test_starred_args, "*args", "test.py");
85    // create_parse_test!(test_starred_in_call, "func(*args)", "test.py");
86}