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

API client generator template uses deprecated syntax for regular expressions #143

Open
jkachel opened this issue Feb 27, 2025 · 1 comment

Comments

@jkachel
Copy link

jkachel commented Feb 27, 2025

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: python/cpython#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:

sub_kls = re.match('list\[(.*)\]', klass).group(1)

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.

@jkachel
Copy link
Author

jkachel commented Feb 27, 2025

Pytest output (note that this is actually when running 3.12):

$ uv run --python 3.13 pytest
Using CPython 3.13.0
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Installed 19 packages in 65ms
==================================================== test session starts ====================================================
platform darwin -- Python 3.13.0, pytest-8.3.4, pluggy-1.5.0
rootdir: /Users/jkachel/src/cybersource-error
configfile: pytest.ini
collected 0 items / 1 error

========================================================== ERRORS ===========================================================
______________________________________________ ERROR collecting index_test.py _______________________________________________
.venv/lib/python3.13/site-packages/_pytest/python.py:493: in importtestmodule
    mod = import_path(
.venv/lib/python3.13/site-packages/_pytest/pathlib.py:587: in import_path
    importlib.import_module(module_name)
../../.local/share/uv/python/cpython-3.13.0-macos-aarch64-none/lib/python3.13/importlib/__init__.py:88: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1387: in _gcd_import
    ???
<frozen importlib._bootstrap>:1360: in _find_and_load
    ???
<frozen importlib._bootstrap>:1331: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:935: in _load_unlocked
    ???
.venv/lib/python3.13/site-packages/_pytest/assertion/rewrite.py:184: in exec_module
    exec(co, module.__dict__)
index_test.py:5: in <module>
    from index import create_orders_api
index.py:3: in <module>
    from CyberSource import ApiClient, OrdersApi
.venv/lib/python3.13/site-packages/CyberSource/__init__.py:1386: in <module>
    from .api.o_auth_api import OAuthApi
.venv/lib/python3.13/site-packages/CyberSource/api/__init__.py:4: in <module>
    from .o_auth_api import OAuthApi
.venv/lib/python3.13/site-packages/CyberSource/api/o_auth_api.py:24: in <module>
    from ..api_client import ApiClient
E     File "/Users/jkachel/src/cybersource-error/.venv/lib/python3.13/site-packages/CyberSource/api_client.py", line 432
E       sub_kls = re.match('list\[(.*)\]', klass).group(1)
E                          ^^^^^^^^^^^^^^
E   SyntaxError: invalid escape sequence '\['
================================================== short test summary info ==================================================
ERROR index_test.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================== 1 error in 1.63s ======================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant