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

SyntaxWarning: "is not" with a literal #2713

Closed
phillxnet opened this issue Oct 19, 2023 · 4 comments
Closed

SyntaxWarning: "is not" with a literal #2713

phillxnet opened this issue Oct 19, 2023 · 4 comments
Assignees

Comments

@phillxnet
Copy link
Member

With our recent Python version updates we are now seeing the following warnings when running our tests within an rpm build environment:

cd src/rockstor/
poetry run django-admin test
/src/rockstor/system/luks.py:241: SyntaxWarning: "is not" with a literal. Did you mean "!="?
if container_dev is not "":
/src/rockstor/system/luks.py:351: SyntaxWarning: "is not" with a literal. Did you mean "!="?
if len(source_dev_fields) is not 2:
/src/rockstor/storageadmin/views/email_client.py:204: SyntaxWarning: "is not" with a literal. Did you mean "!="?
if len(line) > 0 and line[0] is not "#":
/src/rockstor/storageadmin/views/rockon_helpers.py:239: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if new_state is not "pending_update" and task is not None:
/src/rockstor/storageadmin/views/rockon_helpers.py:239: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if new_state is not "pending_update" and task is not None:
/src/rockstor/storageadmin/views/rockon_helpers.py:379: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if c.uid is -1:
Creating test database for alias 'default'...
Creating test database for alias 'smart_manager'...
@phillxnet phillxnet self-assigned this Oct 21, 2023
@phillxnet
Copy link
Member Author

phillxnet commented Oct 21, 2023

New in Py3.8

https://docs.python.org/3.8/whatsnew/3.8.html#changes-in-python-behavior

The compiler now produces a SyntaxWarning when identity checks (is and is not) are used with certain types of literals (e.g. strings, numbers). These can often work by accident in CPython, but are not guaranteed by the language spec. The warning advises users to use equality tests (== and !=) instead. (Contributed by Serhiy Storchaka in bpo-34850.)

Nice article on this here: https://adamj.eu/tech/2020/01/21/why-does-python-3-8-syntaxwarning-for-is-literal/

is checks for identity - if the two variables point to the exact same object.

== checks for equality - if the two variables point at values are equal. That is, if they will act the same way in the same situations.

Identity implies equality, but not the other way round.

@phillxnet
Copy link
Member Author

phillxnet commented Oct 21, 2023

When changing our second instance in a single line as recommended:

/src/rockstor/storageadmin/views/rockon_helpers.py:239: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if new_state is not "pending_update" and task is not None:
/src/rockstor/storageadmin/views/rockon_helpers.py:239: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if new_state is not "pending_update" and task is not None:

We run-into:

Comparison with None performed with equality operators
PEP 8: E711 comparison to None should be 'if cond is not None:'

Ignoring second instance for now for Draft PR preparation.

Context is:

@db_task(context=True)
def uninstall(rid, new_state="available", task=None):

@phillxnet
Copy link
Member Author

phillxnet commented Oct 21, 2023

Double checking on the double "is not" use, and sure enough it seems to be a false alarm:
Carrying just one 'wrong' instance (as we currently have) invokes two warnings:

            if new_state is not "pending_update" and task is not None:

results in:

lbuildvm:/opt/rockstor/src/rockstor # poetry run python -Wd /opt/rockstor/.venv/bin/django-admin test -v 2
/opt/rockstor/.venv/lib/python3.9/site-packages/django/utils/version.py:98: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  loose_version = LooseVersion(version)
/opt/rockstor/src/rockstor/storageadmin/views/rockon_helpers.py:495: DeprecationWarning: invalid escape sequence \l
  [DOCKER, "exec", c.name, "psql", "-U", "postgres", "-c", "\l"],
/opt/rockstor/src/rockstor/storageadmin/views/rockon_helpers.py:239: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if new_state is not "pending_update" and task is not None:
/opt/rockstor/src/rockstor/storageadmin/views/rockon_helpers.py:239: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  if new_state is not "pending_update" and task is not None:
Creating test database for alias 'default' ('test_storageadmin')...

Where as with:

            if new_state != "pending_update" and task is not None:

gives the following:

lbuildvm:/opt/rockstor/src/rockstor # poetry run python -Wd /opt/rockstor/.venv/bin/django-admin test -v 2
/opt/rockstor/.venv/lib/python3.9/site-packages/django/utils/version.py:98: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  loose_version = LooseVersion(version)
/opt/rockstor/src/rockstor/storageadmin/views/rockon_helpers.py:495: DeprecationWarning: invalid escape sequence \l
  [DOCKER, "exec", c.name, "psql", "-U", "postgres", "-c", "\l"],
Creating test database for alias 'default' ('test_storageadmin')...

Moving to pull request. The remaining warnings (via Python -Wd) are separately issued.

phillxnet added a commit to phillxnet/rockstor-core that referenced this issue Oct 21, 2023
Modify conditionals to address Py3.8's added warnings.
phillxnet added a commit that referenced this issue Oct 21, 2023
…h-a-literal

SyntaxWarning: "is not" with a literal #2713
@phillxnet
Copy link
Member Author

Closing as:
Fixed by #2722

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

No branches or pull requests

1 participant