MongoEngine / MongoEngine/mongoengine

isinstance(doc, collections.Mapping) == False

Open
#1,598 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

--> 209         return {'syllabus': self.schema.dump(syllabus).data}

ipdb> s
--Call--
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/schema.py(458)dump()
    457
--> 458     def dump(self, obj, many=None, update_fields=True, **kwargs):
    459         """Serialize an object to native Python data types according to this

ipdb> n
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/schema.py(473)dump()
    472         """
--> 473         errors = {}
    474         many = self.many if many is None else bool(many)

ipdb>
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/schema.py(474)dump()
    473         errors = {}
--> 474         many = self.many if many is None else bool(many)
    475         if not many and utils.is_collection(obj) and not utils.is_keyed_tuple(obj):

ipdb>
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/schema.py(475)dump()
    474         many = self.many if many is None else bool(many)
--> 475         if not many and utils.is_collection(obj) and not utils.is_keyed_tuple(obj):
    476             warnings.warn('Implicit collection handling is deprecated. Set '

ipdb> s
--Call--
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/utils.py(63)is_collection()
     62
---> 63 def is_collection(obj):
     64     """Return True if ``obj`` is a collection type, e.g list, tuple, queryset."""

ipdb> n
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/utils.py(65)is_collection()
     64     """Return True if ``obj`` is a collection type, e.g list, tuple, queryset."""
---> 65     return is_iterable_but_not_string(obj) and not isinstance(obj, collections.Mapping)
     66

ipdb> s
--Call--
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/utils.py(51)is_iterable_but_not_string()
     50
---> 51 def is_iterable_but_not_string(obj):
     52     """Return True if ``obj`` is an iterable object that isn't a string."""

ipdb> n
> /Users/Vic/projects/brain2/venv/lib/python3.6/site-packages/marshmallow/utils.py(54)is_iterable_but_not_string()
     53     return (
---> 54         (isinstance(obj, collections.Iterable) and not hasattr(obj, "strip")) or is_generator(obj)
     55     )

ipdb> obj
Syllabus(id='596b04c9b0207a542963af87', title='First Syllabus', ...)
ipdb> isinstance(obj, collections.Iterable)
True
ipdb> isinstance(obj, collections.Mapping)
False
ipdb>

MongoeEngine documents support dict interface
https://github.com/MongoEngine/mongoengine/blob/a7cab513695ba1814d9316719429d63249fb97dc/mongoengine/base/document.py#L215
mongoengine==0.11.0
But still isinstance(obj, collections.Mapping) == False. This causes MarshMallow to think it's a sequence.

    475         if not many and utils.is_collection(obj) and not utils.is_keyed_tuple(obj):
--> 476             warnings.warn('Implicit collection handling is deprecated. Set '
    477                             'many=True to serialize a collection.',
    478                             category=DeprecationWarning)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with marshmallow/utils.py:is_collection and MongoEngine's mongoengine/base/document.py around line 215; reproduce Schema.dump on a MongoEngine document and inspect the Mapping and iterable checks shown in the report. Done means the document is no longer treated as a sequence during dump, with a regression test covering this behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.