Git Product home page Git Product logo

Comments (9)

buhtz avatar buhtz commented on July 24, 2024 1

Thanks for asking.

how would you name this new message ?

I wonder that there still is an assigning-non-slot. Regarding that existing name I would suggest using declare-non-slot (or define-non-slot). I can't diffrenciate between declare and define.

For the docs and based on assigning-non-slot:

Problematic code

class Student:
    __slots__ = ("name",)

    name: str
    surname: str  # [define-non-slot]

Correct code:

class Student:
    __slots__ = ("name", "surname")

    name: str
    surname: str

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on July 24, 2024 1

Hey @adamtuft, thanks a lot for working on this ! Do you mind opening a pull request directly from your branch ? (Use "Refs #9499" in the description :) )

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on July 24, 2024

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)

from pylint.

adamtuft avatar adamtuft commented on July 24, 2024

Hi there! I'm looking for a way to contribute and I'd like to try implementing this feature.

As well as the [define-non-slot] case I can see another case that you might want to warn on:

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 dataclass decorator, whereas it won't in the [define-non-slot] case.

These checks shouldn't be evaluated if __slots__ contains __dict__.

from pylint.

DanielNoord avatar DanielNoord commented on July 24, 2024

Sounds like a useful warning to me!

from pylint.

adamtuft avatar adamtuft commented on July 24, 2024

I have an implementation for declare-non-slot at https://github.com/adamtuft/pylint/tree/9499-slots-and-class-annotations, would be very grateful for comments & discussion! I will also implement missing-slot-annotation when I have a little more time.

A few points:

  • I thought this check belonged in the class checker instead of the dataclass checker since the problematic code is problematic even without the dataclass decorator. There may be other cases where the class annotations and __slots__ items should match, and making this check dataclass-specific would miss those cases.
  • I went with declare-non-slot over define-non-slot as annotating a class feels more like a declaration that such a member exists, rather than a definition of that member.
  • I made some changes to existing code to factor out some logic into a helper function _get_classdef_slots_names as this seemed the cleanest way to re-use existing logic. I hope this is ok.
  • I've checked that the existing tests still pass and have run the primer tests with python3 -m pytest -m primer_stdlib --primer-stdlib.

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on July 24, 2024

@adamtuft do you think this can be closed following the merge of the new checker ?

from pylint.

adamtuft avatar adamtuft commented on July 24, 2024

@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 missing-slot-annotation warning. Would you still want this? It could be implemented later, referencing this (closed) issue.

from pylint.

Pierre-Sassoulas avatar Pierre-Sassoulas commented on July 24, 2024

Let's keep open for discussion about missing-slot-annotation then. I'm +1 myself.

from pylint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.