Python 3 - Comparing a version string to an int
I added Twilio backward compatibility to django-sendsms so its Twilio backend would work with both the old (v5) and new (v6+) Twilio clients. The version detection looked like this:
import twilio
if twilio.__version__ > 5:
from twilio.rest import Client as TwilioRestClient
else:
from twilio.rest import TwilioRestClient
That works on Python 2, but it’s broken on Python 3. twilio.__version__ is a string like "6.5.0", and comparing a string to an int raises a TypeError on Python 3:
>>> "6.5.0" > 5
TypeError: '>' not supported between instances of 'str' and 'int'
Python 2 lets you order any two objects (ints always sort before strings), so the same comparison silently returns True there. The bug stayed hidden until the code ran under Python 3.
The fix pulls the major version out of __version_info__ and compares ints to ints:
if int(twilio.__version_info__[0]) > 5:
That same PR added Python 3.5 and 3.6 to the Travis build, so this kind of thing gets caught next time.