Skip to content

Commit 79774aa

Browse files
committed
fix(dataclass): Update frozen_dataclass implementation for type safety
why: Ensure custom frozen_dataclass is fully type-checker compatible what: - Added type ignore directives for method assignment in frozen_dataclass - Added global mypy directive to silence inheritance errors in tests - Fixed variable naming for exception handling in tests - Ensured tests properly handle both AttributeError and NotImplementedError refs: Completes full type checking compatibility for frozen dataclasses
1 parent f55a0d0 commit 79774aa

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/libtmux/_internal/frozen_dataclass.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def __delattr__(self: t.Any, name: str) -> None:
6969
object.__delattr__(self, name)
7070

7171
# F. Inject into the class
72-
cls.__init__ = __init__
73-
cls.__setattr__ = __setattr__
74-
cls.__delattr__ = __delattr__
72+
# Add type ignore directives to silence mypy "Cannot assign to a method" errors
73+
cls.__init__ = __init__ # type: ignore[method-assign]
74+
cls.__setattr__ = __setattr__ # type: ignore[method-assign]
75+
cls.__delattr__ = __delattr__ # type: ignore[method-assign]
7576

7677
return cls

tests/_internal/test_frozen_dataclass.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def resize(self, width: int, height: int) -> None:
2525
self.height = height
2626

2727

28+
# Silence specific mypy errors with a global disable
29+
# mypy: disable-error-code="misc"
30+
31+
2832
# 2. Subclass the mutable BasePane, but freeze it with our custom decorator
2933
@frozen_dataclass
3034
class PaneSnapshot(BasePane):
@@ -93,7 +97,7 @@ def test_immutability() -> None:
9397

9498
# Attempting to modify a field should raise AttributeError
9599
with pytest.raises(AttributeError) as excinfo:
96-
snapshot.width = 200
100+
snapshot.width = 200 # type: ignore
97101
assert "immutable" in str(excinfo.value)
98102

99103
# Attempting to add a new field should raise AttributeError
@@ -107,9 +111,10 @@ def test_immutability() -> None:
107111
assert "immutable" in str(excinfo.value)
108112

109113
# Calling a method that tries to modify state should fail
110-
with pytest.raises(NotImplementedError) as excinfo:
114+
# Use separate variable for the NotImplementedError exception info
115+
with pytest.raises(NotImplementedError) as resize_excinfo:
111116
snapshot.resize(200, 50)
112-
assert "immutable" in str(excinfo.value)
117+
assert "immutable" in str(resize_excinfo.value)
113118

114119

115120
def test_nested_references() -> None:

0 commit comments

Comments
 (0)