# Buggy code
def clear_doc(self, docname):
    # type: (unicode) -> None
    for key, (fn, _l) in list(self.data["progoptions"].items()):
        if fn == docname:
            del self.data["progoptions"][key]
    for key, (fn, _l) in list(self.data["objects"].items()):
        if fn == docname:
            del self.data["objects"][key]
    for key, (fn, _l, lineno) in list(self.data["citations"].items()):
        if fn == docname:
            del self.data["citations"][key]
    for key, docnames in list(self.data["citation_refs"].items()):
        if docnames == [docname]:
            del self.data["citation_refs"][key]
        elif docname in docnames:
            docnames.pop(docname)
    for key, (fn, _l, _l) in list(self.data["labels"].items()):
        if fn == docname:
            del self.data["labels"][key]
    for key, (fn, _l) in list(self.data["anonlabels"].items()):
        if fn == docname:
            del self.data["anonlabels"][key]


# Traceback (TypeError)
# Sphinx version: 1.6.1
# Python version: 3.6.1 (CPython)
# Docutils version: 0.13.1 release
# Jinja2 version: 2.9.5
# Last messages:
#   building [mo]: targets for 0 po files that are out of date
#
#   building [html]: targets for 0 source files that are out of date
#
#   updating environment:
#
#   0 added, 19 changed, 0 removed
#
#   reading sources... [  5%] api/generated/metpy.calc.density
#
# Loaded extensions:
#   alabaster (0.7.10) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/alabaster/__init__.py
#   sphinx.ext.autodoc (1.6.1) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/ext/autodoc.py
#   sphinx.ext.autosummary (1.6.1) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/ext/autosummary/__init__.py
#   sphinx.ext.coverage (1.6.1) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/ext/coverage.py
#   sphinx.ext.intersphinx (1.6.1) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/ext/intersphinx.py
#   sphinx.ext.mathjax (1.6.1) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/ext/mathjax.py
#   sphinx.ext.napoleon (1.6.1) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/ext/napoleon/__init__.py
#   sphinx.ext.viewcode (1.6.1) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/ext/viewcode.py
#   matplotlib.sphinxext.plot_directive (unknown version) from /Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/matplotlib/sphinxext/plot_directive.py
Traceback (most recent call last):
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/cmdline.py", line 306, in main
app.build(opts.force_all, filenames)
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/application.py", line 338, in build
self.builder.build_update()
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 328, in build_update
'out of date' % len(to_build))
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/builders/__init__.py", line 341, in build
updated_docnames = set(self.env.update(self.config, self.srcdir, self.doctreedir))
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/environment/__init__.py", line 584, in update
self._read_serial(docnames, self.app)
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/environment/__init__.py", line 602, in _read_serial
self.clear_doc(docname)
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/environment/__init__.py", line 323, in clear_doc
domain.clear_doc(docname)
File "/Users/rmay/miniconda3/envs/py36/lib/python3.6/site-packages/sphinx/domains/std.py", line 542, in clear_doc
docnames.pop(docname)
TypeError: 'str' object cannot be interpreted as an integer

# Fixed code
def clear_doc(self, docname):
    # type: (unicode) -> None
    for key, (fn, _l) in list(self.data["progoptions"].items()):
        if fn == docname:
            del self.data["progoptions"][key]
    for key, (fn, _l) in list(self.data["objects"].items()):
        if fn == docname:
            del self.data["objects"][key]
    for key, (fn, _l, lineno) in list(self.data["citations"].items()):
        if fn == docname:
            del self.data["citations"][key]
    for key, docnames in list(self.data["citation_refs"].items()):
        if docnames == [docname]:
            del self.data["citation_refs"][key]
        elif docname in docnames:
            docnames.remove(docname)
    for key, (fn, _l, _l) in list(self.data["labels"].items()):
        if fn == docname:
            del self.data["labels"][key]
    for key, (fn, _l) in list(self.data["anonlabels"].items()):
        if fn == docname:
            del self.data["anonlabels"][key]