Skip to content

Attribute assignments that come before the definition of __setattr__ don't call it #1239

Closed
python/mypy
#22158

Description

@rheard
class C:
    def __init__(self) -> None:
        self.x = 0

    def __setattr__(self, name: str, value: object) -> None:
        print("setattr", name)
        super().__setattr__(name, value)

    def after(self) -> None:
        self.x = 1


c = C()
c.after()

Interpreted, this prints setattr x twice. Compiled with current master, it prints it once: the assignment in __init__ writes the attribute directly, without calling __setattr__. If __setattr__ is moved above __init__, it's called both times.

Activity

  1. added a commit that references this issue on Oct 13, 2026
    a645d53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions