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

Incorrectly identify queryset as not iterable when model manager is a subclass #117

Closed
SpoonMeiser opened this issue Jan 22, 2018 · 13 comments

Comments

@SpoonMeiser
Copy link

Since upgrading to version 0.8.0 (and pylint 1.8) we're incorrectly getting an error that a queryset is not iterable:

E: 49,29: Non-iterable value MyModel.objects.filter(foo=bar) is used in an iterating context (not-an-iterable)

MyModel in this case is a subclass of a model that has objects set to a subclass of of the usual manager (in particular, it uses model_utils.managers.InheritanceManager)

Similar uses where the standard manager is used does not cause this error, and we did not get this error with previous versions of pylint and pylint-django.

@antdking
Copy link

suspect it's not picking up the class properly if you use the shortcut.

As a workaround, I've changed my managers to be defined like so:

from django.db.models import manager


class MyModelManager(manager.Manager):
    ...

will want to revert this change once the regression is fixed.

@SpoonMeiser
Copy link
Author

model_utils is a third party module, not written by us, so we can't really change it.

FWIW, the manager is defined like this:

class InheritanceManager(InheritanceManagerMixin, models.Manager):
    pass

https://github.com/jazzband/django-model-utils/blob/master/model_utils/managers.py#L262

@atodorov
Copy link
Contributor

@SpoonMeiser can you post a reproducer which includes all the necessary imports and empty class definitions (e.g. using pass) so I can try and reproduce.

@cybojenix I think you are right but without the full reproducer I can't really do anything.

@SpoonMeiser
Copy link
Author

Ok, with python 2.7

$ pip install pylint pylint-django django==1.10 django-model-utils

Test file:

from django.db import models
from model_utils.managers import InheritanceManager


class BaseModel(models.Model):
    objects = InheritanceManager()

    field = models.CharField()


class SubModel(BaseModel):
    pass


def function():
    for r in SubModel.objects.filter(field='foo'):
        print r.field

Linting with pylint-django:

$ pylint -E --load-plugins=pylint_django test.py
Using config file /home/oj/.pylintrc
************* Module test
E: 16,13: Non-iterable value SubModel.objects.filter(field='foo') is used in an iterating context (not-an-iterable)
Traceback (most recent call last):
  File "/home/oj/virtualenv/pd-repoducer/bin/pylint", line 11, in <module>
    sys.exit(run_pylint())
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint/__init__.py", line 16, in run_pylint
    Run(sys.argv[1:])
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint/lint.py", line 1347, in __init__
    linter.check(args)
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint/lint.py", line 768, in check
    self._do_check(files_or_modules)
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint/lint.py", line 901, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint/lint.py", line 980, in check_astroid_module
    walker.walk(ast_node)
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint/utils.py", line 1016, in walk
    cb(astroid)
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint_django/augmentations/__init__.py", line 671, in wrap_func
    with_method(orig_method, *args, **kwargs)
  File "/home/oj/virtualenv/pd-repoducer/local/lib/python2.7/site-packages/pylint_django/augmentations/__init__.py", line 290, in ignore_import_warnings_for_related_fields
    iterat = to_consume[0].items if PY3 else to_consume[0].iteritems
TypeError: 'NamesConsumer' object does not support indexing

The traceback I'd not seen before.

@antdking
Copy link

We've been getting this error since updating to pylint 1.8.2. Suggest replicating with 1.8.1 until the above error is resolved.

@atodorov
Copy link
Contributor

The NamesConsumer error is #120 and was fixed in version 0.9.0

@SpoonMeiser
Copy link
Author

Why is this labelled as an enhancement? Surely it's a bug/regression?

@hermansc
Copy link

hermansc commented Mar 9, 2018

I'm also seeing this issue..

qs = Model.objects.filter(...)
for item in qs:
    pass

@stsewd
Copy link

stsewd commented Mar 16, 2018

In Read the Docs something similar to what @hermansc described happened (with pylint=1.8.3 and pylint-django==0.9.4). The problem was with astroid==1.6.2, using astroid==1.6.1 solves the problem.

@rmecham
Copy link

rmecham commented Mar 22, 2018

I still run into this issue with pylint==1.8.3, pylint-django==0.9.4, and astroid==1.6.1. (I've also tried astroid==1.6.0 and astroid==1.6.2.)

@nirsalon
Copy link

as far as i see, the issue isn't resolved. see this:

from model_utils.managers import InheritanceManager

class Unit(models.Model):
    sim = models.CharField(max_length=64, null=True, blank=True)
    objects = InheritanceManager()

installed_units = Unit.objects.filter(sim__isnull=False)
ids = [u.sim for u in installed_units]

[E1133(not-an-iterable), ] Non-iterable value installed_units is used in an iterating context

using pylint==1.8.4 and pylint-django==0.11

@atodorov
Copy link
Contributor

@nirsalon this is a new issue but related. file it separately.

@timothyshaw
Copy link

FWIW, I was getting not-an-iterable and unsubscriptable-object when using a custom manager, until I used what @cybojenix suggested previously. Changing:

from django.db import models
CustomManager(models.Manager):
    ...

to:

from django.db.models import manager
CustomManager(manager.Manager):
    ...

Did the trick. I am currently using pylint==1.8.2 and pylint-django==0.11.

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

No branches or pull requests

8 participants