OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

mypy/pyright do not warn about undefined class attribute

  • Thread starter Thread starter asdfjlsdkfasjfowei
  • Start date Start date
A

asdfjlsdkfasjfowei

Guest
Code:
class A:
    def set_this_or_everything_breaks(self) -> None:
        self.important_variable = "c"

    def run(self) -> None:
        print(f"this is what is important {self.important_variable.lower()}")

a = A()
a.run()

Why does mypy not complain about self.important_variable being undefined? How can I make it complain? If I remove set_this_or_everything_breaks it'll complain but it should be complaining. Can I force all class attributes to be defined on the class? I would expect that I'd get a possibly undefined error here.
<pre><code>class A:
def set_this_or_everything_breaks(self) -> None:
self.important_variable = "c"

def run(self) -> None:
print(f"this is what is important {self.important_variable.lower()}")

a = A()
a.run()
</code></pre>
<p>Why does mypy not complain about <code>self.important_variable</code> being undefined? How can I make it complain? If I remove <code>set_this_or_everything_breaks</code> it'll complain but it should be complaining. Can I force all class attributes to be defined on the class? I would expect that I'd get a possibly undefined error here.</p>
 

Latest posts

Top