Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 8a23bde

Browse files
authored
Consistently use collections.abc.Mapping to check frozendict. (#12564)
1 parent e8d1ec0 commit 8a23bde

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

changelog.d/12564.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Consistently check if an object is a `frozendict`.

synapse/events/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
)
2828

2929
import attr
30-
from frozendict import frozendict
3130

3231
from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
3332
from synapse.api.errors import Codes, SynapseError
@@ -204,7 +203,9 @@ def _copy_field(src: JsonDict, dst: JsonDict, field: List[str]) -> None:
204203
key_to_move = field.pop(-1)
205204
sub_dict = src
206205
for sub_field in field: # e.g. sub_field => "content"
207-
if sub_field in sub_dict and type(sub_dict[sub_field]) in [dict, frozendict]:
206+
if sub_field in sub_dict and isinstance(
207+
sub_dict[sub_field], collections.abc.Mapping
208+
):
208209
sub_dict = sub_dict[sub_field]
209210
else:
210211
return
@@ -622,7 +623,7 @@ def validate_canonicaljson(value: Any) -> None:
622623
# Note that Infinity, -Infinity, and NaN are also considered floats.
623624
raise SynapseError(400, "Bad JSON value: float", Codes.BAD_JSON)
624625

625-
elif isinstance(value, (dict, frozendict)):
626+
elif isinstance(value, collections.abc.Mapping):
626627
for v in value.values():
627628
validate_canonicaljson(v)
628629

synapse/handlers/relations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import collections.abc
1415
import logging
1516
from typing import (
1617
TYPE_CHECKING,
@@ -24,7 +25,6 @@
2425
)
2526

2627
import attr
27-
from frozendict import frozendict
2828

2929
from synapse.api.constants import RelationTypes
3030
from synapse.api.errors import SynapseError
@@ -380,7 +380,7 @@ async def get_bundled_aggregations(
380380
# Do not bundle aggregations for an event which represents an edit or an
381381
# annotation. It does not make sense for them to have related events.
382382
relates_to = event.content.get("m.relates_to")
383-
if isinstance(relates_to, (dict, frozendict)):
383+
if isinstance(relates_to, collections.abc.Mapping):
384384
relation_type = relates_to.get("rel_type")
385385
if relation_type in (RelationTypes.ANNOTATION, RelationTypes.REPLACE):
386386
continue

synapse/storage/databases/main/state.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import collections.abc
1516
import logging
1617
from typing import TYPE_CHECKING, Collection, Dict, Iterable, Optional, Set, Tuple
1718

18-
from frozendict import frozendict
19-
2019
from synapse.api.constants import EventTypes, Membership
2120
from synapse.api.errors import NotFoundError, UnsupportedRoomVersionError
2221
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
@@ -160,7 +159,7 @@ async def get_room_predecessor(self, room_id: str) -> Optional[JsonMapping]:
160159
predecessor = create_event.content.get("predecessor", None)
161160

162161
# Ensure the key is a dictionary
163-
if not isinstance(predecessor, (dict, frozendict)):
162+
if not isinstance(predecessor, collections.abc.Mapping):
164163
return None
165164

166165
# The keys must be strings since the data is JSON.

synapse/util/frozenutils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import collections.abc
1415
from typing import Any
1516

1617
from frozendict import frozendict
@@ -35,7 +36,7 @@ def freeze(o: Any) -> Any:
3536

3637

3738
def unfreeze(o: Any) -> Any:
38-
if isinstance(o, (dict, frozendict)):
39+
if isinstance(o, collections.abc.Mapping):
3940
return {k: unfreeze(v) for k, v in o.items()}
4041

4142
if isinstance(o, (bytes, str)):

0 commit comments

Comments
 (0)