-
Notifications
You must be signed in to change notification settings - Fork 900
/
redundant_literal_union.rs
164 lines (149 loc) · 5.28 KB
/
redundant_literal_union.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::fmt;
use rustc_hash::FxHashSet;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{self as ast, Expr, LiteralExpressionRef};
use ruff_python_semantic::analyze::typing::traverse_union;
use ruff_python_semantic::SemanticModel;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::fix::snippet::SourceCodeSnippet;
/// ## What it does
/// Checks for redundant unions between a `Literal` and a builtin supertype of
/// that `Literal`.
///
/// ## Why is this bad?
/// Using a `Literal` type in a union with its builtin supertype is redundant,
/// as the supertype will be strictly more general than the `Literal` type.
/// For example, `Literal["A"] | str` is equivalent to `str`, and
/// `Literal[1] | int` is equivalent to `int`, as `str` and `int` are the
/// supertypes of `"A"` and `1` respectively.
///
/// ## Example
/// ```python
/// from typing import Literal
///
/// x: Literal["A", b"B"] | str
/// ```
///
/// Use instead:
/// ```python
/// from typing import Literal
///
/// x: Literal[b"B"] | str
/// ```
#[violation]
pub struct RedundantLiteralUnion {
literal: SourceCodeSnippet,
builtin_type: ExprType,
}
impl Violation for RedundantLiteralUnion {
#[derive_message_formats]
fn message(&self) -> String {
let RedundantLiteralUnion {
literal,
builtin_type,
} = self;
if let Some(literal) = literal.full_display() {
format!("`Literal[{literal}]` is redundant in a union with `{builtin_type}`")
} else {
format!("`Literal` is redundant in a union with `{builtin_type}`")
}
}
}
/// PYI051
pub(crate) fn redundant_literal_union<'a>(checker: &mut Checker, union: &'a Expr) {
let mut typing_literal_exprs = Vec::new();
let mut builtin_types_in_union = FxHashSet::default();
// Adds a member to `literal_exprs` for each value in a `Literal`, and any builtin types
// to `builtin_types_in_union`.
let mut func = |expr: &'a Expr, _parent: &'a Expr| {
if let Expr::Subscript(ast::ExprSubscript { value, slice, .. }) = expr {
if checker.semantic().match_typing_expr(value, "Literal") {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
typing_literal_exprs.extend(elts.iter());
} else {
typing_literal_exprs.push(slice);
}
}
return;
}
let Some(builtin_type) = match_builtin_type(expr, checker.semantic()) else {
return;
};
builtin_types_in_union.insert(builtin_type);
};
traverse_union(&mut func, checker.semantic(), union);
for typing_literal_expr in typing_literal_exprs {
let Some(literal_type) = match_literal_type(typing_literal_expr) else {
continue;
};
if builtin_types_in_union.contains(&literal_type) {
checker.diagnostics.push(Diagnostic::new(
RedundantLiteralUnion {
literal: SourceCodeSnippet::from_str(
checker.locator().slice(typing_literal_expr),
),
builtin_type: literal_type,
},
typing_literal_expr.range(),
));
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
enum ExprType {
Int,
Str,
Bool,
Float,
Bytes,
Complex,
}
impl fmt::Display for ExprType {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Int => fmt.write_str("int"),
Self::Str => fmt.write_str("str"),
Self::Bool => fmt.write_str("bool"),
Self::Float => fmt.write_str("float"),
Self::Bytes => fmt.write_str("bytes"),
Self::Complex => fmt.write_str("complex"),
}
}
}
/// Return the [`ExprType`] of an [`Expr]` if it is a builtin type (e.g. `int`, `bool`, `float`,
/// `str`, `bytes`, or `complex`).
fn match_builtin_type(expr: &Expr, semantic: &SemanticModel) -> Option<ExprType> {
let name = expr.as_name_expr()?;
let result = match name.id.as_str() {
"int" => ExprType::Int,
"bool" => ExprType::Bool,
"str" => ExprType::Str,
"float" => ExprType::Float,
"bytes" => ExprType::Bytes,
"complex" => ExprType::Complex,
_ => return None,
};
if !semantic.is_builtin(name.id.as_str()) {
return None;
}
Some(result)
}
/// Return the [`ExprType`] of an [`Expr`] if it is a literal (e.g., an `int`, like `1`, or a
/// `bool`, like `True`).
fn match_literal_type(expr: &Expr) -> Option<ExprType> {
Some(match expr.as_literal_expr()? {
LiteralExpressionRef::BooleanLiteral(_) => ExprType::Bool,
LiteralExpressionRef::StringLiteral(_) => ExprType::Str,
LiteralExpressionRef::BytesLiteral(_) => ExprType::Bytes,
LiteralExpressionRef::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value {
ast::Number::Int(_) => ExprType::Int,
ast::Number::Float(_) => ExprType::Float,
ast::Number::Complex { .. } => ExprType::Complex,
},
LiteralExpressionRef::NoneLiteral(_) | LiteralExpressionRef::EllipsisLiteral(_) => {
return None;
}
})
}