Pylons / Pylons/deform

AssertionError: assert(len(cstruct) == len(field.sequence_fields))

Open
#359 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement needs-pr should backport
Dominant language
JavaScript
Stars
423
Forks
158
PR merge metrics
No merged PRs in 30d

Description

Running the following program results in an AssertionError in deform/widget.py.

Uncommenting either of the two commented out lines prevents the problem.

import colander
from colander import SchemaNode, Integer
import deform
from deform.widget import Select2Widget


class TitleSchema(colander.MappingSchema):
    @colander.instantiate()
    class xs(colander.SequenceSchema):
        x = SchemaNode(Integer())

    y = SchemaNode(Integer())



def test_stackoverflow():
    schema = TitleSchema()
    form = deform.Form(schema)

    items = [
        ('__start__', 'xs:sequence'), ('x', '1'), ('__end__', 'xs:sequence'),
        # ('y', '2'),
    ]

    try:
        title = form.validate(items)
    except deform.ValidationFailure as e:
        title = {}

    # form = deform.Form(schema)
    form.render(title)

test_stackoverflow()
$ python ~/deform-stackoverflow.py
Traceback (most recent call last):
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/template.py", line 171, in render
    self._render(stream, econtext, rcontext)
  File "mapping_item_8ef2b9ece625905633682437b4262fd4.py", line 527, in render
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/field.py", line 512, in serialize
    return self.widget.serialize(**values)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/widget.py", line 1524, in serialize
    assert(len(cstruct) == len(field.sequence_fields))
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/thomas/deform-stackoverflow.py", line 34, in <module>
    test_stackoverflow()
  File "/home/thomas/deform-stackoverflow.py", line 31, in test_stackoverflow
    rendered_form = form.render(title)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/field.py", line 551, in render
    html = self.serialize(cstruct, **kw)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/field.py", line 512, in serialize
    return self.widget.serialize(**values)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/widget.py", line 1369, in serialize
    return field.renderer(template, **values)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/template.py", line 85, in __call__
    return self.load(template_name)(**kw)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/template.py", line 124, in __call__
    return self.render(**kwargs)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/zpt/template.py", line 297, in render
    return super(PageTemplate, self).render(**_kw)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/template.py", line 171, in render
    self._render(stream, econtext, rcontext)
  File "form_69db4de6f2356e2bc79900fcf934ee09.py", line 634, in render
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/field.py", line 741, in render_template
    return self.renderer(template, **values)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/template.py", line 85, in __call__
    return self.load(template_name)(**kw)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/template.py", line 124, in __call__
    return self.render(**kwargs)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/zpt/template.py", line 297, in render
    return super(PageTemplate, self).render(**_kw)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/template.py", line 191, in render
    raise_with_traceback(exc, tb)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/utils.py", line 75, in raise_with_traceback
    raise exc
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/chameleon/template.py", line 171, in render
    self._render(stream, econtext, rcontext)
  File "mapping_item_8ef2b9ece625905633682437b4262fd4.py", line 527, in render
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/field.py", line 512, in serialize
    return self.widget.serialize(**values)
  File "/home/thomas/.virtualenvs/vodsuite/lib/python3.6/site-packages/deform/widget.py", line 1524, in serialize
    assert(len(cstruct) == len(field.sequence_fields))
AssertionError: <unprintable AssertionError object>

 - Expression: "field.serialize(cstruct).strip()"
 - Filename:   ... python3.6/site-packages/deform/templates/mapping_item.pt
 - Location:   (line 29: col 34)
 - Source:     ... place="structure field.serialize(cstruct).strip()"
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 - Expression: "child.render_template(item_template)"
 - Filename:   ... ite/lib/python3.6/site-packages/deform/templates/form.pt
 - Location:   (line 46: col 32)
 - Source:     ... ace="structure child.render_template(item_template)"/>
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 - Arguments:  field: <Field - at 0x7f9579ebd668>
               cstruct: <_null - at 0x7f957be26b00>
               target_language: <NoneType - at 0x9beb80>
               repeat: {...} (0)
               error_class: <NoneType - at 0x9beb80>
               description:
               title: Xs
               oid: deformField1
               hidden: False
               category: default
               structural: False
               required: True
               input_prepend: <NoneType - at 0x9beb80>
               input_append: <NoneType - at 0x9beb80>

...

Versions used:

  • python3.4 and python3.6
  • deform 2.0.4
  • colander 1.4
  • Chameleon: 3.2

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 the supplied Python reproduction and trace deform/widget.py at the Select2Widget.serialize assertion. Compare the sequence data when y is omitted with the case where it is included, then inspect the existing widget tests for a suitable regression-test location. Done means form.render(title) completes without AssertionError for the reported input while existing tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.