Skip to main content

Arguments

Struct Arguments 

Source
pub struct Arguments {
    pub range: TextRange,
    pub node_index: AtomicNodeIndex,
    pub args: Box<[Expr]>,
    pub keywords: Box<[Keyword]>,
}
Expand description

An AST node used to represent the arguments passed to a function call or class definition.

For example, given:

foo(1, 2, 3, bar=4, baz=5)

The Arguments node would span from the left to right parentheses (inclusive), and contain the arguments and keyword arguments in the order they appear in the source code.

Similarly, given:

class Foo(Bar, baz=1, qux=2):
    pass

The Arguments node would again span from the left to right parentheses (inclusive), and contain the Bar argument and the baz and qux keyword arguments in the order they appear in the source code.

In the context of a class definition, the Python-style AST refers to the arguments as bases, as they represent the “explicitly specified base classes”, while the keyword arguments are typically used for metaclass, with any additional arguments being passed to the metaclass.

Fields§

§range: TextRange§node_index: AtomicNodeIndex§args: Box<[Expr]>§keywords: Box<[Keyword]>

Implementations§

Source§

impl Arguments

Source

pub fn len(&self) -> usize

Return the number of positional and keyword arguments.

Source

pub fn is_empty(&self) -> bool

Return true if there are no positional or keyword arguments.

Source

pub fn find_keyword(&self, keyword_name: &str) -> Option<&Keyword>

Return the Keyword with the given name, or None if no such Keyword exists.

Source

pub fn find_positional(&self, position: usize) -> Option<&Expr>

Return the positional argument at the given index, or None if no such argument exists.

Source

pub fn find_argument_value(&self, name: &str, position: usize) -> Option<&Expr>

Return the value for the argument with the given name or at the given position, or None if no such argument exists. Used to retrieve argument values that can be provided either as keyword or positional arguments.

Source

pub fn find_argument( &self, name: &str, position: usize, ) -> Option<ArgOrKeyword<'_>>

Return the argument with the given name or at the given position, or None if no such argument exists. Used to retrieve arguments that can be provided either as keyword or positional arguments.

Source

pub fn arguments_source_order(&self) -> ArgumentsSourceOrder<'_>

Return the positional and keyword arguments in the order of declaration.

Positional arguments are generally before keyword arguments, but star arguments are an exception:

class A(*args, a=2, *args2, **kwargs):
    pass

f(*args, a=2, *args2, **kwargs)

where *args and args2 are args while a=1 and kwargs are keywords.

If you would just chain args and keywords the call would get reordered which we don’t want. This function instead “merge sorts” them into the correct order.

Note that the order of evaluation is always first args, then keywords:

def f(*args, **kwargs):
    pass

def g(x):
    print(x)
    return x


f(*g([1]), a=g(2), *g([3]), **g({"4": 5}))

Output:

[1]
[3]
2
{'4': 5}
Source

pub fn inner_range(&self) -> TextRange

Source

pub fn l_paren_range(&self) -> TextRange

Source

pub fn r_paren_range(&self) -> TextRange

Trait Implementations§

Source§

impl Clone for Arguments

Source§

fn clone(&self) -> Arguments

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Arguments

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a Arguments> for AnyNodeRef<'a>

Source§

fn from(node: &'a Arguments) -> AnyNodeRef<'a>

Converts to this type from the input type.
Source§

impl<'a> From<&'a Arguments> for AnyRootNodeRef<'a>

Source§

fn from(node: &'a Arguments) -> AnyRootNodeRef<'a>

Converts to this type from the input type.
Source§

impl<'a> From<&'a Arguments> for ComparableArguments<'a>

Source§

fn from(arguments: &'a Arguments) -> Self

Converts to this type from the input type.
Source§

impl HasNodeIndex for Arguments

Source§

fn node_index(&self) -> &AtomicNodeIndex

Returns the AtomicNodeIndex for this node.
Source§

impl PartialEq for Arguments

Source§

fn eq(&self, other: &Arguments) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Ranged for Arguments

Source§

fn range(&self) -> TextRange

The range of this item in the source text.
Source§

fn start(&self) -> TextSize

The start offset of this item in the source text.
Source§

fn end(&self) -> TextSize

The end offset of this item in the source text.
Source§

impl<'a> TryFrom<AnyRootNodeRef<'a>> for &'a Arguments

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(node: AnyRootNodeRef<'a>) -> Result<&'a Arguments, ()>

Performs the conversion.
Source§

impl StructuralPartialEq for Arguments

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.