pygments: Error parsing Python raw f-string with backslash

Python(3) code using raw f-strings containing a backslash within escaped braces (i.e. double braces) cannot be parsed. A minimum example is

fr'{{\S'

Some pointers:

  • Works: f'{{\S'.
  • Works: r'{{\S'
  • Works: rf'{{'.
  • Works: rf'\S'.
  • Also do not work: rf'{{\S'.
  • The S is not important, though some character must be there in order for the syntax to be valid.

This might seem like a pretty esoteric case. For me it comes up a lot when writing LaTeX in Matplotlib.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (9 by maintainers)

Most upvoted comments

… though I’m now slightly reconsidering whether I should have asked the question, I appreciate the answers and discussion šŸ˜„

That looks quite promising – looking forward to the PR!

Thank you for leading the way.

image

I have a version that more or less solves all these problems for all bytes and string literals, I still need to clean up a bit but results are great so far.

Updated examples from https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals.

image