|
| 1 | +//! reverse of `crate::transforms::literal_string`: |
| 2 | +//! `LiteralString` → `literal str` |
| 3 | +//! `typing.LiteralString` → `literal str` |
| 4 | +//! |
| 5 | +//! only fires in type positions on a name spelled `LiteralString` that also |
| 6 | +//! *resolves* to `LiteralString` — the same pair of guards |
| 7 | +//! [`dynamic_keyword`](super::dynamic_keyword) uses, so an alias |
| 8 | +//! (`MyStr = LiteralString; x: MyStr`) keeps its own spelling and a shadowing |
| 9 | +//! binding is left alone. |
| 10 | +//! |
| 11 | +//! descent into nested type positions (`list[LiteralString]`, |
| 12 | +//! `LiteralString | None`, `Annotated[LiteralString, meta]` first arg) is |
| 13 | +//! delegated to [`type_expr_walker`], so metadata and `Literal[…]` slices are |
| 14 | +//! never touched. |
| 15 | +//! |
| 16 | +//! once the last reference is rewritten, `from typing import LiteralString` is |
| 17 | +//! dead and [`prune_imports`](super::prune_imports) drops it. |
| 18 | +
|
| 19 | +use ruff_diagnostics::{Edit, Fix}; |
| 20 | +use ruff_python_ast::visitor::Visitor; |
| 21 | +use ruff_python_ast::{Expr, Stmt}; |
| 22 | +use ruff_text_size::Ranged; |
| 23 | + |
| 24 | +use crate::transforms::source_util::for_each_annotation_in_stmt; |
| 25 | +use crate::transforms::type_expr_walker::{Recurse, TypeExprVisitor, TypePos, walk_one_type_expr}; |
| 26 | +use crate::type_info::TypeInfo; |
| 27 | + |
| 28 | +pub(crate) struct LiteralStringReverse<'src> { |
| 29 | + types: &'src dyn TypeInfo, |
| 30 | + pub(crate) edits: Vec<Fix>, |
| 31 | +} |
| 32 | + |
| 33 | +impl<'src> LiteralStringReverse<'src> { |
| 34 | + pub(crate) fn new(types: &'src dyn TypeInfo) -> Self { |
| 35 | + Self { |
| 36 | + types, |
| 37 | + edits: Vec::new(), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// `LiteralString` (bare) or `<mod>.LiteralString`, where the name is |
| 42 | + /// spelled `LiteralString` *and* resolves to it |
| 43 | + fn is_literal_string(&self, expr: &Expr) -> bool { |
| 44 | + let spelled = match expr { |
| 45 | + Expr::Name(name) => name.id.as_str() == "LiteralString", |
| 46 | + Expr::Attribute(attribute) => attribute.attr.id.as_str() == "LiteralString", |
| 47 | + _ => false, |
| 48 | + }; |
| 49 | + spelled && self.types.is_literal_string(expr) |
| 50 | + } |
| 51 | + |
| 52 | + fn rewrite_annotation(&mut self, annotation: &Expr) { |
| 53 | + walk_one_type_expr(annotation, self); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl TypeExprVisitor for LiteralStringReverse<'_> { |
| 58 | + fn visit(&mut self, expr: &Expr, _pos: TypePos) -> Recurse { |
| 59 | + if self.is_literal_string(expr) { |
| 60 | + self.edits.push(Fix::safe_edit(Edit::range_replacement( |
| 61 | + "literal str".to_owned(), |
| 62 | + expr.range(), |
| 63 | + ))); |
| 64 | + } |
| 65 | + Recurse::Descend |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl<'ast> Visitor<'ast> for LiteralStringReverse<'_> { |
| 70 | + fn visit_stmt(&mut self, stmt: &'ast Stmt) { |
| 71 | + for_each_annotation_in_stmt(stmt, |annotation| { |
| 72 | + self.rewrite_annotation(annotation); |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use crate::{Config, reverse_transpile, transpile}; |
| 80 | + use indoc::indoc; |
| 81 | + |
| 82 | + /// `Config::test_default` leaves import pruning off so expectations stay |
| 83 | + /// stable; the now-dead `from typing import LiteralString` is dropped in |
| 84 | + /// real `--reverse` usage, which `dead_import_is_pruned` covers |
| 85 | + fn check(input: &str, expected: &str) { |
| 86 | + assert_eq!( |
| 87 | + reverse_transpile(input, &Config::test_default()).unwrap(), |
| 88 | + expected |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn simple_annotation() { |
| 94 | + check( |
| 95 | + "from typing import LiteralString\nx: LiteralString\n", |
| 96 | + "from typing import LiteralString\nx: literal str\n", |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn nested_in_generic() { |
| 102 | + check( |
| 103 | + "from typing import LiteralString\nx: list[LiteralString]\n", |
| 104 | + "from typing import LiteralString\nx: list[literal str]\n", |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn in_union() { |
| 110 | + check( |
| 111 | + "from typing import LiteralString\nx: LiteralString | None\n", |
| 112 | + "from typing import LiteralString\nx: literal str | None\n", |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn function_param_and_return() { |
| 118 | + check( |
| 119 | + indoc! {" |
| 120 | + from typing import LiteralString |
| 121 | + def f(x: LiteralString) -> LiteralString: ... |
| 122 | + "}, |
| 123 | + indoc! {" |
| 124 | + from typing import LiteralString |
| 125 | + def f(x: literal str) -> literal str |
| 126 | + "}, |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn qualified_typing_literal_string() { |
| 132 | + check( |
| 133 | + indoc! {" |
| 134 | + import typing |
| 135 | + x: typing.LiteralString |
| 136 | + "}, |
| 137 | + indoc! {" |
| 138 | + import typing |
| 139 | + x: literal str |
| 140 | + "}, |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn annotated_first_arg_only() { |
| 146 | + // only the first arg of `Annotated[T, meta]` is a type position; a |
| 147 | + // `LiteralString` in the metadata slot is an arbitrary value |
| 148 | + check( |
| 149 | + "from typing import Annotated, LiteralString\nx: Annotated[LiteralString, LiteralString]\n", |
| 150 | + "from typing import Annotated, LiteralString\nx: Annotated[literal str, LiteralString]\n", |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn value_position_unchanged() { |
| 156 | + check( |
| 157 | + "from typing import LiteralString\nx = LiteralString\n", |
| 158 | + "from typing import LiteralString\nx = LiteralString\n", |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn shadowed_unchanged() { |
| 164 | + // a local binding shadows the typing import, so the name no longer |
| 165 | + // resolves to `LiteralString` — leave it alone |
| 166 | + check( |
| 167 | + indoc! {" |
| 168 | + LiteralString = object() |
| 169 | + x: LiteralString |
| 170 | + "}, |
| 171 | + indoc! {" |
| 172 | + LiteralString = object() |
| 173 | + x: LiteralString |
| 174 | + "}, |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn dead_import_is_pruned() { |
| 180 | + // the real `--reverse` path prunes: once the last reference is a |
| 181 | + // keyword, the import supplies nothing |
| 182 | + let out = reverse_transpile( |
| 183 | + "from typing import LiteralString\nx: LiteralString\n", |
| 184 | + &Config::default(), |
| 185 | + ) |
| 186 | + .unwrap(); |
| 187 | + assert_eq!(out, "x: literal str\n"); |
| 188 | + } |
| 189 | + |
| 190 | + /// the pair is a real round trip: python in, the same python out |
| 191 | + #[test] |
| 192 | + fn round_trips_through_the_forward_lowering() { |
| 193 | + let python = "from typing import LiteralString\nx: LiteralString\n"; |
| 194 | + let reversed = reverse_transpile(python, &Config::default()).unwrap(); |
| 195 | + assert_eq!(reversed, "x: literal str\n"); |
| 196 | + let forward = transpile(&reversed, &Config::test_default()).unwrap(); |
| 197 | + assert!( |
| 198 | + forward.contains("x: LiteralString"), |
| 199 | + "expected the annotation back, got:\n{forward}" |
| 200 | + ); |
| 201 | + } |
| 202 | +} |
0 commit comments