Grigoriy Petukhov committed f7864f3

Raise exception when func_field decorator is used incorrectly, fix #5

Comments (0)

Files changed (3)


 from __future__ import absolute_import
 from .field import (StringField, IntegerField, DateTimeField,
                     HTMLField, FuncField, NullField, ChoiceField,
-                    RegexField)
+                    RegexField, ItemListField)
 from .item import Item
+from ..error import GrabMisuseError
 def func_field(*args, **kwargs):
+    if not kwargs and len(args) == 1 and callable(args[0]):
+        raise GrabMisuseError('It seems that you forgot to "call" the func_field decorator. Use "@func_field()" instead "func_field".')
     def wrapper(func):
         kwargs['pass_item'] = True
         return FuncField(func=func, *args, **kwargs)


-from .field import Field
+from .field import Field, ItemListField
 from ..selector import XpathSelector
 class ItemBuilder(type):
 # Hack environment to force import "item" module from grab/ location
 root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
 sys.path.insert(0, root)
+from lxml.html import fromstring
 from grab import Grab, DataNotFound
 from grab.item import (Item, IntegerField, StringField, DateTimeField, func_field,
 from test.util import GRAB_TRANSPORT
 from import get_node_text, parse_html
 from grab.selector import XpathSelector
+from grab.error import GrabMisuseError
 XML = """<?xml version='1.0' encoding='utf-8'?>
 <bbapi version='1'>
         func = Player.get_function('height2')
         html = '<html><body><height>3'
         self.assertEquals(3, func(XpathSelector(parse_html(html))))
+    def test_func_field_warning(self):
+        """
+        Test that usage of func_field decorators without "()"
+        raises exception.
+        """
+        def foo():
+            class TestItem(Item):
+                @func_field
+                def foo(self, sel):
+                    return 'test'
+        self.assertRaises(GrabMisuseError, foo)
+        def foo():
+            class TestItem(Item):
+                @func_field()
+                def foo(self, sel):
+                    return 'test'
+            return TestItem(fromstring('<div></div>')).foo
+        self.assertEqual('test', foo())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.