Skip to content
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 that attributes are defined #112

Open
Goldziher opened this issue Oct 29, 2022 · 4 comments
Open

Check that attributes are defined #112

Goldziher opened this issue Oct 29, 2022 · 4 comments
Labels
enhancement New feature or request needs refinement This issue needs refinement before work can start

Comments

@Goldziher
Copy link

Hi, i would like to ask for a small feature- a check that tests that attributes declared in slots are actually defined on the class.

@ariebovenberg
Copy link
Owner

What would this look like? Something like this?

class Foo:
    __slots__ = ('a', 'b', 'c', 'd')

    def __init__(self):
        self.a = ...
        self.b = ...
        self.c = ...
        # ERROR: slot 'd' is not used

@Goldziher
Copy link
Author

What would this look like? Something like this?

class Foo:
    __slots__ = ('a', 'b', 'c', 'd')

    def __init__(self):
        self.a = ...
        self.b = ...
        self.c = ...
        # ERROR: slot 'd' is not used

Exactly

@ariebovenberg ariebovenberg added enhancement New feature or request needs refinement This issue needs refinement before work can start labels Oct 30, 2022
@ariebovenberg
Copy link
Owner

I can definitely see this being useful, although I can't think of a way to do this without some kind of static analysis.
That makes this a complex feature and triggers design decisions about slotscheck: should static analysis be added? Should it be in a separate flake8 plugin?

The decision to make it an import-time checker does give major advantages (#6).

For now, I'll have to put this on hold -- if there is more interest that will bump priority

@ariebovenberg
Copy link
Owner

ariebovenberg commented Oct 31, 2022

Giving this some more thought:

  1. some basic static analysis wouldn't be too hard, although I can imagine it to be quite costly for performance (each class imported would also need to be statically analyzed)
  2. There should probably be an easy way to disable this on a per-class basis, since I can imagine rare (but not implausible) cases where a slot is not used in the class definition itself, but only set from outside.
  3. There should probably also be a mechanism to allow 'unused' slots if they are used in a subclass. Or perhaps slotscheck should enforce these to be defined in subclasses only... 🤔

@ariebovenberg ariebovenberg changed the title Enhancement: check that attribtues are defined Check that attributes are defined Mar 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs refinement This issue needs refinement before work can start
Projects
None yet
Development

No branches or pull requests

2 participants