mypy - prevent undeclared members?

I just realized, after years of using mypy, that I can assign a class member that was not explicitly defined in the class definition. Can I force mypy to flag those? I can't find any option for that. I use --strict all the time.

class Foo:
    x:int

    def __init__(self) -> None:
        self.x=3
        self.y=5   # I want this to fail

Posted Feb 19, 2026, 3:07 PM UTC

18
Loading comments…