Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing iterable check on types #8

Open
rpkilby opened this issue Oct 6, 2017 · 7 comments
Open

Failing iterable check on types #8

rpkilby opened this issue Oct 6, 2017 · 7 comments

Comments

@rpkilby
Copy link

rpkilby commented Oct 6, 2017

Hi. The iterable check on Line 25 doesn't work when encountering types.

elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):

I believe this should be changed to

    elif isinstance(obj, collections.Iterable) and not isinstance(obj, (str, bytes, bytearray)):
@bosswissam
Copy link
Owner

@rpkilby can you provide an example of the failure?

@bosswissam
Copy link
Owner

@rpkilby Hi. I will change this algorithm to use a recursive approach. For now if you are blocked you can use sys.setrecursionlimit. Good catch!

@rpkilby
Copy link
Author

rpkilby commented Oct 6, 2017

Yep

>>> from collections import OrderedDict
>>> from pysize import get_size
>>> get_size(OrderedDict)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/bagel/Documents/django-rest-framework-filters/pysize.py", line 26, in get_size
    size += sum((get_size(i, seen) for i in obj))
TypeError: 'type' object is not iterable

@rpkilby
Copy link
Author

rpkilby commented Oct 6, 2017

@bosswissam - it's not a recursion issue, just a simple TypeError. I've fixed it locally by making the change in my original comment.

@rpkilby
Copy link
Author

rpkilby commented Oct 7, 2017

Basically, OrderedDict.__iter__ exists as an unbound method, but the OrderedDict type is not itself iterable, which is why the preceding check erroneously passes.

Hope that helps.

@bosswissam
Copy link
Owner

@rpkilby Apologies I seem to have had another issues open and left a comment on this one mistakenly. Yup will make this change, also feel free to send a PR if you'd like.

@kelechifletcher
Copy link

kelechifletcher commented Oct 19, 2017

Please correct me if I'm wrong, but if a TypeError is occurring simply because a type object has been encountered, wouldn't adding type to the isinstance classinfo tuple fix this issue?

elif hasattr(obj, '__iter__') and not isinstance(obj, (type, str, bytes, bytearray)):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants