Type hints in Python allow statically verifying the code is correct, with tools like mypy, efficiently eliminating a whole class of bugs. However sometimes you get the message found module but no type hints or library stubs, because that library doesn't have any type information. It's easy to work around this by adding type stubs.

When you see this error it's worth first checking that there aren't any types already available. Sometimes a later version of the library may have type annotations, so upgrading may make the message go away. There are third party type repositories, like data-science-types which is very helpful if you're using libraries like matplotlib, numpy and pandas. Other times someone has already written a type stub that's not in a package, like in the tqdm issue for a type-stub (and you can put this in tqdm.pyi in the root of your package directory).

If no one else has done the work for you then you have to do it yourself. Mypy has some documentation on stub files and PEP561 outlines the details of how to do it, but they're a bit heavy to read so I'll work through a simple example.

I was trying to typecheck a library with mypy that used warcio. When I ran mypy I got the following errors:

01_fetch_data.py:10: error: Skipping analyzing 'warcio.archiveiterator': found module but no type hints or library stubs

At the time of writing warcio doesn't have type annotations, so we have to write our own stubs. We can start with creating a blank stub; because it's a module we need to do it in the warcio direcory and have a __init__.pyi file (otherwise mypy won't find them).

mkdir warcio
touch warcio/archiveiterator.pyi
touch warcio/__init__.pyi

We then get a more specific error about a missing module:

01_fetch_data.py:10: error: Module 'warcio.archiveiterator' has no attribute 'ArchiveIterator'

We don't need to write stubs for everything in the module for it to be typechecked; just the things we use. To get an idea of what to do it's helpful to look at example stubs that come with mypy in typeshed, and check the source code you're stubbing. In this case we're using ArchiveIterator and all I was using was initialising it (the __init__ function) and iterating over it (the __iter__ and __next__ function). So I juse took these methods from the source of warcio.archiveiterator and replaced the bodies with ...:

import six

class ArchiveIterator(six.Iterator):
def __init__(self, fileobj, no_record_parse=False,
verify_http=False, arc2warc=False,
check_digests=False): ...

def __iter__(self): ...

def __next__(self): ...

Running mypy now gets an error about Name 'BUFF_SIZE' is not defined. In the original code it's imported as from warcio.utils import BUFF_SIZE; but if I do that I'll have to stub warcio.utils too. I can just cheat and add a type declaration at the top of the file.

import six

BUFF_SIZE: int

class ArchiveIterator(six.Iterator):
def __init__(self, fileobj, no_record_parse=False,
verify_http=False, arc2warc=False,
check_digests=False): ...

def __next__(self): ...

def __iter__(self): ...

Surprisingly this is enough to make mypy happy. However we haven't told it much about the types; so there's not much it can actually check. For example if I pass the string 'Yes please' to no_record_parse mypy will say everything is good.

It's fairly easy to infer the types for __init__. The tricky one is fileobj; a quick StackOverflow search gives the types for filelike objects. Normally you pass gzipped files in byte mode to ArchiveIterator so it can take at least typing.BinaryIO, but I don't know whether it can take text files too and could be typing.IO. Generally it's safer to pick the more restrictive type, and then if we get a type error broaden the type as necessary. The rest of the types are clear from the default values and the method has no return in the source.

I know from library interface that the iterator yieds ArcWarcRecords, and so the remaining methods are easy to annotate types too. Typing a Generator is a bit complicated, it takes 3 types, but when you don't send to the generator or return from it the last two arguments are None. All together this is typed now.

import six
from typing import BinaryIO, Generator

BUFF_SIZE: int

class ArchiveIterator(six.Iterator):
def __init__(self, fileobj:BinaryIO, no_record_parse:bool=False,
verify_http:bool=False, arc2warc:bool=False,
check_digests:bool=False) -> None: ...

def __next__(self) -> ArcWarcRecord: ...

def __iter__(self) -> Generator[ArcWarcRecord, None, None]: ...

However now I get a message from mypy that I haven't typed warcio.recordloader yet; I need to add annotations for ArcWarcRecord.

error: Skipping analyzing 'warcio.recordloader': found module but no type hints or library stubs

So I can easily create that stub in warcio/recordloader.pyi and add an ArcWarcRecord class to get more specific issues about the methods I use.

"ArcWarcRecord" has no attribute "content_stream"
"ArcWarcRecord" has no attribute "rec_headers"
To get an idea of how type checking can help in this same codebase I was calculating the end byte given an offset and a length; end_byte = start_byte + length. This was then formated into a string f"{start_byte}-{end_byte}". Unfortunately start_byte and length were strings, so the code ran fine but it concatenated the strings rather than adding their numbers. A type check would have caught this early and clearly pointed out the problem, rather than leaving me scratching my head for a while working out what happened.