CyberSource / CyberSource/cybersource-rest-client-python
API client generator template uses deprecated syntax for regular expressions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24
- Forks
- 42
- Avg merge
- 29m
- Merged PRs (30d)
- 1
Description
Description
Using the REST Client library in a project with Python >= 3.12 results in chattery SyntaxWarning messages, and sometimes pytest failures.
Starting in Python 3.6, strings that contain invalid escape sequences raise a DeprecationWarning. This includes regular strings that are used for regex expressions, as regex uses backslashed characters to denote operators/classes/etc. and those don't match up with escape sequences. DeprecationWarning is usually silent, but in 3.12 this was changed so that these now emit a SyntaxWarning, which isn't silent.
Compounding the issue is that pytest treats these as a failure when using Python 3.13.
More Info
The change is noted in the What's New for 3.12 here: https://docs.python.org/3/whatsnew/3.12.html#other-language-changes
See also the discussion here: https://github.com/python/cpython/issues/98401
I created a very simple test case that demonstrates the issue. It is available here: https://github.com/jkachel/cybersource-rest-error
The pytest error output is in a comment below to keep this issue from being too long.
Potential Fix
Regular expressions should use raw strings.
The main offenders seem to be here: https://github.com/CyberSource/cybersource-rest-client-python/blob/9d057e3bf85f9c8085394ec319bb2c74e843ffc7/generator/cybersource-python-template/api_client.mustache#L424
and in line 429 in the same file. The fix should be as simple as adding r to the start of those regex strings. I don't have a workflow set up for regenerating this client, and there may be other instances where this should be fixed that I don't know about, so I haven't tried to fix it.
Workaround
Client code can use the warnings library to filter SyntaxWarning when importing the CyberSource API client. Ex:
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=SyntaxWarning)
from CyberSource import ApiClient, OrdersApi
This resolves the issue in the short term but these should be fixed - as noted in the change, Python will (eventually) start emitting SyntaxError for these issues.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in generator/cybersource-python-template/api_client.mustache at the referenced lines 424 and 429, then search the template for other regular expressions using non-raw strings. Regenerate the client if the project workflow permits it and run pytest under Python 3.12 or 3.13; done means the generated client no longer emits these SyntaxWarnings or related failures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100