-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
bpo-32614: Modify re examples to use a raw string to prevent warning #5265
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the ones that are invalid escape sequences. I can go either way on the '\n' examples, although I would personally not change them.
Doc/howto/regex.rst
Outdated
@@ -1233,7 +1233,7 @@ either side. This takes the job beyond :meth:`!replace`'s abilities.) | |||
|
|||
Another common task is deleting every occurrence of a single character from a | |||
string or replacing it with another single character. You might do this with | |||
something like ``re.sub('\n', ' ', S)``, but :meth:`~str.translate` is capable of | |||
something like ``re.sub(r'\n', ' ', S)``, but :meth:`~str.translate` is capable of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change isn't strictly required, since '\n' is valid and won't raise a warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit tricky. '\n' produces a string of length 1. r'\n' produces a string of length 2. But re recognizes the same escapes that Python does, plus the additional ones, so the latter is also cooked down to a singles character, but later by re instead of Python itself. I think I would be inclined to omit 'r' but mention that it can be optionally added even when the literal only has standard escapes.
Doc/library/re.rst
Outdated
@@ -1398,7 +1398,7 @@ into a list with each nonempty line having its own entry: | |||
.. doctest:: | |||
:options: +NORMALIZE_WHITESPACE | |||
|
|||
>>> entries = re.split("\n+", text) | |||
>>> entries = re.split(r"\n+", text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
I think you can add to this patch a generic comment about adding an 'r' prefix, as discussed on one of the lists (just today?). For instance, after the r'\d+' example. "The 'r' prefix, making the literal a raw string literal, is needed because escape sequences in a normal 'cooked' string literal that are not recognized by Python, as opposed to re, now result in a deprecation warning and will eventually become a syntax error." This should also be someplace in re.rst. |
@terryjreedy I've made the changes, but there's a separate issue for adding the DeprecationWarning info to the docs - #5255 I had opened this one just to make sure the examples wouldn't produce a warning/error if someone was following them. |
If I understand, you think ready to go except for news entry. Please add. The failure log just says "There was an error while trying to fetch the log." I clicked Restart button to see if there is a real problem that none of us see. |
The addition of
causes
I don't know enough .rst to know the fix. |
Doc/howto/regex.rst
Outdated
>>> p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping') | ||
['12', '11', '10'] | ||
|
||
The `r` prefix, making the literal a raw string literal, is needed in this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use double backticks:
``r``
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Doc/howto/regex.rst
Outdated
@@ -327,6 +329,12 @@ backslashes are not handled in any special way in a string literal prefixed with | |||
while ``"\n"`` is a one-character string containing a newline. Regular | |||
expressions will often be written in Python code using this raw string notation. | |||
|
|||
In addition, special escape sequences that are valid in ``re``, but not valid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use RE
or regular expressions
instead of ``re``
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Doc/howto/regex.rst
Outdated
>>> p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping') | ||
['12', '11', '10'] | ||
|
||
The `r` prefix, making the literal a raw string literal, is needed in this | ||
example because escape sequences in a normal "cooked" string literal that are | ||
not recognized by Python, as opposed to ``re``, now result in a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@terryjreedy and @serhiy-storchaka Thank you! I've made the requested changes. |
Am I correct in thinking this should be backported to 3.7, but not 3.6? Serhiy, do you wish to merge this, or should I? |
Please merge it Terry. Warnings are emitted starting from 3.6. Examples in 3.6 should be fixed too. |
Thanks @csabella for the PR, and @terryjreedy for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7. |
…ythonGH-5265) Modify RE examples in documentation to use raw strings to prevent DeprecationWarning. Add text to REGEX HOWTO to highlight the deprecation. Approved by Serhiy Storchaka. (cherry picked from commit 6677142) Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
GH-5499 is a backport of this pull request to the 3.7 branch. |
Sorry, @csabella and @terryjreedy, I could not cleanly backport this to |
…rning (pythonGH-5265) Modify RE examples in documentation to use raw strings to prevent DeprecationWarning. Add text to REGEX HOWTO to highlight the deprecation. Approved by Serhiy Storchaka. (cherry picked from commit 6677142)
…rning (pythonGH-5265) Modify RE examples in documentation to use raw strings to prevent DeprecationWarning. Add text to REGEX HOWTO to highlight the deprecation. Approved by Serhiy Storchaka.. (cherry picked from commit 6677142)
I only modified the examples that would have given a warning.
https://bugs.python.org/issue32614