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 syntax warnings #556

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

tueda
Copy link

@tueda tueda commented Jul 30, 2024

With Python 3.12 (w/o .pyc cache files in __pycache__), by importing madminer,

import madminer

I got the following warnings:

/home/tueda/work/madminer/madminer/utils/interfaces/mg.py:268: SyntaxWarning: invalid escape sequence '\$'
  replacement_command = """sed -e 's@\$mgprocdir@'"$mgprocdir"'@' {}/{} > {}/{}""".format(
/home/tueda/work/madminer/madminer/fisherinformation/geometry.py:164: SyntaxWarning: invalid escape sequence '\p'
  """
/home/tueda/work/madminer/madminer/fisherinformation/geometry.py:187: SyntaxWarning: invalid escape sequence '\p'
  """
/home/tueda/work/madminer/madminer/fisherinformation/geometry.py:349: SyntaxWarning: "is not" with 'int' literal. Did you mean "!="?
  if self.dimension is not 2:

This patch resolves these warnings.

The last warning SyntaxWarning: "is not" with 'int' literal may need some explanation: indeed, I expect the current code to work correctly due to CPython's optimization of small integers (from -5 to 256),

x = 2
print(x is 2)  # prints True

but using is for integer comparison in general can lead to incorrect results. For example, the following code gives a counterintuitive result:

x = 257
print(x is 257)  # prints False

* Correct invalid escape sequences in string literals.
* Use "!=" instead of "is not" for integer comparison.
@matthewfeickert
Copy link
Member

Thanks for the PR @tueda! I'm traveling at the moment and so just seeing this, but I can review it tomorrow.

Copy link

@Merosmsm Merosmsm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@Merosmsm Merosmsm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

@Merosmsm Merosmsm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hola

Copy link

@Merosmsm Merosmsm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hola
/home/tueda/work/madminer/madminer/utils/interfaces/mg.py:268: SyntaxWarning: invalid escape sequence '$'
replacement_command = """sed -e 's@$mgprocdir@'"$mgprocdir"'@' {}/{} > {}/{}""".format(
/home/tueda/work/madminer/madminer/fisherinformation/geometry.py:164: SyntaxWarning: invalid escape sequence '\p'
"""
/home/tueda/work/madminer/madminer/fisherinformation/geometry.py:187: SyntaxWarning: invalid escape sequence '\p'
"""
/home/tueda/work/madminer/madminer/fisherinformation/geometry.py:349: SyntaxWarning: "is not" with 'int' literal. Did you mean "!="?
if self.dimension is not 2:

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