{"record":{"id":"c557e7b500599262","repo":"RustPython/RustPython","slug":"next-is-an-invalid-escape-sequence-such-sequ","errorCode":null,"errorMessage":"\"\\{next}\" is an invalid escape sequence. Such sequences will not work in the future. Did you mean \"\\\\{next}\"? A raw string is also an option.","messagePattern":"\"\\\\(.+?)\" is an invalid escape sequence\\. Such sequences will not work in the future\\. Did you mean \"\\\\\\\\(.+?)\"\\? A raw string is also an option\\.","errorType":"console","errorClass":"SyntaxWarning","httpStatus":null,"severity":"warning","filePath":"crates/vm/src/stdlib/_ast/string.rs","lineNumber":268,"sourceCode":"            }\n            'N' => {\n                if let Some('{') = chars.peek().copied() {\n                    chars.next();\n                    for c in chars.by_ref() {\n                        if c == '}' {\n                            break;\n                        }\n                    }\n                }\n                true\n            }\n            _ => false,\n        };\n        if !valid {\n            let message = vm.ctx.new_str(format!(\n                \"\\\"\\\\{next}\\\" is an invalid escape sequence. Such sequences will not work in the future. Did you mean \\\"\\\\\\\\{next}\\\"? A raw string is also an option.\"\n            ));\n            let _ = warn::warn(\n                message.into(),\n                Some(vm.ctx.exceptions.syntax_warning.to_owned()),\n                1,\n                None,\n                vm,\n            );\n        }\n    }\n}\n\nfn ruff_format_spec_to_joined_str(\n    vm: &VirtualMachine,\n    source_file: &SourceFile,\n    flags: ast::AnyStringFlags,\n    format_spec: Option<Box<ast::InterpolatedStringFormatSpec>>,\n) -> Option<Box<JoinedStr>> {\n    match format_spec {\n        None => None,","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/RustPython/RustPython/blob/5dc83d997e635e8ddce1af1a989299d241389e17/crates/vm/src/stdlib/_ast/string.rs#L250-L286","documentation":"SyntaxWarning raised while RustPython turns a string literal into an AST Constant when the source contains a backslash sequence Python does not recognize (such as \\d or \\s). Unknown escapes are currently kept literally, but the construct is scheduled to become a SyntaxError, so the compiler warns. The message names the two valid spellings: double the backslash, or use a raw string.","triggerScenarios":"Compiling or parsing source (compile(), ast.parse(), exec, import) that contains non-raw strings with unrecognized escapes: \"\\d+\", \"\\server\\path\", \"\\w+\\s+\". Most common in regular expressions and Windows file paths; in this code path it surfaces via the _ast string parser when literals become Constant nodes.","commonSituations":"Regex patterns written without the r-prefix; Windows paths in normal strings; escapes copied from other languages (\\s from sed/grep habits); code that worked silently on older interpreters.","solutions":["Use a raw string: r\"\\d+\" instead of \"\\d+\"","Double the backslash when a raw string is not possible: \"\\\\d\"","Fix the typo if a valid escape was intended (for example \\t vs \\n confusion)","Run ruff/flake8 rule W605 or python -W error::SyntaxWarning to surface every occurrence"],"exampleFix":"# before\npattern = \"\\d+\"\n\n# after\npattern = r\"\\d+\"","handlingStrategy":"validation","validationCode":"import warnings\n\ndef compiles_clean(source: str) -> bool:\n    with warnings.catch_warnings(record=True) as caught:\n        warnings.simplefilter(\"always\", SyntaxWarning)\n        compile(source, \"<check>\", \"exec\")\n    return not caught","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write regex patterns as raw strings by convention (always r-prefix)","Run ruff or flake8 rule W605 in CI to flag invalid escapes statically","Compile with -W error::SyntaxWarning during migration to enumerate all sites"],"tags":["string","escape-sequence","syntax-warning","regex"],"backgroundTag":"invalid-escape-sequence","analyzedSha":"5dc83d997e635e8ddce1af1a989299d241389e17","analyzedAt":"2026-08-17T08:14:46.185Z","contentChangedAt":"2026-08-17T08:14:46.185Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}