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

Fix SyntaxWarning from tex/re strings #3163

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

llimeht
Copy link
Contributor

@llimeht llimeht commented Dec 20, 2024

Description

Strings throughout the codebase generate SyntaxWarning when run with newer Python versions. These are from regular expressions or TeX/LaTeX expressions where \ is used lots; Python these days insists that any \X construction it sees should be a valid escape sequence (like \n for a new line), and so invalid ones like \_ now generate a SyntaxWarning.

It's possible to protect these with lots of extra \ all through the code, but it's better to write them as raw strings: r"\d+\_" etc.

(As a general rule for future work, just use raw strings for TeX labels for matplotlib and for regular expressions. A job for a future style CI test...)

How Has This Been Tested?

SyntaxWarnings can be tested with:

find -name \*pyc -delete
py3compile ./src

CI to run tests - hopefully. CI seems to have been disabled on push again, so this is untested until the PR is opened?

Review Checklist:

[if using the editor, use [x] in place of [ ] to check a box]

Documentation (check at least one)

  • There is nothing that needs documenting
  • Documentation changes are in this PR
  • There is an issue open for the documentation (link?)

Installers

  • There is a chance this will affect the installers, if so
    • Windows installer (GH artifact) has been tested (installed and worked)
    • MacOSX installer (GH artifact) has been tested (installed and worked)

Licencing (untick if necessary)

  • The introduced changes comply with SasView license (BSD 3-Clause)

@rozyczko
Copy link
Member

I tried running

find -name \*pyc -delete
py3compile ./src

on Ubuntu 22.04, with Python 3.13 and can see only a single warning from one of the tests, unrelated to the escape sequence.

How did you see all these warning?

@llimeht
Copy link
Contributor Author

llimeht commented Dec 21, 2024

@rozyczko, your py3compile was likely using Python 3.10 as that is the version of Python on Ubuntu 22.04. With Python earlier than 3.12, you'll only see the SyntaxWarning for the incorrect use of is:

$ py3compile --version
py3compile 3.11.2-1+b1
$ py3compile ./src
./src/sas/qtgui/Utilities/UnitTesting/GuiUtilsTest.py:535: SyntaxWarning: "is" with a literal. Did you mean "=="?
assert convertUnitToHTML(s) is ""

With Python 3.12 or 3.13, you'll see the complete list:

