Skip to content

Conversation

@thejchap
Copy link
Contributor

@thejchap thejchap commented Nov 13, 2025

Summary

astral-sh/ty#111

this pr adds a cannot-overwrite-attribute diagnostic when a custom __setattr__ or __delattr__ is defined on a dataclass where frozen=True (docs)

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[cannot-overwrite-attribute]: 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 `cannot-overwrite-attribute` is enabled by default

Found 1 diagnostic

Test Plan

  • new mdtests
  • e2e
  • the attrs mypy primer diff looks to be a true positive - the other results have been unpredictable and have changed every time i pushed new code, even if the diagnostic logic didn't change...

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 13, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 13, 2025

mypy_primer results

Changes were detected when running on open source projects
attrs (https://github.com/python-attrs/attrs)
+ tests/test_setattr.py:374:17: error[cannot-overwrite-attribute] Cannot overwrite attribute `__setattr__` in class `CustomSetAttr`
- Found 601 diagnostics
+ Found 602 diagnostics

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/_logging.py:153:13: warning[unsupported-base] Unsupported class base with type `<class 'Mapping[str, Style]'> | <class 'Mapping[str, Divergent]'>`
- src/scikit_build_core/build/_pathutil.py:25:38: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `str | PathLike[str]`, found `DirEntry[Path]`
- src/scikit_build_core/build/_pathutil.py:27:24: error[invalid-argument-type] Argument to function `__new__` is incorrect: Expected `str | PathLike[str]`, found `DirEntry[Path]`
- src/scikit_build_core/build/wheel.py:98:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 42 diagnostics
+ Found 38 diagnostics

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- pandas-stubs/_typing.pyi:1217:16: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 5519 diagnostics
+ Found 5518 diagnostics

No memory usage changes detected ✅

@thejchap thejchap force-pushed the thejchap/frozen-setattr branch from dad0985 to 5c3f14a Compare November 15, 2025 05:58
@thejchap thejchap force-pushed the thejchap/frozen-setattr branch from 5c3f14a to 3272ab1 Compare November 30, 2025 02:24
@thejchap thejchap force-pushed the thejchap/frozen-setattr branch 9 times, most recently from da5dfae to b7e81c1 Compare December 8, 2025 00:34
@thejchap thejchap force-pushed the thejchap/frozen-setattr branch from b7e81c1 to bcf0226 Compare December 8, 2025 00:35
@thejchap thejchap marked this pull request as ready for review December 8, 2025 00:39
@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants