Edgewall Software

Ticket #323 (new enhancement)

Opened 5 years ago

Last modified 14 months ago

Error message is confusing if <py:for ...> lacks each=""

Reported by: brandon@… Owned by: cmlenz
Priority: major Milestone: 0.6.1
Component: Template processing Version: 0.5.1
Keywords: Cc: brandon@…

Description

I probably use too many templating languages, but a few minutes ago I wrote this when using Genshi:

<py:for loop="item in mylist">

and was greeted with the perplexing error message "TypeError?: argument of type 'NoneType?' is not iterable". This was rather bewildering and it took several minutes for me to work out what was going wrong. It seems that the error message is, instead of pointing out the actual error, leaking an internal detail about how Genshi works: when it asks itself what value I supplied for each="", it gets back the answer None if I failed to specify each="".

Could the error message be re-worded to something like: "py:for is missing each="" attribute" or something?

That would point users at the solution much more effectively.

Thanks!

Attachments

Change History

Changed 5 years ago by cmlenz

  • milestone changed from 0.6 to 0.6.1

Changed 4 years ago by Carsten Klein <carsten.klein@…>

The error results from the statement

if ' in ' not in value:
 ...

Here, the NoneType? is not iterable, see the attached patch that will issue the error on both attach and on init

  • directives.py

     
    338338    __slots__ = ['assign', 'filename'] 
    339339 
    340340    def __init__(self, value, template, namespaces=None, lineno=-1, offset=-1): 
    341         if ' in ' not in value: 
    342             raise TemplateSyntaxError('"in" keyword missing in "for" directive', 
     341 
     342        try: 
     343            if ' in ' not in value: 
     344                raise TemplateSyntaxError('"in" keyword missing in "for" ' 
     345                                          'directive', 
     346                                          template.filepath, lineno, offset) 
     347        except: 
     348            raise TemplateSyntaxError('"value" must be of type "str" in ' 
     349                                      '"for" directive', 
    343350                                      template.filepath, lineno, offset) 
     351 
    344352        assign, value = value.split(' in ', 1) 
    345353        ast = _parse(assign, 'exec') 
    346354        value = 'iter(%s)' % value.strip() 
     
    351359    @classmethod 
    352360    def attach(cls, template, stream, value, namespaces, pos): 
    353361        if type(value) is dict: 
     362            if 'each' not in value: 
     363                raise TemplateSyntaxError('"each" attribute missing in "for"' 
     364                                          ' directive', 
     365                                          template.filepath, *pos[1:]) 
    354366            value = value.get('each') 
     367 
    355368        return super(ForDirective, cls).attach(template, stream, value, 
    356369                                               namespaces, pos) 

Add/Change #323 (Error message is confusing if <py:for ...> lacks each="")

Author


E-mail address and user name can be saved in the Preferences.


Change Properties
<Author field>
Action
as new
as The resolution will be set. Next status will be 'closed'
to The owner will change from cmlenz. Next status will be 'new'
The owner will change from cmlenz to anonymous. Next status will be 'assigned'
 
Note: See TracTickets for help on using tickets.