Convert isinstance assertions to TypeErrors.

Using assert statements can cause subtle bugs when Python
interpreter invoked with the -O option..

See https://code.djangoproject.com/ticket/23924 for an example report.