mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-17 09:00:26 +00:00
[ruff_python_ast] Add name and default functions to TypeParam. (#14964)
## Summary This change adds `name` and `default` functions to `TypeParam` to access the corresponding attributes more conveniently. I currently have these as helper functions in code built on top of ruff_python_ast, and they seemed like they might be generally useful. ## Test Plan Ran the checks listed in CONTRIBUTING.md#development. --------- Co-authored-by: Micha Reiser <micha@reiser.io> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
1389cb8e59
commit
112e9d2d82
1 changed files with 18 additions and 0 deletions
|
@ -3386,6 +3386,24 @@ pub enum TypeParam {
|
|||
TypeVarTuple(TypeParamTypeVarTuple),
|
||||
}
|
||||
|
||||
impl TypeParam {
|
||||
pub const fn name(&self) -> &Identifier {
|
||||
match self {
|
||||
Self::TypeVar(x) => &x.name,
|
||||
Self::ParamSpec(x) => &x.name,
|
||||
Self::TypeVarTuple(x) => &x.name,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default(&self) -> Option<&Expr> {
|
||||
match self {
|
||||
Self::TypeVar(x) => x.default.as_deref(),
|
||||
Self::ParamSpec(x) => x.default.as_deref(),
|
||||
Self::TypeVarTuple(x) => x.default.as_deref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [TypeVar](https://docs.python.org/3/library/ast.html#ast.TypeVar)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TypeParamTypeVar {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue