# Buggy code
def read_with_python_encoding(path: str, pyversion: Tuple[int, int]) -> str:
    """Read the Python file with while obeying PEP-263 encoding detection"""
    source_bytearray = bytearray()
    encoding = "utf8" if pyversion[0] >= 3 else "ascii"

    with open(path, "rb") as f:
        # read first two lines and check if PEP-263 coding is present
        source_bytearray.extend(f.readline())
        source_bytearray.extend(f.readline())

        # check for BOM UTF-8 encoding and strip it out if present
        if source_bytearray.startswith(b"\xef\xbb\xbf"):
            encoding = "utf8"
            source_bytearray = source_bytearray[3:]
        else:
            _encoding, _ = util.find_python_encoding(source_bytearray, pyversion)
            # check that the coding isn't mypy. We skip it since
            # registering may not have happened yet
            if _encoding != "mypy":
                encoding = _encoding

        source_bytearray.extend(f.read())
        return source_bytearray.decode(encoding)


# Traceback (LookupError)
9.796:LOG:  Parsing Lib/test/bad_coding.py (test.bad_coding)
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 6, in <module>
main(__file__)
File "/usr/local/lib/python3.2/site-packages/mypy/main.py", line 54, in main
res = type_check_only(sources, bin_dir, options)
File "/usr/local/lib/python3.2/site-packages/mypy/main.py", line 102, in type_check_only
python_path=options.python_path)
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 209, in build
dispatch(sources, manager)
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 1253, in dispatch
graph = load_graph(sources, manager)
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 1268, in load_graph
st = State(id=bs.module, path=bs.path, source=bs.text, manager=manager)
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 1040, in __init__
self.parse_file()
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 1151, in parse_file
self.tree = manager.parse_file(self.id, self.xpath, source)
File "/usr/local/lib/python3.2/contextlib.py", line 66, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 1105, in wrap_context
yield
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 1144, in parse_file
source = read_with_python_encoding(self.path, manager.pyversion)
File "/usr/local/lib/python3.2/site-packages/mypy/build.py", line 640, in read_with_python_encoding
return source_bytearray.decode(encoding)
LookupError: unknown encoding: uft-8

*** INTERNAL ERROR ***

Lib/test/bad_coding.py: error: Internal error -- please report a bug at https://github.com/python/mypy/issues

# Fixed code
def read_with_python_encoding(path: str, pyversion: Tuple[int, int]) -> str:
    """Read the Python file with while obeying PEP-263 encoding detection"""
    source_bytearray = bytearray()
    encoding = "utf8" if pyversion[0] >= 3 else "ascii"

    with open(path, "rb") as f:
        # read first two lines and check if PEP-263 coding is present
        source_bytearray.extend(f.readline())
        source_bytearray.extend(f.readline())

        # check for BOM UTF-8 encoding and strip it out if present
        if source_bytearray.startswith(b"\xef\xbb\xbf"):
            encoding = "utf8"
            source_bytearray = source_bytearray[3:]
        else:
            _encoding, _ = util.find_python_encoding(source_bytearray, pyversion)
            # check that the coding isn't mypy. We skip it since
            # registering may not have happened yet
            if _encoding != "mypy":
                encoding = _encoding

        source_bytearray.extend(f.read())
        try:
            source_bytearray.decode(encoding)
        except LookupError as lookuperr:
            raise DecodeError(str(lookuperr))
        return source_bytearray.decode(encoding)