braintree_python icon indicating copy to clipboard operation
braintree_python copied to clipboard

Fix all instances of anomalous backslashes

Open DavidCain opened this issue 3 years ago • 0 comments

Summary

This commit fixes deprecation warnings that arise from using backslashes in strings, but not as part of an escape sequence. It will help this library be used with newer versions of Python.

Examples

Consider the string '\A', previously used in braintree/resource.py:

$ python -Wd -c 'print("\A")'
DeprecationWarning: invalid escape sequence \A
$ python -W error -c 'print("\A")'
SyntaxError: invalid escape sequence \A

Explanation

For an explanation of the problem (and the recommended solution), see: https://docs.python.org/3/library/re.html

Also, please note that any invalid escape sequences in Python’s usage of the backslash in string literals now generate a DeprecationWarning and in the future this will become a SyntaxError. This behaviour will happen even if it is a valid escape sequence for a regular expression.

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'.

There are no functional changes!

The raw strings used here are equivalent to their originals on all currently supported versions of Python (including EOL Python 3.5, 3.6).

>>> r"(?<=[^\\])_" == "(?<=[^\\\\])_"
True
>>> r"\[\_\_any\_key\_\_\]" == "\\[\\_\\_any\\_key\\_\\_\\]"
True
>>> r"\[__any_key__\]" == "\\[__any_key__\\]"
True
>>> r'\Z' == '\Z'
True

Checklist

  • [x] Added changelog entry
  • [x] Ran unit tests (nosetests tests/unit)
  • [x] I understand that unless this is a Draft PR or has a DO NOT MERGE label, this PR is considered to be in a deploy ready state and can be deployed if merged to main

DavidCain avatar Jul 29 '22 14:07 DavidCain