Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 26, 2024
1 parent abab20b commit c135320
Showing 1 changed file with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::checkers::ast::Checker;
use crate::settings::types::PythonVersion;

/// ## What it does
/// Checks for use of `TypeAlias` annotation or `TypeAliasType` assignment for declaring type
/// aliases.
/// Checks for use of `TypeAlias` annotations and `TypeAliasType` assignments
/// for declaring type aliases.
///
/// ## Why is this bad?
/// The `type` keyword was introduced in Python 3.12 by [PEP 695] for defining
Expand Down Expand Up @@ -88,21 +88,26 @@ pub(crate) fn non_pep695_type_alias_type(checker: &mut Checker, stmt: &StmtAssig
}

let StmtAssign { targets, value, .. } = stmt;

let Expr::Call(ExprCall {
func, arguments, ..
}) = value.as_ref()
else {
return;
};

let [Expr::Name(target_name)] = targets.as_slice() else {
return;
};

let [Expr::StringLiteral(name), value] = arguments.args.as_ref() else {
return;
};

if name.value.to_str() != target_name.id {
return;
}

let type_params = match arguments.keywords.as_ref() {
[] => &[],
[Keyword {
Expand Down Expand Up @@ -130,35 +135,35 @@ pub(crate) fn non_pep695_type_alias_type(checker: &mut Checker, stmt: &StmtAssig
})
})
})
.collect()
.collect::<Option<Vec<_>>>()
else {
return;
};
checker.diagnostics.push(get_diagnostic(

checker.diagnostics.push(create_diagnostic(
checker.generator(),
stmt.range(),
target_name.id.clone(),
Box::new(value.clone()),
vars,
&target_name.id,
&value,
&vars,
Applicability::Safe,
TypeAliasKind::TypeAliasType,
));
}

/// UP040
pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign) {
if checker.settings.target_version < PythonVersion::Py312 {
return;
}

let StmtAnnAssign {
target,
annotation,
value,
..
} = stmt;

// Syntax only available in 3.12+
if checker.settings.target_version < PythonVersion::Py312 {
return;
}

if !checker
.semantic()
.match_typing_expr(annotation, "TypeAlias")
Expand Down Expand Up @@ -191,12 +196,12 @@ pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign)
.unique_by(|TypeVar { name, .. }| name.id.as_str())
.collect::<Vec<_>>();

checker.diagnostics.push(get_diagnostic(
checker.diagnostics.push(create_diagnostic(
checker.generator(),
stmt.range(),
name.clone(),
value.clone(),
vars,
&name,
&value,
&vars,
// The fix is only safe in a type stub because new-style aliases have different runtime behavior
// See https://github.com/astral-sh/ruff/issues/6434
if checker.source_type.is_stub() {
Expand All @@ -208,12 +213,13 @@ pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign)
));
}

fn get_diagnostic(
/// Generate a [`Diagnostic`] for a non-PEP 695 type alias or type alias type.
fn create_diagnostic(
generator: Generator,
stmt_range: TextRange,
name: String,
value: Box<Expr>,
vars: Vec<TypeVar>,
name: &str,
value: &Expr,
vars: &[TypeVar],
applicability: Applicability,
type_alias_kind: TypeAliasKind,
) -> Diagnostic {
Expand All @@ -229,11 +235,16 @@ fn get_diagnostic(
range: TextRange::default(),
name: Identifier::new(name.id.clone(), TextRange::default()),
bound: match restriction {
Some(TypeVarRestriction::Bound(bound)) => Some(Box::new(bound.clone())),
Some(TypeVarRestriction::Bound(bound)) => {
Some(Box::new((*bound).clone()))
}
Some(TypeVarRestriction::Constraint(constraints)) => {
Some(Box::new(Expr::Tuple(ast::ExprTuple {
range: TextRange::default(),
elts: constraints.into_iter().cloned().collect(),
elts: constraints
.into_iter()
.map(|expr| (*expr).clone())
.collect(),
ctx: ast::ExprContext::Load,
parenthesized: true,
})))
Expand All @@ -251,7 +262,7 @@ fn get_diagnostic(

Diagnostic::new(
NonPEP695TypeAlias {
name: name.clone(),
name: name.to_string(),
type_alias_kind,
},
stmt_range,
Expand All @@ -262,11 +273,11 @@ fn get_diagnostic(
range: TextRange::default(),
name: Box::new(Expr::Name(ExprName {
range: TextRange::default(),
id: name,
id: name.to_string(),
ctx: ast::ExprContext::Load,
})),
type_params,
value,
value: Box::new(value.clone()),
})),
stmt_range,
),
Expand Down

0 comments on commit c135320

Please sign in to comment.