Webhelpers.paginate error line 434

Issue #59 wontfix
Dennis Próchniak
created an issue

While using TurboGears 2 which relies on Webhelpers (sprox forms) I found this bug:

Line 434 has an error causing:

Module webhelpers.paginate:434 in init view

self.items = list(self.collection[self.first_item-1:self.last_item]) TypeError: unhashable type

The solution to fix this is to change:

-- self.items = list(self.collection[self.first_item-1:self.last_item])

++ self.items = list(self.collection)[self.first_item-1:self.last_item]

Comments (9)

  1. Christoph Haas

    I haven't used TurboGears and can't tell or verify where TypeError: unhashable type comes from. But loading the entire collection and then slicing it is obviously wrong - at least in an SQLAlchemy context. I'm in favor of reverting this change.

  2. Marcin Kuzminski

    Hi, I think the problem lies in the `get_wrapper`

        # If object is iterable we can use it directly
        if hasattr(obj, "__iter__") and hasattr(obj, "__len__"):
            return obj

    This test will be True for tuple,custom object with iter and len

    example that fails:

    class my(object):
       def __init__(self):
          self.l = range(10)
       def __iter__(self):
           for i in self.l:
               yield i
       def __len__(self):
           return len(self.l)
    from webhelpers.paginate import Page

    Simple fix is to add also check for

    hasattr(obj, "__getslice__")


    That is `getslice` ofcourse :)

  3. Mike Orr

    Reverted original patch in 556b55e75dd6 , and added Martin Kuzminski's code.

    Sprox objects are apparently incompatible with paginate because they aren't sliceable. I had to move the slice check below the SQLAlchemy checks because SQLAlchemy queries and selects also don't define .__getitem__, but that's OK because their wrappers emulate it. Along the way I discovered another bug with paginate and SQLAlchemy select, #64.

    I'll close this with WONTFIX in a few days. We can't add a wrapper for every library out there. Instead I think paginate should be refactored, #65, although that is a longer-term project. The refactoring would allow users to provide a helper class that knows how to extract a page from a third-party collection type.

  4. Mike Orr

    Refined in f6adc7aff8bf to raise the incompatible-collection-type error in this case. The original exception reported ("TypeError: unhashable type") is common to the Sprox error and some other unsliceable sequences, so i check specifically for that.

    Closing as WONTFIX. If #65 is accepted, it would provide a way for users to provide Sprox support if it's not built-in.

  5. Log in to comment