[Sublime + Python Setup] The Ctrl+s "Heisenbug"

"What the **** is going on?!" I heard Keith yell.

Returning from my lunch break and in a helpful mood I grabbed my coffee mug and shuffled over to my coworker's desk.

"What's wrong?"

Keith was working on a ticket to fix an issue with our Python-based web portal. The result of some numerical calculation was way off. It seemed like a straightforward algorithmic fix…

"I think this Python code is haunted or something," said Keith as I sat down next to him. "Opening and saving this source file fixes one bug, but then 5 others show up!"

Two hours of swearing and pair-programming later, our investigation of the "haunted code" revealed the following steps to reproduce the issue:

First, we check out the original version of the affected source file from Git. Bug A shows up. So far, so expected.

Next, we open the file in Sublime Text and, without making any edits, immediately hit Ctrl+S to save it again. Now bug A is gone—only to have brand new bugs B, C, and D show up!

Umm…

We were stumped.

"Maybe it's some odd whitespace issue…"

You see, whitespace has a meaning in Python. It uses whitespace indentation levels to determine how code blocks are nested.

Personally, I really like the idea of "semantic whitespace". But occasionally it can lead to pure hell. Take a look at the screenshot below:

Image

Running this script you'd expect to get 10 × 10 = 100 as the answer.

However, by using a mixture of space and tab (\t) characters, you can get this program to print "10"—a completely bogus answer:

For Python indenting, a tab is counted as equivalent to 8 spaces…

And code that looked correct on our screens was actually way off for the Python interpreter:

def square(x):
    result = 0
    for i in range(x):
        result += x
<\t>return result  # ← 1 tab character
^^^^               #   instead of 4 spaces

Remember, one tab is equal to 8 spaces. So this mixture of tab and spaces gets parsed as the following:

def square(x):
    result = 0
    for i in range(x):
        result += x
        return result  # ← 1 tab == 8 spaces

Now the return-statement is indented one level too far. It breaks out of the loop after the first iteration —

D'Oh!

Now by merely re-saving the file in Sublime these tab characters were converted to 4 space characters each. Thus fixing the original indentation problem, but also introducing several new ones elsewhere in the code…

Double D'Oh!

In the end Keith and I easily spent 20 or more developer hours on tracking down various bugs caused by inconsistent whitespace throughout the code base.

It was a nightmare of a bug to fix, and what frustrated me the most about it was how easily it could've been avoided in the first place:

Had we used static code analysis tools back then we would've caught these problems much more easily. Code linting tools would've simply highlighted this whitespace issue right in our editors (and on our build server):

Image

For this reason I'm a big proponent of static code analysis tools now. They can help you detect and void certain bugs and classes of errors completely.

A code linter can catch functional bugs like misspelled identifiers, or reveal code quality issues like unused variables or imports.

I won't say automated code analysis is a miracle cure (sometimes it feels like it) —

But usually the Return on Investment for these tools is simply through the roof. They help reduce debugging and code review time with just a tiny initial time investment.

Now, as awesome as these tools sound, there are some common gotchas to integrating them with Sublime Text:

If you're not careful, integrated code linting can get overly verbose and distracting—and it can slow your editing experience down to a crawl…

To see how to set up silky smooth code linting for Python in less than 10 minutes, click here for more.

— Dan Bader

Older messages

[Python Dependency Pitfalls] What dev managers expect from Python candidates

Wednesday, September 28, 2022

Hey there, My friend Og is a senior manager at Red Hat and works with a large team of developers and quality engineers using Python. I got to pick his brain on what he thought were the most important

[Python Mastery] What does it mean to "master" Python?

Wednesday, September 28, 2022

Hey there, A couple of years ago I'd become quite interested in martial arts. Hours upon hours of watching "The Karate Kid" growing up must've taken their toll on me... And so, I

[Sublime + Python Setup] How to become a happier & more productive Python dev

Wednesday, September 28, 2022

Hey there, I really struggled with setting up an effective development environment as a new Python developer. It was difficult to build the right habits and to find a set of tools I enjoyed to use.

[Python Dependency Pitfalls] How to set the world on fire

Tuesday, September 27, 2022

Hey there, #1 on my list of dependency management pitfalls is there for a good reason: It lead to a single developer causing mayhem and breaking thousands of open-source projects around the world in

[Python Dependency Pitfalls] A total mess?

Monday, September 26, 2022

Hey there, Recently I watched a Pythonista ask for advice on setting up a Python project on his work machine. This new developer had some prior experience with NodeJS and had just started to get his

You Might Also Like

Pydantic, Web Security, State of Python, and More

Tuesday, April 16, 2024

Pydantic: Simplifying Data Validation in Python #625 – APRIL 16, 2024 VIEW IN BROWSER The PyCoder's Weekly Logo Pydantic: Simplifying Data Validation in Python Discover the power of Pydantic,

Save big - online product conference on May 2nd

Tuesday, April 16, 2024

INDUSTRY Virtual takes place on May 2nd — which includes: Broadcasts of some of our favorite conference talks from the past year from Petra Wille, Kate Leto, Quincy Olatunde, and others — with live

Navigating the Digital Thread: Join Our Exclusive Webinar!

Tuesday, April 16, 2024

Enhancing Collaboration and Efficiency with Windchill, Creo, and Codebeamer View this email in your browser engineering.com Navigating the Digital Thread: Enhancing Collaboration and Efficiency with

Daily Coding Problem: Problem #1414 [Easy]

Tuesday, April 16, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Bloomberg. Determine whether there exists a one-to-one character mapping from one string

Charted | The Declining Value of the U.S. Federal Minimum Wage 📉

Tuesday, April 16, 2024

This graphic compares the nominal vs. inflation-adjusted value of the US minimum wage, from 1940 to 2023. View Online | Subscribe Presented by: The economy is changing. Is your portfolio adapting? >

⚙️ Limitless

Tuesday, April 16, 2024

Plus: OpenAI Japan takeover ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

The latest Go developer survey results are in

Tuesday, April 16, 2024

Plus lots of database stuff, a new Go book is in print, and a Minesweeper implementation. | #​503 — April 16, 2024 Unsub | Web Version If you wondered why you didn't get an issue last week, we took

Noonification: HackerNoons Emoji Credibility Indicators are Live on GitHub and Figma!

Tuesday, April 16, 2024

Top Tech Content sent at Noon! How are you, @newsletterest1? 🪐 What's happening in tech this week: The Noonification by HackerNoon has got you covered with fresh content from our top 5 stories of

We’re LIVE! 🤖

Tuesday, April 16, 2024

We're up to this slide [IMAGE] ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

Issue #49: Juno by Alex Staravoitau

Tuesday, April 16, 2024

Today, we're looking at Juno by Alex Staravoitau. Juno offers a comprehensive Python development environment for both iPad and iPhone, designed to meet all your coding needs. It features