From 88e613ef7b110e5e225e2fe7a93b07d57a82062f Mon Sep 17 00:00:00 2001 From: Asuka Minato Date: Wed, 17 Jun 2026 16:02:40 +0900 Subject: [PATCH] fix --- pyrefly/lib/binding/expr.rs | 7 +++++++ pyrefly/lib/test/annotation.rs | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/pyrefly/lib/binding/expr.rs b/pyrefly/lib/binding/expr.rs index c4cc620b13..2dbbfbbe2b 100644 --- a/pyrefly/lib/binding/expr.rs +++ b/pyrefly/lib/binding/expr.rs @@ -1224,6 +1224,13 @@ impl<'a> BindingsBuilder<'a> { Expr::StringLiteral(literal) if let Some(literal) = as_forward_ref(literal, in_string_literal) => { + if literal.flags.prefix().is_raw() { + self.error( + literal.range(), + ErrorKind::InvalidAnnotation, + "Raw string literals are not allowed in type expressions".to_owned(), + ); + } match Ast::parse_type_literal(literal) { Ok(expr) => { *x = expr; diff --git a/pyrefly/lib/test/annotation.rs b/pyrefly/lib/test/annotation.rs index d05ceac855..1ee4699b90 100644 --- a/pyrefly/lib/test/annotation.rs +++ b/pyrefly/lib/test/annotation.rs @@ -22,6 +22,15 @@ assert_type(D.x, int) # E: assert_type(Unknown, int) failed "#, ); +testcase!( + test_raw_string_type_annotation, + r#" +def f(x: r"int") -> R"str": # E: Raw string literals are not allowed in type expressions # E: Raw string literals are not allowed in type expressions + y: r"bool" = True # E: Raw string literals are not allowed in type expressions + return "" +"#, +); + testcase!( test_union_operator_with_bare_string_literal, TestEnv::new_with_version(PythonVersion::new(3, 13, 0)),