Replace isinstance(obj, str) with duck-typing test.