You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you intend to initialise an instance variable in __init__ but forget to do so, mypy won't warn if PEP 526 style instance variable annotation is used. For example, the following code emits a runtime warning but no warning from mypy. Tested on mypy version 0.540.
class MyClass:
my_attr: str
def __init__(self, my_attr: str) -> None:
pass
# missing line: self.my_attr = my_attr
x = MyClass("grapefruit")
s: str = x.my_attr
# ^ AttributeError at runtime
However, if the instance variable annotation is removed, mypy does warn.
Uh oh!
There was an error while loading. Please reload this page.
If you intend to initialise an instance variable in
__init__
but forget to do so, mypy won't warn if PEP 526 style instance variable annotation is used. For example, the following code emits a runtime warning but no warning from mypy. Tested on mypy version 0.540.However, if the instance variable annotation is removed, mypy does warn.
From PEP 526 (emphasis mine):
The text was updated successfully, but these errors were encountered: