# Buggy code
def default_data_dir(bin_dir: Optional[str]) -> str:
    """Returns directory containing typeshed directory

    Args:
      bin_dir: directory containing the mypy script
    """
    if not bin_dir:
        if os.name == "nt":
            prefixes = [os.path.join(sys.prefix, "Lib")]
            try:
                prefixes.append(os.path.join(site.getuserbase(), "lib"))
            except AttributeError:
                # getuserbase in not available in virtualenvs
                prefixes.append(os.path.join(get_python_lib(), "lib"))
            for parent in prefixes:
                data_dir = os.path.join(parent, "mypy")
                if os.path.exists(data_dir):
                    return data_dir
        mypy_package = os.path.dirname(__file__)
        parent = os.path.dirname(mypy_package)
        if (
            os.path.basename(parent) == "site-packages"
            or os.path.basename(parent) == "dist-packages"
        ):
            # Installed in site-packages or dist-packages, but invoked with python3 -m mypy;
            # __file__ is .../blah/lib/python3.N/site-packages/mypy/build.py
            # or .../blah/lib/python3.N/dist-packages/mypy/build.py (Debian)
            # or .../blah/lib64/python3.N/dist-packages/mypy/build.py (Gentoo)
            # or .../blah/lib/site-packages/mypy/build.py (Windows)
            # blah may be a virtualenv or /usr/local.  We want .../blah/lib/mypy.
            lib = parent
            for i in range(2):
                lib = os.path.dirname(lib)
                if os.path.basename(lib) in ("lib", "lib32", "lib64"):
                    return os.path.join(os.path.dirname(lib), "lib/mypy")
        subdir = os.path.join(parent, "lib", "mypy")
        if os.path.isdir(subdir):
            # If installed via buildout, the __file__ is
            # somewhere/mypy/__init__.py and what we want is
            # somewhere/lib/mypy.
            return subdir
        # Default to directory containing this file's parent.
        return parent
    base = os.path.basename(bin_dir)
    dir = os.path.dirname(bin_dir)
    if (
        sys.platform == "win32"
        and base.lower() == "scripts"
        and not os.path.isdir(os.path.join(dir, "typeshed"))
    ):
        # Installed, on Windows.
        return os.path.join(dir, "Lib", "mypy")
    elif base == "scripts":
        # Assume that we have a repo check out or unpacked source tarball.
        return dir
    elif base == "bin":
        # Installed to somewhere (can be under /usr/local or anywhere).
        return os.path.join(dir, "lib", "mypy")
    elif base == "python3":
        # Assume we installed python3 with brew on os x
        return os.path.join(os.path.dirname(dir), "lib", "mypy")
    elif dir.endswith("python-exec"):
        # Gentoo uses a python wrapper in /usr/lib to which mypy is a symlink.
        return os.path.join(os.path.dirname(dir), "mypy")
    else:
        # Don't know where to find the data files!
        raise RuntimeError("Broken installation: can't determine base dir")


# Traceback (AssertionError)
./contrib/sacrebleu/sacrebleu.py: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.610
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 11, in <module>
sys.exit(console_entry())
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
main(None)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/main.py", line 91, in main
res = type_check_only(sources, bin_dir, options, flush_errors, fscache)  # noqa
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/main.py", line 148, in type_check_only
fscache=fscache)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 185, in build
flush_errors, fscache)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 285, in _build
graph = dispatch(sources, manager)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 2398, in dispatch
process_graph(graph, manager)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 2691, in process_graph
process_stale_scc(graph, scc, manager)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 2801, in process_stale_scc
graph[id].semantic_analysis()
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 2009, in semantic_analysis
self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 1818, in wrap_context
yield
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/build.py", line 2009, in semantic_analysis
self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/semanal.py", line 291, in visit_file
v.type = self.anal_type(v.type)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/semanal.py", line 1578, in anal_type
typ = t.accept(a)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/types.py", line 203, in accept
return visitor.visit_unbound_type(self)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/typeanal.py", line 178, in visit_unbound_type
typ = self.visit_unbound_type_nonoptional(t)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/typeanal.py", line 186, in visit_unbound_type_nonoptional
sym = self.lookup(t.name, t, suppress_errors=self.third_pass)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/semanal.py", line 3058, in lookup_qualified
n = self.lookup(parts[0], ctx, suppress_errors=suppress_errors)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/semanal.py", line 3038, in lookup
self.name_not_defined(name, ctx)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/semanal.py", line 3246, in name_not_defined
self.fail(message, ctx)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/semanal.py", line 3281, in fail
self.errors.report(ctx.get_line(), ctx.get_column(), msg, blocker=blocker)
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/errors.py", line 260, in report
target=self.current_target())
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/errors.py", line 208, in current_target
return self.scope.current_target()
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/mypy/scope.py", line 30, in current_target
assert self.module
AssertionError:
./contrib/sacrebleu/sacrebleu.py: : note: use --pdb to drop into pdb

# Fixed code
def default_data_dir() -> str:
    """Returns directory containing typeshed directory."""
    return os.path.dirname(__file__)