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

Warn if instance atribute is defined outside of __init__ #10552

Open
AndrewUshakov opened this issue May 29, 2021 · 3 comments
Open

Warn if instance atribute is defined outside of __init__ #10552

AndrewUshakov opened this issue May 29, 2021 · 3 comments
Labels

Comments

@AndrewUshakov
Copy link

Feature

(Optionally?) generate error if the instance attribute is defined outside of __init__

Pitch

Mypy does not generate any errors for the code below:

class Tst:
    def __init__(self) -> None:
        ...

    def create_field(self) -> None:
        self.abc = 1  # Please, generate error here

    def print_field(self) -> None:
        print(self.abc)  # And, optionally here...


def main() -> None:
    tst = Tst()
    tst.print_field()


if __name__ == '__main__':
    main()

which fails with run time error, caused by the absence of the abc attribute definition in the __init__ method.

Additionaly:

PyCharm and pylint generate error in the line 6.

@erictraut
Copy link

erictraut commented Jun 2, 2021

For what it's worth, I prototyped this feature in pyright after noticing this issue. We found that it was very noisy (hundreds or thousands of false positive errors in some representative code bases) and did little, if anything, to discover actual bugs. The largest source of errors was in unit tests, which typically have a method such as setUp where instance variables are initialized. Another common pattern is to initialize instance variables from a method that is called from the constructor. For these reasons, we felt that very few developers would find this feature useful, and we decided not to include it in pyright.

@AndrewUshakov
Copy link
Author

PyCharm silently ignores this error if name of the method is setUp ;) It can be also switched off in settings. In mypy # type: ignore or mypy.ini with different settings for different project parts, if control detection of this issue with a new command-line option, could be used.

By the way, mypy generates an error if instance attribute is created outside of the class...

@mrcljx
Copy link

mrcljx commented Nov 16, 2022

I just ran into an issue because of a typo and was a bit surprised that Mypy didn't warn me.

class MyClass:
  _cached_value: Optional[int] = None

  def value(self) -> None:
    if (v := self._cached_value) is not None and v > 0:
      return v
    self._cached_valoo = generator() # wrong attr is set
    return self._cached_valoo

It looks like the only static analyzer that catches this otherwise is Pylint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants