-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Check "__slots__" and "@dataclass" attributes #9499
Comments
Thank you for opening the issue, how would you name this new message ? There is already some slot related messages in the classe checker (https://github.com/pylint-dev/pylint/blob/main/pylint/checkers/classes/class_checker.py#L665), but it seems it could go in the data classes checker (https://github.com/pylint-dev/pylint/blob/main/pylint/checkers/dataclass_checker.py) |
Thanks for asking.
I wonder that there still is an assigning-non-slot. Regarding that existing name I would suggest using For the docs and based on Problematic codeclass Student:
__slots__ = ("name",)
name: str
surname: str # [define-non-slot] Correct code:class Student:
__slots__ = ("name", "surname")
name: str
surname: str |
Hi there! I'm looking for a way to contribute and I'd like to try implementing this feature. As well as the Problematic code:class Student:
__slots__ = ("name", "age") # [missing-slot-annotation]
name: str Correct code:class Student:
__slots__ = ("name", "age")
name: str
age: int I'd consider this merely a warning rather than an error as this code will still run with the These checks shouldn't be evaluated if |
Sounds like a useful warning to me! |
I have an implementation for A few points:
|
@adamtuft do you think this can be closed following the merge of the new checker ? |
The new checker covers the original issue, but there was also some discussion above of a |
Let's keep open for discussion about |
Current problem
The following is valid code but implicates some problems.
There are two slot variables (foo & bar) but only
foo
is handled by the@dataclass
. For examplestr(MyClass())
will result inMyClass(foo="one")
. The variablebar
is missing in that output no matter that it exists.Desired solution
PyLint should warn about that.
The list of variables in
__slot__
do not fit to the list specificied for@dataclass
.Python 3.10 or higher to offer
@dataclass(slots=True)
to prevent situations like this. So this could be one possible solution PyLint could suggest. But there are also older Python versions still relevant.Additional context
No response
The text was updated successfully, but these errors were encountered: