Menu

class decorator - syntax error for valid code

Will Berry
2011-02-15
2013-03-15
  • Will Berry

    Will Berry - 2011-02-15

    Using Python 2.5 grammar and VM.  Using Eclipse Helios and PyDev both fully patched on Windows XP.

    My code:

    foo = lambda x: x
    @foo
    class bar(object): pass
    

    The "Problems" box shows the error message:

    "Encountered "class" at line 13, column 1. Was expecting one of:     "def" ...     "@" ...
    

    The PyDev editor also highlights the word "class" in the declaration of "bar" in red accordingly (not expected).  If I use the def statement instead of the class statement to create "bar", the compiler does not complain and the text editor does not highlight anything in red (as expected).

    Using decorators on classes, while perhaps unusual, is valid and realistic.  I have implemented a Singleton decorator which creates a singleton subclass of the given class dynamically and returns it, so that repeated calls to the factory return the same object, and issubclass() and isinstance() work as expected.

    I do enjoy PyDev and it is a great help to productivity.  I apologize that I do not have time to work on a patch myself or experiment with non-release SVN revisions.  If there are questions about my settings I will be happy to answer.  Hopefully this report helps to improve the tool for others' benefit.  Thank you.

     
  • Fabio Zadrozny

    Fabio Zadrozny - 2011-02-15

    Actually, that's correct when using a Python 2.5 grammar (class decorators were only added on Python 2.6 - so, if you set your grammar to Python 2.6, it should work).

    Cheers,

    Fabio

     
  • Will Berry

    Will Berry - 2011-02-16

    Sorry for wasting your time.  I had been using 2.6 to test all that time, and not realized it.