pylint-dev / pylint-django

Pylint plugin for improving code analysis for when using Django
Other
591 stars 117 forks source link

Instance of 'ManyToManyField' has no 'all' memberPylint E1101:(no-member) #404

Open LCH-1 opened 1 year ago

LCH-1 commented 1 year ago

models.py

from django.db import models

class Category(models.Model):
    name = models.CharField("name", max_length=100)

class Movie(models.Model):
    categories = models.ManyToManyField(Category, related_name="movies")
    created_at = models.DateTimeField("created_at", auto_now_add=True)

    def __str__(self):
        test = ""  # for just test
        return ""

    def get_categories(self):
        return self.categories.all()

    def get_created_date(self):
        return self.created_at.strftime("%Y-%m-%d")

pylint extention result

2023-09-13 18:33:10.721 [info] [Trace - 6:33:10 PM] Sending notification 'textDocument/didSave'.
2023-09-13 18:33:10.725 [info] [Trace - 6:33:10 PM] Received notification 'window/logMessage'.
2023-09-13 18:33:10.725 [info] c:\Users\user\Desktop\django_test\venv\Scripts\python.exe -m pylint --reports=n --output-format=json --disable=C0115, C0114, C0116 --load-plugins=pylint_django --django-settings-module=test_project.settings --clear-cache-post-run=y --from-stdin c:\Users\user\Desktop\django_test\testapp\models.py
2023-09-13 18:33:10.725 [info] [Trace - 6:33:10 PM] Received notification 'window/logMessage'.
2023-09-13 18:33:10.725 [info] CWD Linter: c:\Users\user\Desktop\django_test
2023-09-13 18:33:12.273 [info] [Trace - 6:33:12 PM] Received notification 'window/logMessage'.
2023-09-13 18:33:12.273 [info] file:///c%3A/Users/user/Desktop/django_test/testapp/models.py :
[
    {
        ...
        "type": "warning",
        "obj": "Movie.__str__",
        "symbol": "unused-variable",
        "message": "Unused variable 'test'",
        "message-id": "W0612"
    },
    {
        ...
        "type": "error",
        "obj": "Movie.get_categories",
        "symbol": "no-member",
        "message": "Instance of 'ManyToManyField' has no 'all' member",
        "message-id": "E1101"
    },
    {
        ...
        "type": "error",
        "obj": "Movie.get_created_date",
        "symbol": "no-member",
        "message": "Instance of 'DateTimeField' has no 'strftime' member",
        "message-id": "E1101"
    }
]

pylint terminal result

c:\Users\user\Desktop\django_test\venv\Scripts\python.exe -m pylint --reports=n --output-format=json --disable="C0115, C0114, C0116" --load-plugins=pylint_django --django-settings-module=test_project.settings --clear-cache-post-run=y c:\Users\user\Desktop\django_test\testapp\models.py

[
    {
        ...
        "type": "warning",
        "obj": "Movie.__str__",
        "symbol": "unused-variable",
        "message": "Unused variable 'test'",
        "message-id": "W0612"
    }
]

versions

Pylint(Vscode Extention) v2023.6.0
Python 3.11.1
Django==4.2.4
pylint==2.17.5
pylint-django==2.5.3
astroid==2.15.6
LCH-1 commented 1 year ago

this error does not occur in terminal, but only in vscode pylint extension.

Pierre-Sassoulas commented 1 year ago

What are the versions used in the vscode extension ? You also need to check if you're in the same virtual environnement.

LCH-1 commented 1 year ago

More detailed results were reported in the first comment.

I also checked that the selected virtual environment settings are the same as terminal and vscode selected interpreter.

poulposse commented 6 months ago

Any progress? (Same issue here)

sjdemartini commented 5 months ago

I'm experiencing the same problem, but I'm guessing it's an issue with the VSCode Extension (https://github.com/microsoft/vscode-pylint) and perhaps not specific to pylint-django. Seemingly the VSCode extension is (as of recently) failing to load the pylint-django plugin properly, regardless of whether I specify it with the pylint.args setting or in my .pylintrc. I'm not sure where to dig in further.

For what it's worth, I do see this near the top of the VSCode Pylint logs:

2024-04-17 14:44:36.710 [info] [Error - 2:44:36 PM] Traceback (most recent call last):
  File "<frozen runpy>", line 148, in _get_module_details
  File "<frozen runpy>", line 142, in _get_module_details
ImportError: No module named pylint.__main__

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sdemartini/.vscode/extensions/ms-python.pylint-2023.10.1/bundled/tool/lsp_server.py", line 770, in _run_tool_on_document
    result = utils.run_module(
             ^^^^^^^^^^^^^^^^^
  File "/Users/sdemartini/.vscode/extensions/ms-python.pylint-2023.10.1/bundled/tool/lsp_utils.py", line 208, in run_module
    return _run_module(module, argv, use_stdin, source)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sdemartini/.vscode/extensions/ms-python.pylint-2023.10.1/bundled/tool/lsp_utils.py", line 193, in _run_module
    runpy.run_module(module, run_name="__main__")
  File "<frozen runpy>", line 222, in run_module
  File "<frozen runpy>", line 152, in _get_module_details
ImportError: No module named pylint.__main__; 'pylint' is a package and cannot be directly executed
poulposse commented 3 months ago

I am not sure that we got the same issue, @sjdemartini . Though I agree with you that it does not seem to be a pylint-django issue, but rather a pylint or VSCode extension issue, since like @LCH-1 , a run in the terminal outputs correct results. In my case, the behavior was the following: in VSCode, the output of the very first call to pylint was correct, then the subsequent outputs were giving false positives (as if pylint was run without django plugin), although the call was exactly the same.

It turns out that the --clear-cache-post-run argument of pylint (that is added to my arguments by VSCode extension) is responsible of this behavior. I commented it out in lsp_server.py (function _linting_helper) in the VSCode extension, and it now behaves well.

(VSCode extension: v2023.10.1, pylint: 3.0.2, pylint-django: 4.5.2)