# Buggy code
def _append_compressed_format_query(handle):
    # Convert the tuple from urlparse into list so it can be updated in place.
    parsed = list(urlparse.urlparse(handle))
    qsl = urlparse.parse_qsl(parsed[4])
    qsl.append(_COMPRESSED_FORMAT_QUERY)
    parsed[4] = urlencode(qsl)
    return urlparse.urlunparse(parsed)


# Traceback (TypeError)
[2018-06-12 20:47:19,845] {base_task_runner.py:98} INFO - Subtask: [2018-06-12 20:47:19,841] {tf_hub_encode_posts.py:118} INFO - ... save posts into list ...
[2018-06-12 20:47:19,845] {base_task_runner.py:98} INFO - Subtask: [2018-06-12 20:47:19,842] {tf_hub_encode_posts.py:121} INFO - ... look at posts ...
[2018-06-12 20:47:19,847] {base_task_runner.py:98} INFO - Subtask: [2018-06-12 20:47:19,842] {tf_hub_encode_posts.py:122} INFO - [u'11 Stars Who Looked Sexy Playing Home-Wreckers On-Screen: Taylor Swift &amp;amp; More', u"Shania Twain Performs 'Life's About To Get Good' In Sexy Cheetah Print Suit On James Corden", u'Selena Gomez &amp;amp; Justin Bieber: It\u2019s \u2018Painful\u2019 For Her To See Him With Hailey Baldwin', u"Meghan Markle Is Adjusting To Royal Life 'Faster Than Expected' -- Which Royal Is Her New Bestie?", u'Dennis Rodman Mocked After He Breaks Down On CNN After Trump Meets With Kim Jong Un', u'Kate Upton Shows Off Major Sideboob In Sexy Nude Video To Thank Fans For Their Birthday Wishes', u'Hailey Baldwin Playfully Dries Justin Bieber Off With A Towel After Taking A Dip In The Pool', u"'Criminal Minds': Kirsten Vangsness Reveals What\u2019s Really On Her Computer Screen On The Show", u"Mac Miller 'Devastated' Over Ariana's Engagement To Pete Davidson: 'It's A Punch To The Gut'", u'Bode Miller: 5 Things To Know About Olympian Whose 19-Month-Old Daughter Tragically Drowned']
[2018-06-12 20:47:19,847] {base_task_runner.py:98} INFO - Subtask: [2018-06-12 20:47:19,842] {tf_hub_encode_posts.py:125} INFO - ... begin - get module from tf-hub ...
[2018-06-12 20:47:19,858] {base_task_runner.py:98} INFO - Subtask: [2018-06-12 20:47:19,857] {tf_logging.py:160} INFO - Using /tmp/tfhub_modules to cache modules.
[2018-06-12 20:47:19,858] {base_task_runner.py:98} INFO - Subtask: [2018-06-12 20:47:19,858] {tf_logging.py:116} INFO - Downloading TF-Hub Module 'https://tfhub.dev/google/nnlm-en-dim50/1'.
[2018-06-12 20:47:19,935] {base_task_runner.py:98} INFO - Subtask: [2018-06-12 20:47:19,935] {sendgrid.py:84} INFO - Email with subject Airflow alert: <TaskInstance: tf_hub_encode_posts.hollywoodlife_encode_posts_nnlm_en_dim50 2018-06-12 20:30:00 [up_for_retry]> is successfully sent to recipients: [{'to': [{'email': 'andrew.maguire@pmc.com'}]}]
[2018-06-12 20:47:19,974] {base_task_runner.py:98} INFO - Subtask: Traceback (most recent call last):
[2018-06-12 20:47:19,975] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/bin/airflow", line 27, in <module>
[2018-06-12 20:47:19,975] {base_task_runner.py:98} INFO - Subtask:     args.func(args)
[2018-06-12 20:47:19,975] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/airflow/bin/cli.py", line 392, in run
[2018-06-12 20:47:19,976] {base_task_runner.py:98} INFO - Subtask:     pool=args.pool,
[2018-06-12 20:47:19,976] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/airflow/utils/db.py", line 50, in wrapper
[2018-06-12 20:47:19,976] {base_task_runner.py:98} INFO - Subtask:     result = func(*args, **kwargs)
[2018-06-12 20:47:19,976] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/airflow/models.py", line 1492, in _run_raw_task
[2018-06-12 20:47:19,976] {base_task_runner.py:98} INFO - Subtask:     result = task_copy.execute(context=context)
[2018-06-12 20:47:19,977] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/airflow/operators/python_operator.py", line 89, in execute
[2018-06-12 20:47:19,977] {base_task_runner.py:98} INFO - Subtask:     return_value = self.execute_callable()
[2018-06-12 20:47:19,977] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/airflow/operators/python_operator.py", line 94, in execute_callable
[2018-06-12 20:47:19,977] {base_task_runner.py:98} INFO - Subtask:     return self.python_callable(*self.op_args, **self.op_kwargs)
[2018-06-12 20:47:19,977] {base_task_runner.py:98} INFO - Subtask:   File "/home/airflow/gcs/dags/tf_hub_encode_posts.py", line 127, in fn_encode_posts
[2018-06-12 20:47:19,978] {base_task_runner.py:98} INFO - Subtask:     embed = hub.Module("https://tfhub.dev/google/nnlm-en-dim50/1")
[2018-06-12 20:47:19,978] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/module.py", line 105, in __init__
[2018-06-12 20:47:19,979] {base_task_runner.py:98} INFO - Subtask:     self._spec = as_module_spec(spec)
[2018-06-12 20:47:19,979] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/module.py", line 31, in as_module_spec
[2018-06-12 20:47:19,980] {base_task_runner.py:98} INFO - Subtask:     return native_module.load_module_spec(spec)
[2018-06-12 20:47:19,980] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/native_module.py", line 99, in load_module_spec
[2018-06-12 20:47:19,981] {base_task_runner.py:98} INFO - Subtask:     path = compressed_module_resolver.get_default().get_module_path(path)
[2018-06-12 20:47:19,982] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/resolver.py", line 385, in get_module_path
[2018-06-12 20:47:19,982] {base_task_runner.py:98} INFO - Subtask:     return self._get_module_path(handle)
[2018-06-12 20:47:19,983] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/resolver.py", line 467, in _get_module_path
[2018-06-12 20:47:19,983] {base_task_runner.py:98} INFO - Subtask:     return resolver.get_module_path(handle)
[2018-06-12 20:47:19,983] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/resolver.py", line 385, in get_module_path
[2018-06-12 20:47:19,984] {base_task_runner.py:98} INFO - Subtask:     return self._get_module_path(handle)
[2018-06-12 20:47:19,984] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/compressed_module_resolver.py", line 105, in _get_module_path
[2018-06-12 20:47:19,985] {base_task_runner.py:98} INFO - Subtask:     self._lock_file_timeout_sec())
[2018-06-12 20:47:19,985] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/resolver.py", line 313, in atomic_download
[2018-06-12 20:47:19,985] {base_task_runner.py:98} INFO - Subtask:     download_fn(handle, tmp_dir)
[2018-06-12 20:47:19,986] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/compressed_module_resolver.py", line 86, in download
[2018-06-12 20:47:19,986] {base_task_runner.py:98} INFO - Subtask:     request = url.Request(_append_compressed_format_query(handle))
[2018-06-12 20:47:19,986] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/tensorflow_hub/compressed_module_resolver.py", line 62, in _append_compressed_format_query
[2018-06-12 20:47:19,987] {base_task_runner.py:98} INFO - Subtask:     return urlparse.urlunparse(parsed)
[2018-06-12 20:47:19,987] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/future/backports/urllib/parse.py", line 387, in urlunparse
[2018-06-12 20:47:19,987] {base_task_runner.py:98} INFO - Subtask:     _coerce_args(*components))
[2018-06-12 20:47:19,988] {base_task_runner.py:98} INFO - Subtask:   File "/usr/local/lib/python2.7/site-packages/future/backports/urllib/parse.py", line 115, in _coerce_args
[2018-06-12 20:47:19,988] {base_task_runner.py:98} INFO - Subtask:     raise TypeError("Cannot mix str and non-str arguments")
[2018-06-12 20:47:19,988] {base_task_runner.py:98} INFO - Subtask: TypeError: Cannot mix str and non-str arguments

# Fixed code
def _append_compressed_format_query(handle):
    # Convert the tuple from urlparse into list so it can be updated in place.
    parsed = list(urlparse.urlparse(handle))
    qsl = urlparse.parse_qsl(parsed[4])
    qsl.append(_COMPRESSED_FORMAT_QUERY)
    # NOTE: Cast to string to avoid urlunparse to deal with mixed types.
    # This happens due to backport of urllib.parse into python2 returning an
    # instance of <class 'future.types.newstr.newstr'>.
    parsed[4] = str(urlencode(qsl))
    return urlparse.urlunparse(parsed)