[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:
Rebecca Chen 2024-12-15 04:04:51 -08:00 committed by GitHub
parent 1389cb8e59
commit 112e9d2d82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 {