Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve r-string prefix capitalization #1244

Closed
janosh opened this issue Jan 23, 2020 · 5 comments · Fixed by #1271 or #2285
Closed

Preserve r-string prefix capitalization #1244

janosh opened this issue Jan 23, 2020 · 5 comments · Fixed by #1271 or #2285
Labels
F: strings Related to our handling of strings S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: bug Something isn't working T: enhancement New feature or request

Comments

@janosh
Copy link

janosh commented Jan 23, 2020

The MagicPython syntax highlighter (and VS Code, Sublime Text and Atom which all use it) interprets upper (R"foo") and lower case r-strings (r"bar") differently. The reason for this is explained in their readme:

Raw strings are often interpreted as regular expressions. This is a bit of a problem, because depending on the application this may actually not be the most common case. Raw strings can simply be the input to some other processor, in which case regexp-specific highlighting is really hindering the overall readability. MagicPython follows a convention that a lower-case r prefix means a regexp string, but an upper-case R prefix means just a raw string with no special regexp semantics. This convention holds true for all of the legal combinations of prefixes. As always the syntax is biased towards Python 3, thus it will mark Python-2-only prefixes (i.e. variations of ur) as deprecated.

This is a neat feature, especially in cases with more involved string interpolation which are hard to read and easy to mess up without proper highlighting. As a simplified example:

import matplotlib.pyplot as plt

plt.title(R"$y_\mathrm{true}$ vs $y_\mathrm{pred}$")
plt.title(r"$y_\mathrm{true}$ vs $y_\mathrm{pred}$")

Note that even GitHub syntax highlighting has this feature. This is what it looks like in VS Code:

upper
lower

Unfortunately, this feature becomes unusable in conjunction with black since black automatically lower cases all r-strings.

Describe the solution you'd like

Either add an option to pyproject.toml to disable automatic lower casing and/or change black's default behavior to preserve string prefix casing. I prefer the latter.

Additional context

This behavior was introduced in #198, apparently without being aware that some syntax highlighters treat the cases differently.

@JelleZijlstra
Copy link
Collaborator

I'm fine with removing the casefolding. Should we do this only for R, or is there a reason to do it for all string prefixes?

@janosh
Copy link
Author

janosh commented Jan 23, 2020

I'm not aware of any other string prefixes where case matters. But then I only did a quick search just now.

@zsol
Copy link
Collaborator

zsol commented Jan 23, 2020

I would remove it for all string prefixes for consistency.

@wenzeslaus
Copy link

wenzeslaus commented May 30, 2021

Was this issue reintroduced?

R"""About test"""


def test1():
    R"""Create test data"""
    pass

21.5b1 creates:

R"""About test"""


def test1():
    r"""Create test data"""
    pass

@JelleZijlstra
Copy link
Collaborator

Can confirm that we lowercase docstring R prefixes now.

@JelleZijlstra JelleZijlstra reopened this May 30, 2021
@JelleZijlstra JelleZijlstra added the F: strings Related to our handling of strings label May 30, 2021
@ichard26 ichard26 added T: bug Something isn't working S: accepted The changes in this design / enhancement issue have been accepted and can be implemented labels May 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: strings Related to our handling of strings S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: bug Something isn't working T: enhancement New feature or request
Projects
None yet
6 participants