Skip to content

Disable the special __setattr__ handling #12696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
deifactor opened this issue Apr 29, 2022 · 1 comment
Closed

Disable the special __setattr__ handling #12696

deifactor opened this issue Apr 29, 2022 · 1 comment
Labels

Comments

@deifactor
Copy link

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.

@hauntsaninja
Copy link
Collaborator

try:

if not typing.TYPE_CHECKING:
    def __setattr__(self, ...): ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants