mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 13:30:49 -05:00
## Summary https://github.com/astral-sh/ty/issues/111 this pr adds an `invalid-dataclass-override` diagnostic when a custom `__setattr__` or `__delattr__` is defined on a dataclass where `frozen=True` ([docs](https://docs.python.org/3/library/dataclasses.html#frozen-instances)) ### Runtime exception ``` Traceback (most recent call last): File "/Users/justinchapman/src/ty-playground/main.py", line 4, in <module> @dataclass(frozen=True) ~~~~~~~~~^^^^^^^^^^^^^ File "/Users/justinchapman/.local/share/uv/python/cpython-3.13.0-macos-aarch64-none/lib/python3.13/dataclasses.py", line 1295, in wrap return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, match_args, kw_only, slots, weakref_slot) File "/Users/justinchapman/.local/share/uv/python/cpython-3.13.0-macos-aarch64-none/lib/python3.13/dataclasses.py", line 1157, in _process_class func_builder.add_fns_to_class(cls) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^ File "/Users/justinchapman/.local/share/uv/python/cpython-3.13.0-macos-aarch64-none/lib/python3.13/dataclasses.py", line 516, in add_fns_to_class raise TypeError(error_msg) TypeError: Cannot overwrite attribute __setattr__ in class A ``` ### Diagnostic ``` error[invalid-dataclass-override]: Cannot overwrite attribute __setattr__ in class A --> /Users/justinchapman/src/ty-playground/main.py:6:5 | 4 | @dataclass(frozen=True) 5 | class A: 6 | def __setattr__(self, name: str, value: object) -> None: ... | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | info: __setattr__ info: rule `invalid-dataclass-override` is enabled by default Found 1 diagnostic ``` ## Test Plan - new mdtests - e2e - the `attrs` mypy primer diff looks to be a [true positive](https://github.com/python-attrs/attrs/blob/main/tests/test_setattr.py#L373) - the other results have been unpredictable and have changed every time i pushed new code, even if the diagnostic logic didn't change... --------- Co-authored-by: Carl Meyer <carl@astral.sh>
ty
ty is an extremely fast type checker.
The Rust code for ty lives in this repository; see CONTRIBUTING.md for more information on contributing to ty.
See the ty repo for ty documentation and releases.