Skip to content

Disable the special __setattr__ handling #12696

Closed
@deifactor

Description

@deifactor

Feature

I'd like to be able to disable the behavior introduced in #3451 where defining __setattr__ makes mypy assume all property assignments are valid.

Pitch

Suppose I have a class that logs all attribute accesses:

@dataclass
class MyClass:
  field: int
  another_field: str

  def __setattr__(self, key: str, val: Any) -> None:
    print(f"You just set {key}!")
    super().__setattr__(key, val)

Defining __setattr__ means that I can now write my_class.feild = 123 and mypy won't catch it. My current workaround is to define a free function and do MyClass.__setattr__ = _setattr, but that's kind of janky. I'd like to be able to use a magic comment or a magic dunder variable (__mypy_no_special_setattr_handling__) to disable this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions