mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Pylint warning - Class 'User' has no 'objects' member (no-member)

Open warvariuc opened this issue 10 years ago • 32 comments

PyLint is complaining:

Class 'User' has no 'objects' member (no-member) Class 'User' has no 'DoesNotExist' member (no-member)

This is because objects is assigned in the metaclass base/metaclasses.py:345:

        # Provide a default queryset unless exists or one has been set
        if 'objects' not in dir(new_class):
            new_class.objects = QuerySetManager()

I think having it defined in the base document would have the effect but be more discoverable:

class Document(BaseDocument):
    objects = QuerySetManager()

Similarly you could do

from mongoengine.queryset import DoesNotExist, MultipleObjectsReturned
class Document(BaseDocument):
    DoesNotExist = DoesNotExist
    MultipleObjectsReturned = MultipleObjectsReturned

Also the per-class exceptions can have their names to include model name:

        # Merge in exceptions with parent hierarchy
        exceptions_to_merge = (DoesNotExist, MultipleObjectsReturned)
        module = attrs.get('__module__')
        for exc in exceptions_to_merge:
            _name = exc.__name__
            parents = tuple(getattr(base, _name) for base in flattened_bases
                         if hasattr(base, _name)) or (exc,)
            # Create new exception and set to new_class
            exception = type(name + _name, parents, {'__module__': module})
            setattr(new_class, _name, exception)

Thus Sentry having such an exceptions + __repr__ would give hint what model the exception belongs to.

warvariuc avatar Jan 23 '15 17:01 warvariuc

+1

jontrainor avatar Jul 15 '15 23:07 jontrainor

+1

citizen-stig avatar Feb 08 '16 20:02 citizen-stig

+1

goldan avatar Feb 22 '16 23:02 goldan

+1

reallistic avatar Mar 04 '16 02:03 reallistic

The same issue in Django is solved thanks to a Pylint plugin.

Indeed, in an ideal world, you'd rather make the checker more clever than adapt the software to the checker's shortcomings.

If it is too much work, it might not be worth the pain, though.

Hopefully, we only need the equivalent of a small subpart of the Django checker. From a quick glance, it looks like this file addresses those issues.

lafrech avatar Apr 29 '16 13:04 lafrech

+1

DavidHwu avatar Jun 22 '16 23:06 DavidHwu

@lafrech

Indeed, in an ideal world, you'd rather make the checker more clever than adapt the software to the checker's shortcomings.

I don't thing adapting a checker to a particular project is also a clever idea. For example autocompletion in PyCharm also fails in this case.

mindojo-victor avatar Jun 23 '16 04:06 mindojo-victor

pylint is still failing on .objects call - any workaround?

boazin avatar Jul 25 '16 02:07 boazin

same here

kiddten avatar Aug 15 '16 12:08 kiddten

Any update on this issue? It has been almost a year ...

nguyenkims avatar Oct 21 '16 20:10 nguyenkims

I added "Python for VSCode" add-on and the problem is resolved in my case.

javsal avatar Aug 15 '17 11:08 javsal

@javsal I added the same extension but it still throws that same error :/

Hankrecords avatar Oct 05 '17 13:10 Hankrecords

@javsal I installed "Python for VSCode" add-on and still have the error
Class 'somemodel' has no 'objects' member (no-member) it's some workaround ?

pabloazurduy avatar Oct 25 '17 13:10 pabloazurduy

Same issue using "Python for VSCode".

gutomarzagao avatar Dec 10 '17 03:12 gutomarzagao

pip install pylint-django

And add to VSC config:

"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
],

tfalcao avatar Dec 12 '17 14:12 tfalcao

"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
],

it will solve the question, but it let my pylint all failure. whats happening and how to fix it?

ghost avatar Apr 17 '18 09:04 ghost

"python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
],

Makes my pylint not working too @ghost, did you managed to make it work?

JMGama avatar Jul 31 '18 16:07 JMGama

I was using pylint_django but when I tried to upgrade, it stopped working with mongoengine so I wrote this: https://github.com/jucacrispim/pylint-mongoengine

jucacrispim avatar Sep 13 '18 21:09 jucacrispim

@JMGama Me too, are you fix this ?

geknow avatar Apr 01 '19 08:04 geknow

from django.shortcuts import render

from .models import Job


