[ruff] Implement falsy-dict-get-fallback (RUF056) (#15160)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Arnav Gupta 2024-12-31 05:40:51 -05:00 committed by GitHub
parent 68d2466832
commit 3c9021ffcb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 897 additions and 88 deletions

View file

@ -3780,6 +3780,15 @@ pub enum ArgOrKeyword<'a> {
Keyword(&'a Keyword),
}
impl<'a> ArgOrKeyword<'a> {
pub const fn value(&self) -> &'a Expr {
match self {
ArgOrKeyword::Arg(argument) => argument,
ArgOrKeyword::Keyword(keyword) => &keyword.value,
}
}
}
impl<'a> From<&'a Expr> for ArgOrKeyword<'a> {
fn from(arg: &'a Expr) -> Self {
Self::Arg(arg)
@ -3828,15 +3837,24 @@ impl Arguments {
.nth(position)
}
/// 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
/// 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.
pub fn find_argument(&self, name: &str, position: usize) -> Option<&Expr> {
pub fn find_argument_value(&self, name: &str, position: usize) -> Option<&Expr> {
self.find_keyword(name)
.map(|keyword| &keyword.value)
.or_else(|| self.find_positional(position))
}
/// Return the 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.
pub fn find_argument(&self, name: &str, position: usize) -> Option<ArgOrKeyword> {
self.find_keyword(name)
.map(ArgOrKeyword::from)
.or_else(|| self.find_positional(position).map(ArgOrKeyword::from))
}
/// Return the positional and keyword arguments in the order of declaration.
///
/// Positional arguments are generally before keyword arguments, but star arguments are an