Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Warnings About Dangerous Syntax

In this lesson, you’ll learn about new additions to SyntaxWarning. Python has a SyntaxWarning that can warn you about dubious syntax that is typically not a SyntaxError. Python 3.8 adds a few new ones that can help you when you’re coding and debugging.

The difference between is and == can be confusing. The latter checks for equal values, while is is True only when objects are the same. Python 3.8 will try to warn you about cases when you should use == instead of is:

Python
>>> # Python 3.7
>>> version = "3.7"
>>> version is "3.7"
False

>>> # Python 3.8
>>> version = "3.8"
>>> version is "3.8"
<stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="?
False

>>> version == "3.8"
True

It’s easy to miss a comma when you’re writing out a long list, especially when formatting it vertically. Forgetting a comma in a list of tuples will give a confusing error message about tuples not being callable. Python 3.8 additionally emits a warning that points toward the real issue:

Python
>>> [
...   (3, 5)
...   (7, 9)
... ]
<stdin>:2: SyntaxWarning: 'tuple' object is not callable; perhaps
           you missed a comma?
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'tuple' object is not callable

The warning correctly identifies the missing comma as the real culprit.

Here are a few resources with info on using IPython, the REPL(Read–Eval–Print Loop) tool used in this video:

00:00 This video is about a few new warnings about dangerous syntax. Python 3.8 has a couple of additions to SyntaxWarning. A SyntaxWarning is different than a SyntaxError.

00:12 A SyntaxWarning is warning about dubious syntax. The first is adding some clarity for the difference between is and == and how it can be confusing.

00:23 The double equals (==) is a Boolean-style check for equality, whereas when you use the keyword is, it’s checking for if the objects are the same actual object.

00:34 Let me show you the difference between Python 3.7 and how it’s changed in Python 3.8. For the next couple of examples, I’m going to be using a different REPL. Instead of using the standard Python REPL or the one I’ve been using up to now, bpython, I’m going to be using IPython.

00:49 There are links below this video to an article that describes installing it. First off, I want to show you what it looks like in Python 3.7, and then I’ll show you the change in Python 3.8.

00:59 I don’t suggest uninstalling Python 3.8 and reinstalling Python 3.7. I’m simply switching between the two for these demonstrations. To start IPython I just type ipython and here you can see I’m running Python 3.7.2.

01:15 Here, if you were to make a variable called version and assign it a string value of "3.7", here you can see that when I type this version by itself, it’s equal to '3.7'—a text string.

01:24 But if you were to type version is "3.7", it would simply say False. version is equal to "3.7", but it is not the string "3.7". That would be a separate object.

01:38 Now let me show you what it looks like in Python 3.8.

01:42 Now in Python 3.8,

01:46 if you were to give it a version with a string—here you see it again—

01:51 and you were to use is, you’ll get a different response than simply False. Here it’s saying a SyntaxWarning of using "is" with a literal.

01:59 “Did you mean that it should be "=="?” Which is pretty neat. It still provides the False, but it’s wondering why you chose to use is in that particular case, when it may be that you were looking for this, version == "3.8".

02:15 Another SyntaxWarning—in this case, if you were missing a comma when writing out a list of tuples. Let me show you what it looks like in Python 3.7 as compared to Python 3.8.

02:28 In Python 3.7, if you were to create a new object that had a list in it, let’s say of several tuples… In this case, I’m skipping—or I missed the comma that should be after the first tuple. If I close that off, ending the list—that can happen if you have a nice vertical format, you may have missed putting the comma in. You raise an exception of a TypeError, and that a tuple is not callable, which is a bit confusing.

02:52 It’s thinking that you want to call this tuple, but what’s missing is truly having a comma right here. Python 3.8 adds a little more context. Let me show you. Now I’ll attempt the same thing in Python 3.8.

03:05 If you were to create that list of tuples again, skipping the comma between the lines, and again close it off, closing the list, you get something slightly different: a new SyntaxWarning.

03:14 A SyntaxWarning saying the 'tuple' object is not callable; perhaps you missed a comma? This is the new addition here. And yes, right here—it’s funny because it was pointing at line 3 before, but this time it’s pointing at line 2 saying, “Hey, maybe there should be a comma there.” That’s pretty handy. Up next, I’ll show you a few optimizations that are part of Python 3.8.

Become a Member to join the conversation.