braintree / braintree_python

Braintree Python library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
242 stars 116 forks source link

Fix all instances of anomalous backslashes #144

Closed DavidCain closed 2 years ago

DavidCain commented 2 years ago

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

hollabaq86 commented 2 years ago

👋 @DavidCain thanks for the PR! We'll take a look and provide feedback. For internal tracking, issue 354

saralvasquez commented 2 years ago

This has gone in and will be release in the next version of the SDK. Thanks for the PR!

DavidCain commented 2 years ago

Thank you, Sara!