Skip to content

Commit 0750bc2

Browse files
authored
copy bindings (#2176)
* copy bindings * changelog * simplify
1 parent 2094d6b commit 0750bc2

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## Unreleased
9+
10+
### Fixed
11+
12+
- Fixed bindings persistance https://github.com/Textualize/textual/issues/1613
13+
814
## [0.17.1] - 2023-03-30
915

1016
### Fixed

src/textual/binding.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import TYPE_CHECKING, Iterable, MutableMapping
4+
from typing import TYPE_CHECKING, Iterable
55

66
import rich.repr
77

@@ -92,12 +92,22 @@ def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]:
9292
priority=binding.priority,
9393
)
9494

95-
self.keys: MutableMapping[str, Binding] = (
95+
self.keys: dict[str, Binding] = (
9696
{binding.key: binding for binding in make_bindings(bindings)}
9797
if bindings
9898
else {}
9999
)
100100

101+
def copy(self) -> Bindings:
102+
"""Return a copy of this instance.
103+
104+
Return:
105+
New bindings object.
106+
"""
107+
copy = Bindings()
108+
copy.keys = self.keys.copy()
109+
return copy
110+
101111
def __rich_repr__(self) -> rich.repr.Result:
102112
yield self.keys
103113

src/textual/dom.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ def __init__(
165165
self._auto_refresh: float | None = None
166166
self._auto_refresh_timer: Timer | None = None
167167
self._css_types = {cls.__name__ for cls in self._css_bases(self.__class__)}
168-
self._bindings = self._merged_bindings or Bindings()
168+
self._bindings = (
169+
Bindings()
170+
if self._merged_bindings is None
171+
else self._merged_bindings.copy()
172+
)
169173
self._has_hover_style: bool = False
170174
self._has_focus_within: bool = False
171175

0 commit comments

Comments
 (0)