$ py3compile --version
py3compile 3.12.7-1
$ py3compile -V 3.13 ./src
./src/sas/qtgui/Plotting/Slicers/AnnulusSlicer.py:134: SyntaxWarning: invalid escape sequence '\p'
new_plot.xaxis("\\rm{\phi}", 'degrees')
./src/sas/qtgui/Plotting/Slicers/WedgeSlicer.py:207: SyntaxWarning: invalid escape sequence '\p'
new_plot.xaxis("\\rm{\phi}", "degrees")
./src/sas/qtgui/Calculators/SldPanel.py:126: SyntaxWarning: invalid escape sequence '\-'
rx = QtCore.QRegularExpression("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?")
./src/sas/qtgui/Calculators/DensityPanel.py:59: SyntaxWarning: invalid escape sequence '\-'
rx = QtCore.QRegularExpression("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?")
./src/sas/qtgui/Calculators/KiessigPanel.py:22: SyntaxWarning: invalid escape sequence '\-'
rx = QtCore.QRegularExpression("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?")
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:131: SyntaxWarning: invalid escape sequence '\d'
input_wavelength = re.match('\d+\.?\d*', input_string)
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:140: SyntaxWarning: invalid escape sequence '\d'
interval_wavelength = re.match('^\d+\.?\d*\s*-\s*\d+\.?\d*$',
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:151: SyntaxWarning: invalid escape sequence '\d'
re.findall('\d+\.?\d*', interval_wavelength.group())
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:170: SyntaxWarning: invalid escape sequence '\d'
pattern = '^\d+\.?\d*(|;\s*\d+)$'
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:186: SyntaxWarning: invalid escape sequence '\d'
pattern = '^\d+\.?\d*\s*-\s*\d+\.?\d*(|;\s*\d+)$'
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:211: SyntaxWarning: invalid escape sequence '\d'
pattern = '^\d+\.?\d*,\s*\d+\.?\d*$'
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:229: SyntaxWarning: invalid escape sequence '\d'
pattern = '^-?\d+\.?\d*(,\s*-?\d+\.?\d*)*$'
./src/sas/qtgui/Calculators/ResolutionCalculatorPanel.py:268: SyntaxWarning: invalid escape sequence '\d'
pattern = '^\d+\.?\d*(|,\s*\d+)$'
./src/sas/qtgui/Perspectives/Corfunc/IDFCanvas.py:22: SyntaxWarning: invalid escape sequence '\A'
self.axes.set_xlabel("Z [$\AA$]")
./src/sas/qtgui/Perspectives/Corfunc/ExtractionCanvas.py:37: SyntaxWarning: invalid escape sequence '\A'
self.axes.set_xlabel("Z [$\AA$]")
./src/sas/qtgui/Perspectives/Corfunc/QSpaceCanvas.py:76: SyntaxWarning: invalid escape sequence '\A'
self.axes.set_xlabel("Q [$\AA^{-1}$]")
./src/sas/qtgui/Perspectives/Corfunc/RealSpaceCanvas.py:28: SyntaxWarning: invalid escape sequence '\A'
self.axes.set_xlabel("Z [$\AA$]")
./src/sas/qtgui/Perspectives/Corfunc/RealSpaceCanvas.py:35: SyntaxWarning: invalid escape sequence '\G'
self.axes.plot(data1.x, data1.y, label="$\Gamma_1(z)$")
./src/sas/qtgui/Perspectives/Corfunc/RealSpaceCanvas.py:36: SyntaxWarning: invalid escape sequence '\G'
self.axes.plot(data3.x, data3.y, label="$\Gamma_3(z)$")
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:76: SyntaxWarning: invalid escape sequence '\+'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:76: SyntaxWarning: invalid escape sequence '\*'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:76: SyntaxWarning: invalid escape sequence '\%'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:76: SyntaxWarning: invalid escape sequence '\*'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:78: SyntaxWarning: invalid escape sequence '\+'
'\+=', '-=', '\*=', '/=', '\%=',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:78: SyntaxWarning: invalid escape sequence '\*'
'\+=', '-=', '\*=', '/=', '\%=',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:78: SyntaxWarning: invalid escape sequence '\%'
'\+=', '-=', '\*=', '/=', '\%=',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:80: SyntaxWarning: invalid escape sequence '\^'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:80: SyntaxWarning: invalid escape sequence '\|'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:80: SyntaxWarning: invalid escape sequence '\&'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:80: SyntaxWarning: invalid escape sequence '\~'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:85: SyntaxWarning: invalid escape sequence '\{'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:85: SyntaxWarning: invalid escape sequence '\}'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:85: SyntaxWarning: invalid escape sequence '\('
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:85: SyntaxWarning: invalid escape sequence '\)'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:85: SyntaxWarning: invalid escape sequence '\['
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Perspectives/ParticleEditor/syntax_highlight.py:85: SyntaxWarning: invalid escape sequence '\]'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Perspectives/Inversion/InversionLogic.py:139: SyntaxWarning: invalid escape sequence '\D'
"""
./src/sas/qtgui/Perspectives/Fitting/FittingWidget.py:3922: SyntaxWarning: invalid escape sequence '\A'
profile_data._xunit = "\AA"
./src/sas/qtgui/Perspectives/Fitting/FittingWidget.py:3924: SyntaxWarning: invalid escape sequence '\A'
profile_data._yunit = "10^{-6}\AA^{-2}"
./src/sas/qtgui/Perspectives/Fitting/FittingUtilities.py:641: SyntaxWarning: invalid escape sequence '\_'
data1d.xaxis(r'\rm{{{}}}'.format(name.replace('_', '\_')), xunit)
./src/sas/qtgui/MainWindow/DataManager.py:148: SyntaxWarning: invalid escape sequence '\['
result = re.split("\[[0-9]+\]$", name)
./src/sas/qtgui/MainWindow/DataManager.py:281: SyntaxWarning: invalid escape sequence '\['
data_name_split = re.split("\[[0-9]+\]$", name)
./src/sas/qtgui/Utilities/PythonSyntax.py:65: SyntaxWarning: invalid escape sequence '\+'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Utilities/PythonSyntax.py:65: SyntaxWarning: invalid escape sequence '\*'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Utilities/PythonSyntax.py:65: SyntaxWarning: invalid escape sequence '\%'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Utilities/PythonSyntax.py:65: SyntaxWarning: invalid escape sequence '\*'
'\+', '-', '\*', '/', '//', '\%', '\*\*',
./src/sas/qtgui/Utilities/PythonSyntax.py:67: SyntaxWarning: invalid escape sequence '\+'
'\+=', '-=', '\*=', '/=', '\%=',
./src/sas/qtgui/Utilities/PythonSyntax.py:67: SyntaxWarning: invalid escape sequence '\*'
'\+=', '-=', '\*=', '/=', '\%=',
./src/sas/qtgui/Utilities/PythonSyntax.py:67: SyntaxWarning: invalid escape sequence '\%'
'\+=', '-=', '\*=', '/=', '\%=',
./src/sas/qtgui/Utilities/PythonSyntax.py:69: SyntaxWarning: invalid escape sequence '\^'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Utilities/PythonSyntax.py:69: SyntaxWarning: invalid escape sequence '\|'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Utilities/PythonSyntax.py:69: SyntaxWarning: invalid escape sequence '\&'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Utilities/PythonSyntax.py:69: SyntaxWarning: invalid escape sequence '\~'
'\^', '\|', '\&', '\~', '>>', '<<',
./src/sas/qtgui/Utilities/PythonSyntax.py:74: SyntaxWarning: invalid escape sequence '\{'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Utilities/PythonSyntax.py:74: SyntaxWarning: invalid escape sequence '\}'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Utilities/PythonSyntax.py:74: SyntaxWarning: invalid escape sequence '\('
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Utilities/PythonSyntax.py:74: SyntaxWarning: invalid escape sequence '\)'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Utilities/PythonSyntax.py:74: SyntaxWarning: invalid escape sequence '\['
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Utilities/PythonSyntax.py:74: SyntaxWarning: invalid escape sequence '\]'
'\{', '\}', '\(', '\)', '\[', '\]',
./src/sas/qtgui/Utilities/UnitTesting/GuiUtilsTest.py:518: SyntaxWarning: invalid escape sequence '\ '
s = ".. |Ang^-1| replace:: |Ang|\ :sup:`-1`"
./src/sas/qtgui/Utilities/UnitTesting/GuiUtilsTest.py:520: SyntaxWarning: invalid escape sequence '\ '
s = ".. |1e-6Ang^-2| replace:: 10\ :sup:`-6`\ |Ang|\ :sup:`-2`"
./src/sas/qtgui/Utilities/UnitTesting/GuiUtilsTest.py:522: SyntaxWarning: invalid escape sequence '\ '
s = ".. |cm^-1| replace:: cm\ :sup:`-1`"
./src/sas/qtgui/Utilities/UnitTesting/GuiUtilsTest.py:535: SyntaxWarning: "is" with 'str' literal. Did you mean "=="?
assert convertUnitToHTML(s) is ""
./src/sas/sascalc/calculator/geni.py:151: SyntaxWarning: invalid escape sequence '\|'
_calc_Iqxy.__doc__ = """
./src/sas/sascalc/calculator/geni.py:179: SyntaxWarning: invalid escape sequence '\|'
"""Compute I(q) for a set of points (x, y), with magnetism on each point.
./src/sas/sascalc/invariant/invariant.py:207: SyntaxWarning: invalid escape sequence '\l'
"""

You might also be able to convince your system py3compile to target the newer Python from your venv; I tend to use chroots rather than venvs as I'm looking at packaging with the distro tools, so the above are using Debian bookworm and sid respectively.

You should be able to ask Python in your venv to do it directly:

$ python3.13 -m py_compile $(find src -type f -name \*.py)

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

Successfully merging this pull request may close these issues.

3 participants