pylint
pylint copied to clipboard
False positive ``W0612`` / ``unused-variable`` with dataclasses
Bug description
With the minimal code shown below, I get error W0612, unused variable _HAS_DEFAULT_FACTORY at line 1 column 1, the start of the code. The module docstring is not relevant, it is just there to show that the error is signalled at line 1, column 1, no matter the content.
My config file forces all warnings, that's true, but it is still a false positive.
"""."""
import dataclasses
@dataclasses.dataclass
class SomeDataClass():
"""."""
SomeDataClass()
Configuration
[MASTER]
jobs=0
unsafe-load-any-extension=yes
[MESSAGES CONTROL]
enable=all
disable=I,too-many-lines
[REPORTS]
#reports=yes
msg-template='{path}:{line}:{column}: {msg_id} ({symbol}) {msg}'
[BASIC]
good-names=i,j,c,_
include-naming-hint=yes
[FORMAT]
max-line-length=120
[VARIABLES]
allow-global-unused-variables=no
Command used
pylint pylint_bug.py
Pylint output
************* Module pylint_bug
pylint_bug.py:1:0: W0612 (unused-variable) Unused variable '_HAS_DEFAULT_FACTORY'
------------------------------------------------------------------
Your code has been rated at 6.67/10 (previous run: 6.67/10, +0.00)
Expected behavior
No errors.
Pylint version
pylint 3.0.2
astroid 3.0.1
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
OS / Environment
Windows 11 Home 10.0.22631 N/D Compilación 22631
I can't reproduce.. Is dataclasses overwritten by a local module?
Not that I know of, @DanielNoord but I'll check my system just in case, as soon as possible. During the weekend I think I can do a clean reinstall of Python on my system, removing all installed Python packages, just in case some of them is the culprit (they are all pretty standard, pylint, its dependencies and a few others) and I'll try to get my hands on another otherwise pristine system and I will test there, too.
Thanks for the interest!
I could take a minute and I did a thorough cleanup of my system (to the best of my knowledge, of course, I can't be sure there are not any cached files left), performed a clean install of Python 3.12, a clean install of Pylint and I can reproduce the problem.
I don't know if I made clear that my config file (the one I posted) has allow-global-unused-variables=no, but this is intended, in order to produce W0612 errors for unused global variables, which is something I want for my code as a default. Still, this is a false positive.
So, since you cannot reproduce the problem, @DanielNoord but I can after a clean install, is there any way I can produce some debugging output for pylint so I can try to pinpoint the problem myself?
Thanks a lot in advance!
Hey @DervishD thank you opening an issue. Are you able to provide a dockerfile based on python:3.11 to reproduce ?
For example: 3.11.6-bookworm
Or if this is windows specific: 3.11.6-windowsservercore-ltsc2022 3.11.6-windowsservercore-1809
@Pierre-Sassoulas I replied to your comment but somehow the reply got lost. I'll try to reproduce it: I have never used docker (or other containers), and I don't know how to create one, or a dockerfile for that matter. If you just need a dockerfile which reflects my system, I think I could provide one using the ones you point as base, but I don't know if it would be useful.
Last, but not least: can the dockerfile be based on python 3.12? I can reproduce the problem in that version and it is already installed in the two computers I use for development, the same ones I used to reproduce the problem.
While I try to learn how to write a dockerfile, is there any other way I can help with debugging?
Thanks A LOT for your reply and your interest!
Sorry, I should have said python 3.12, my mistake. The way to do that would be to install docker and then you can create an almost empty docker file
FROM python:3.12
RUN pip3 install pylint==3.0.2
RUN ...
You can test what it does with docker run -it . (or docker exec to test directly inside it before creating the run). Then we will be able to reproduce using either the dockerfile you run with docker run or the list of command you used on the base docker file in docker exec
Got it, then. I'll install docker in the machines where I can reproduce the problem as soon as I can. I'll try to reproduce the problem in a Linux Mint VM I have set up, just in case, so I can provide a dockerfile for Linux, too.
I'll keep you informed, and again thanks A LOT for your help 😄
First of all, sorry for the delay, I have had no time to go back to this, I'm very sorry.
But today I had time to do further tests. I have been able to reproduce the problem in quite an amount of unrelated computers:
- 2x
Windows 10, python 3.12, pylint 3.0.2 - 3x
Windows 11, python 3.12, pylint 3.0.2 - 5x
WSL, Ubuntu 22.04.3LTS, python 3.10.12, pylint 3.0.1 - 2x
macos 12.7.1, python 3.12 and python 3.11, pylint 3.0.1 and 3.0.2 - 6x
Linux Mint 21.2, python 3.10.12, pylint 3.0.3
So, I don't think the problem is related to my computer or installation. The only way the bug does not trigger is that the configuration used DO NOT SET allow-global-unused-variables=no. In that case, obviously, the problem is not reported, but I want that in my configuration, in order to produce W0612 errors for unused global variables, and anyway, setting that flag to no does not get rid of the false positive, it just hides it.
I don't think a dockerfile is going to help here, since the problem does not seem to be related to OS version, Python version or even pylint version, it seems only related to using allow-global-unused-variables=no. But if it is essential I can try my best to generate one if I can install Docker-Desktop on the main computer I have access to, which uses the Hyper-V for WSL so I'm not sure docker will work there.
And again, thanks a lot 😃
Thank you for the tip and all your effort to reproduce @DervishD, I can reproduce with:
a.py
import dataclasses
@dataclasses.dataclass
class SomeDataClass():
...
Command: pylint a.py --allow-global-unused-variables=no
Thanks to you, @Pierre-Sassoulas for your interest in this problem 😄 My pleasure, and if you finally need the dockerfile, just tell me and I'll try my best to produce one.
I can confirm that it still happens in Windows 11, python 3.12, pylint 3.2.4 .