explicitly check for Document instances when dereferencing

In particular, `collections.namedtuple` instances also have a `_fields`
attribute which confuses the dereferencing.
Merge remote-tracking branch 'origin/pr/261'
Explicitly check for Document instances when dereferencing (#261)