ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Rule for missing members typing annotations

Open yairp03 opened this issue 1 year ago • 2 comments

Consider the following class:

class Person:
    name: str

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

The rule would suggests adding a typing annotation for Person.age so the code would look like this:

class Person:
    name: str
    age: int

    def __init__(self, name: str, age: int) -> None:
        self.name = name
        self.age = age

yairp03 avatar Oct 24 '24 16:10 yairp03

Thanks. Such a rule does make sense to me but Ruff's current capabilities only allow this rule to provide precise results when the class has no base classes because it can't resolve attributes from other classes (at least not if they're defined in other files). This would make the rule less useful.

MichaReiser avatar Oct 25 '24 07:10 MichaReiser

I'm not a fan of the extra boilerplate and bookkeeping personally. Type-checkers already scan for assignments in __init__. And if you don't enable a rule to ensure that you don't forget to initialize, you may end up with extra declarations if you remove the member later.

Avasam avatar Oct 25 '24 11:10 Avasam

@Avasam There could also be a rule for redundant type annotations that aren't used at all

yairp03 avatar Oct 30 '24 20:10 yairp03