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. Did you mean "!="? #248

Open
sun1ch opened this issue Jan 31, 2022 · 5 comments
Open

SyntaxWarning: "is not" with a literal. Did you mean "!="? #248

sun1ch opened this issue Jan 31, 2022 · 5 comments

Comments

@sun1ch
Copy link

sun1ch commented Jan 31, 2022

I'm trying to run

$ python3 contemplate_koans.py
/home/sashka/python_koans/koans/about_none.py:50: SyntaxWarning: "is not" with a literal. Did you mean "!="?
  self.assertEqual(__, None is not 0)

Thinking AboutAsserts
  test_assert_truth has damaged your karma.

You have not yet reached enlightenment ...
  AssertionError: False is not true

Please meditate on the following code:
  File "/home/sashka/python_koans/koans/about_asserts.py", line 17, in test_assert_truth
    self.assertTrue(False) # This should be True


You have completed 0 (0 %) koans and 0 (out of 37) lessons.
You are now 304 koans and 37 lessons away from reaching enlightenment.

Beautiful is better than ugly.

My python version is 3.9.5 on Ubuntu 21.04

@minusf
Copy link

minusf commented Feb 28, 2022

The warning comes from a later test: koans/about_none.py:50:

        self.assertEqual(__, None is not 0)
        self.assertEqual(__, None is not False)

This is the old python2 syntax and with python3 it should be !=:

...
Python 3.9.10 (main, Jan 15 2022, 11:40:53)
...
>>> None is not 0
<stdin>:1: SyntaxWarning: "is not" with a literal. Did you mean "!="?
True

>>> None != 0
True

However the != version will generate a false positive warning by flake8 and similar tools

comparison to None should be 'if cond is not None:'

@HeavenEvolved
Copy link

The warning comes from a later test: koans/about_none.py:50:

        self.assertEqual(__, None is not 0)
        self.assertEqual(__, None is not False)

This is the old python2 syntax and with python3 it should be !=:

...
Python 3.9.10 (main, Jan 15 2022, 11:40:53)
...
>>> None is not 0
<stdin>:1: SyntaxWarning: "is not" with a literal. Did you mean "!="?
True

>>> None != 0
True

However the != version will generate a false positive warning by flake8 and similar tools

comparison to None should be 'if cond is not None:'

Hey @minusf Is there a way to work around that? For example adding # flake8: noqa to that line if we have the != operator? I am not sure what the other tools you mentioned are, so could you list them, if possible? I was thinking a try/except block might be useful here to catch the SyntaxWarning and print the other form if needed. What do you think?

@minusf
Copy link

minusf commented Mar 16, 2023

i think the fix in #256 is fine...

@hugovk
Copy link
Contributor

hugovk commented Mar 16, 2023

I would leave noqa out of the code intended for beginners, it's could be confusing.

It's okay for some of these to produce Flake8 warnings, they're introducing concepts, and aren't necessarily something you would use in production code.

I would remove the runtime SyntaxWarning to avoid confusing beginners, and this still illustrates "None is distinct from other things which are False":

-self.assertEqual(__, None is not 0)
-self.assertEqual(__, None is not False)
+self.assertEqual(__, None != False)
+self.assertEqual(__, None != 0)

Please see PR #277 for this.

@HeavenEvolved
Copy link

I would leave noqa out of the code intended for beginners, it's could be confusing.

It's okay for some of these to produce Flake8 warnings, they're introducing concepts, and aren't necessarily something you would use in production code.

I would remove the runtime SyntaxWarning to avoid confusing beginners, and this still illustrates "None is distinct from other things which are False":

-self.assertEqual(__, None is not 0)
-self.assertEqual(__, None is not False)
+self.assertEqual(__, None != False)
+self.assertEqual(__, None != 0)

Please see PR #277 for this.

That's true, but I was just wondering if we could do the ignore thing if we absolutely wanted no errors at all. The best thing would probably be just using != or even a try/except block to try both the versions.

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 a pull request may close this issue.

4 participants