Typechecking with assert_type

Recently when pondering the question of when it would or would not be appropriate to put in type assertions to help catch bugs, I concluded that it makes the most sense to do so when building a data structure, but not when just passing things around between calls.

The most difficult-to-debug type errors are the ones where you've put something of the wrong type into a data structure, and it's sat there for a while not causing any obvious problem until something else tries to use it, by which time most evidence of where it came from is gone.

Inspired by this remark on the python-3000 mailing list, we bring you typecheck.assert_type. This function can be used to do in-code typechecking (as opposed to decorator-based typechecking, e.g., accepts, returns, etc). assert_type is intended to help you debug a subtle class of errors: accidental corruption of data structures (for example, inserting the wrong kind of key/value pair into a dictionary). You use it like so:

from typecheck import assert_type
				
my_dict = dict()
# manipulate
# manipulate
# pass my_dict to some other functions
# manipulate
assert_type({str: (int, int, int)}, my_dict)

In this example, if my_dict's values are not 3-tuples of integers, assert_type will raise an AssertionError.

Why would you want to use assert_type instead of the typechecking decorators? Simple efficiency. assert_type can be used to do typechecking only after you've completed a batch of changes, or only when you know that you've changed a data structure (instead of checking an unchanged structure over and over again).

Notes:

Valid XHTML 1.0 Transitional