def home(request):
    jobs = Job.objects
    return render(request, 'jobs/home.html', {"jobs":jobs} )`

I am getting error


{
	"resource": "/c:/Users/Akhil/Desktop/django_stuff/portfolio-project/jobs/views.py",
	"owner": "python",
	"code": "no-member",
	"severity": 8,
	"message": "Class 'Job' has no 'objects' member",
	"source": "pylint",
	"startLineNumber": 8,
	"startColumn": 12,
	"endLineNumber": 8,
	"endColumn": 12
}

Class 'Job' has no 'objects' member

What should i do now ?

https://github.com/Meet-Kalariya/portfolio-project

meetkalariya avatar Apr 07 '19 10:04 meetkalariya

setp 1. pip install pylint-django

setp 2. open VSCode -> File -> Preferences -> Settings -> search 'python.linting.pylintArgs'

setp 3. Add this in json.

"python.linting.pylintArgs" :[
"--load-plugins=pylint_django"
]

Daviswww avatar Jul 14 '19 13:07 Daviswww

Edit "C:\Users\anmar\AppData\Roaming\Code\User\settings.json" and add 
python.linting.pylintArgs lines at the end as shown below:

{
    "team.showWelcomeMessage": false,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "git.enableSmartCommit": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "files.autoSave": "onWindowChange",
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
        "--errors-only"
    ],
}

nemodev76 avatar Sep 22 '19 19:09 nemodev76

Edit "C:\Users\anmar\AppData\Roaming\Code\User\settings.json" and add 
python.linting.pylintArgs lines at the end as shown below:

{
    "team.showWelcomeMessage": false,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "git.enableSmartCommit": true,
    "powershell.codeFormatting.useCorrectCasing": true,
    "files.autoSave": "onWindowChange",
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django",
        "--errors-only"
    ],
}

Great, this worked for me def post_list(request): posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') return render(request, 'blog/post_list.html', {'posts': posts })

I had an issue where Post wouldn't let me use "objects" as it wasn't a member of the "Post" import, the setting this person posted helped me a lot, thanks!

Cristroyer avatar Oct 14 '19 12:10 Cristroyer

if we do this only `"python.linting.pylintArgs": [ "--load-plugins=pylint_django"

],`

it gives other errors but this does magic "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--errors-only" ], thanks for help

ajaysbugatti avatar May 05 '20 14:05 ajaysbugatti

I fixed this issue by:

First, install python package pylint-django pip install pylint-django

then open the project folder .vscode/settings.json and add:

{
 ...
  "python.linting.pylintArgs": [
    "--load-plugins=pylint_django"
  ],
  ...
}

then the message was gone.

david-daming avatar May 21 '20 14:05 david-daming

def home(request): # pylint: disable=no-member <--------This is just a workaround to get rid of warnings jobs = Job.objects return render(request, 'jobs/home.html', {"jobs":jobs} )`

swapnildongre89 avatar May 28 '20 12:05 swapnildongre89

I got the same error when implementing Model.get_FOO_display() - Django; in vscode .

IMPLEMENTATION.

from django.db import models
    
    class Plan(models.Model):

    PLAN_CHOICES = [
        ('T', 'Talk More'),
        ('S', 'STANDARD),
    ]

    name = models.CharField(
        choices=PLAN_CHOICES,
        default='S',
        max_length=255,
    )

    def __str__(self):
        return self.get_name_display()

ERROR

return self.get_name_display()

Instance of 'Plan' has no 'get_name_display' memberpylint(no-member)

WORK FORM ME

join @david-daming and @ajaysbugatti Thanks!!!

ENGINES

  • Python==3.8.2
  • Django==3.0.8
  • pylint==2.5.3
  • pylint-django==2.1.0
  • pylint-plugin-utils==0.6

cesardmn avatar Jul 16 '20 08:07 cesardmn

I did the: "python.linting.pylintArgs": [ "--load-plugins=pylint_django" ], and "python.linting.pylintArgs": [ "--load-plugins=pylint_django", "--errors-only" ], and stop the problems messages, but also stop other error messages

ghost avatar Jul 29 '20 07:07 ghost

use pylint --disable no-member to suppress no-member error messages.

For me it is working..

npscode avatar Oct 27 '20 08:10 npscode

Similar need here: #2326

NicolasM-Forsk avatar Nov 15 '22 15:11 NicolasM-Forsk