|
| 1 | +use ruff_diagnostics::{Diagnostic, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast as ast; |
| 4 | +use ruff_python_ast::{Expr, Operator}; |
| 5 | +use ruff_text_size::Ranged; |
| 6 | + |
| 7 | +use crate::checkers::ast::Checker; |
| 8 | + |
| 9 | +/// ## What it does |
| 10 | +/// Checks for the presence of string literals in `X | Y`-style union types. |
| 11 | +/// |
| 12 | +/// ## Why is this bad? |
| 13 | +/// [PEP 604] introduced a new syntax for union type annotations based on the |
| 14 | +/// `|` operator. |
| 15 | +/// |
| 16 | +/// While Python's type annotations can typically be wrapped in strings to |
| 17 | +/// avoid runtime evaluation, the use of a string member within an `X | Y`-style |
| 18 | +/// union type will cause a runtime error. |
| 19 | +/// |
| 20 | +/// Instead, remove the quotes, wrap the _entire_ union in quotes, or use |
| 21 | +/// `from __future__ import annotations` to disable runtime evaluation of |
| 22 | +/// annotations entirely. |
| 23 | +/// |
| 24 | +/// ## Example |
| 25 | +/// ```python |
| 26 | +/// var: str | "int" |
| 27 | +/// ``` |
| 28 | +/// |
| 29 | +/// Use instead: |
| 30 | +/// ```python |
| 31 | +/// var: str | int |
| 32 | +/// ``` |
| 33 | +/// |
| 34 | +/// Or, extend the quotes to include the entire union: |
| 35 | +/// ```python |
| 36 | +/// var: "str | int" |
| 37 | +/// ``` |
| 38 | +/// |
| 39 | +/// ## References |
| 40 | +/// - [PEP 535](https://peps.python.org/pep-0563/) |
| 41 | +/// - [PEP 604](https://peps.python.org/pep-0604/) |
| 42 | +/// |
| 43 | +/// [PEP 604]: https://peps.python.org/pep-0604/ |
| 44 | +#[violation] |
| 45 | +pub struct RuntimeStringUnion; |
| 46 | + |
| 47 | +impl Violation for RuntimeStringUnion { |
| 48 | + #[derive_message_formats] |
| 49 | + fn message(&self) -> String { |
| 50 | + format!("Invalid string member in `X | Y`-style union type") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// TCH006 |
| 55 | +pub(crate) fn runtime_string_union(checker: &mut Checker, expr: &Expr) { |
| 56 | + if !checker.semantic().in_type_definition() { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if !checker.semantic().execution_context().is_runtime() { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Search for strings within the binary operator. |
| 65 | + let mut strings = Vec::new(); |
| 66 | + traverse_op(expr, &mut strings); |
| 67 | + |
| 68 | + for string in strings { |
| 69 | + checker |
| 70 | + .diagnostics |
| 71 | + .push(Diagnostic::new(RuntimeStringUnion, string.range())); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Collect all string members in possibly-nested binary `|` expressions. |
| 76 | +fn traverse_op<'a>(expr: &'a Expr, strings: &mut Vec<&'a Expr>) { |
| 77 | + match expr { |
| 78 | + Expr::StringLiteral(_) => { |
| 79 | + strings.push(expr); |
| 80 | + } |
| 81 | + Expr::BytesLiteral(_) => { |
| 82 | + strings.push(expr); |
| 83 | + } |
| 84 | + Expr::BinOp(ast::ExprBinOp { |
| 85 | + left, |
| 86 | + right, |
| 87 | + op: Operator::BitOr, |
| 88 | + .. |
| 89 | + }) => { |
| 90 | + traverse_op(left, strings); |
| 91 | + traverse_op(right, strings); |
| 92 | + } |
| 93 | + _ => {} |
| 94 | + } |
| 95 | +} |
0 commit comments